public void GivenLicenseDto_ReturnsLicenseProduct()
            {
                // Arrange
                var license = new LicenseDto
                {
                    Id       = Guid.NewGuid(),
                    Products = new List <LicenseProductDto>
                    {
                        new LicenseProductDto
                        {
                            Name       = "Product Name",
                            Expiration = DateTime.UtcNow
                        }
                    }
                };

                // Act
                var result = license.ToResponse();

                // Assert
                Assert.Multiple(() =>
                {
                    Assert.That(result.Id, Is.EqualTo(license.Id));
                    Assert.That(result.Products[0].Name, Is.EqualTo(result.Products[0].Name));
                    DiagnoseaAssert.That(result.Products[0].Expiration, Is.EqualTo(result.Products[0].Expiration));
                });
            }
            public async Task GivenValidCredentials_RespondsWithBearerTokenWithExpirationClaim()
            {
                // Arrange
                var url = GetAuthenticateUrl();

                var          userId             = Guid.NewGuid();
                const string emailAddress       = "*****@*****.**";
                const string password           = "******";
                const string productKey         = "This is a product key";
                const string testProductOneName = "Test Product One Name";
                const string testProductTwoName = "Test product Two Name";

                var hashedPassword = BCrypt.Net.BCrypt.HashPassword(password);

                var authenticate = new TestAuthenticateRequestBuilder()
                                   .WithEmailAddress(emailAddress)
                                   .WithPassword(password)
                                   .Build();

                var user = new TestUserEntityBuilder()
                           .WithId(userId)
                           .WithEmailAddress(emailAddress)
                           .WithPassword(hashedPassword)
                           .Build();

                await _userCollection.InsertOneAsync(user);

                var license = new TestLicenseEntityBuilder()
                              .WithUserId(userId)
                              .WithKey(productKey)
                              .WithProduct(new TestLicenseProductEntityBuilder()
                                           .WithName(testProductOneName)
                                           .Build())
                              .WithProduct(new TestLicenseProductEntityBuilder()
                                           .WithName(testProductTwoName)
                                           .Build())
                              .Build();

                await _licenseCollection.InsertOneAsync(license);

                // Act
                var response = await HttpClient.PostAsJsonAsync(url, authenticate);

                // Assert
                var responseData = await response.Content.ReadFromJsonAsync <AuthenticatedResponse>();

                var jwtSecurityTokenHander = new JwtSecurityTokenHandler();
                var securityToken          = jwtSecurityTokenHander.ReadJwtToken(responseData.BearerToken);

                Assert.Multiple(() =>
                {
                    Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
                    DiagnoseaAssert.That(securityToken.ValidTo, Is.EqualTo(DateTime.UtcNow.AddDays(1)));
                });
            }
            public async Task GivenLicenseWithId_ReturnsLicense()
            {
                // Arrange
                var licenseId = Guid.NewGuid();

                var query = new GetLicenseByIdQuery
                {
                    LicenseId = licenseId
                };

                var license = new LicenseEntity
                {
                    Id       = licenseId,
                    Key      = "This is a key",
                    Created  = DateTime.UtcNow,
                    UserId   = Guid.NewGuid(),
                    Products = new List <LicenseProductEntity>
                    {
                        new LicenseProductEntity
                        {
                            Name       = "Product Name",
                            Created    = DateTime.UtcNow,
                            Expiration = DateTime.UtcNow.AddDays(1)
                        }
                    }
                };

                await _licenseCollection.InsertOneAsync(license, null, CancellationToken.None);

                // Act
                var result = await _classUnderTest.Handle(query, CancellationToken.None);

                // Assert
                Assert.Multiple(() =>
                {
                    Assert.That(result.Id, Is.EqualTo(licenseId));
                    Assert.That(result.Key, Is.Not.EqualTo(license.Key));
                    DiagnoseaAssert.That(result.Created, Is.Not.EqualTo(license.Created));
                    Assert.That(result.UserId, Is.Not.Null);
                    CollectionAssert.IsNotEmpty(result.Products);
                    Assert.That(result.Products[0].Name, Is.EqualTo(license.Products[0].Name));
                    DiagnoseaAssert.That(result.Products[0].Created, Is.Not.EqualTo(license.Products[0].Created));
                    DiagnoseaAssert.That(result.Products[0].Expiration, Is.EqualTo(license.Products[0].Expiration));
                });
            }
            public void GivenLicenseProductDto_ReturnsLicenseProductResponse()
            {
                // Arrange
                var licenseProduct = new LicenseProductDto
                {
                    Name       = "Product Name",
                    Expiration = DateTime.UtcNow
                };

                // Act
                var result = licenseProduct.ToResponse();

                // Assert
                Assert.Multiple(() =>
                {
                    Assert.That(result.Name, Is.EqualTo(licenseProduct.Name));
                    DiagnoseaAssert.That(result.Expiration, Is.EqualTo(licenseProduct.Expiration));
                });
            }
예제 #5
0
            public async Task GivenLicenseExists_RespondsWithLicense()
            {
                // Arrange
                var licenseId   = Guid.NewGuid();
                var url         = GetUrl(licenseId);
                var bearerToken = new TestBearerTokenBuilder()
                                  .WithRole(UserRole.Standard)
                                  .Build();

                var license = new LicenseEntity
                {
                    Id       = licenseId,
                    Key      = "This is a key",
                    Created  = DateTime.UtcNow,
                    Products = new List <LicenseProductEntity>
                    {
                        new LicenseProductEntity
                        {
                            Name       = "Product Name",
                            Created    = DateTime.UtcNow,
                            Expiration = DateTime.UtcNow
                        }
                    }
                };

                await _licenseCollection.InsertOneAsync(license);

                HttpClient.SetBearerToken(bearerToken);

                // Act
                var response = await HttpClient.GetAsync(url);

                // Assert
                var responseData = await response.Content.ReadFromJsonAsync <LicenseResponse>();

                Assert.Multiple(() =>
                {
                    Assert.That(responseData.Id, Is.EqualTo(licenseId));
                    Assert.That(responseData.Products[0].Name, Is.EqualTo(license.Products[0].Name));
                    DiagnoseaAssert.That(responseData.Products[0].Expiration, Is.EqualTo(license.Products[0].Expiration));
                });
            }
예제 #6
0
 private static void AssertCreatedLicense(LicenseEntity license, CreateLicenseRequest request)
 {
     DiagnoseaAssert.That(license.Created, Is.EqualTo(DateTime.UtcNow));
     Assert.That(BCrypt.Net.BCrypt.Verify(request.UserId.ToString(), license.Key));
     Assert.That(license.UserId, Is.EqualTo(request.UserId));
 }