Exemplo n.º 1
0
        public async Task Should_Return_Forbidden_Status_Code_When_Requesting_By_User_Client()
        {
            var factory = new UserWebApplicationFactory("UserShouldNotUpdateAccountRolesIntegrationTest");
            var client  = factory.WithWebHostBuilder(builder => builder.ConfigureWebHostBuilderForIntegrationTest())
                          .CreateClient(new WebApplicationFactoryClientOptions
            {
                AllowAutoRedirect = false
            });
            var accountRoleEntity = await InsertUserRoleEntityIfNotExistsAsync(factory.DbContext);

            var accountEntity = await InsertAccountEntityAsync(factory.DbContext, accountRoleEntity.Id);

            var updateAccountRolesRequest = new UpdateAccountRolesRequest
            {
                Roles = new List <Guid> {
                    accountRoleEntity.Id
                }
            };
            var updateAccountRolesRequestString = JsonConvert.SerializeObject(updateAccountRolesRequest);
            var requestContent = new StringContent(updateAccountRolesRequestString, Encoding.UTF8, "application/json");

            client.DefaultRequestHeaders.Add("api-version", "1");

            var response = await client.PutAsync($"api/accounts/{accountEntity.Id}/roles", requestContent);

            response.StatusCode.Should().BeEquivalentTo(HttpStatusCode.Forbidden);
        }
Exemplo n.º 2
0
        public async Task Should_Assign_Password_When_Requesting_By_User_Client()
        {
            var factory = new UserWebApplicationFactory("UserShouldSetPasswordIntegrationTest");
            var client  = factory.WithWebHostBuilder(builder => builder.ConfigureWebHostBuilderForIntegrationTest())
                          .CreateClient(new WebApplicationFactoryClientOptions
            {
                AllowAutoRedirect = false
            });
            var accountEntity = await InsertAccountEntityAsync(factory.DbContext);

            var assignPasswordRequest = new AssignPasswordRequest
            {
                Password        = "******",
                ConfirmPassword = "******"
            };
            var setPasswordRequestString = JsonConvert.SerializeObject(assignPasswordRequest);
            var requestContent           = new StringContent(setPasswordRequestString, Encoding.UTF8, "application/json");

            client.DefaultRequestHeaders.Add("api-version", "1");

            var response = await client.PostAsync($"api/accounts/{accountEntity.Id}/passwords/assignments", requestContent);

            await factory.DbContext.Entry(accountEntity).ReloadAsync();

            response.StatusCode.Should().BeEquivalentTo(HttpStatusCode.NoContent);
            accountEntity.PasswordHash.Should().NotBeNullOrEmpty();
        }
Exemplo n.º 3
0
        public async Task Should_Change_Password_When_Requesting_By_User_Client()
        {
            var factory = new UserWebApplicationFactory("UserShouldChangePasswordIntegrationTest");
            var client  = factory.WithWebHostBuilder(builder => builder.ConfigureWebHostBuilderForIntegrationTest())
                          .CreateClient(new WebApplicationFactoryClientOptions
            {
                AllowAutoRedirect = false
            });
            const string oldPassword   = "******";
            var          accountEntity = await InsertAccountEntityAsync(factory.DbContext, oldPassword);

            var oldPasswordHash       = accountEntity.PasswordHash;
            var changePasswordRequest = new ChangePasswordRequest
            {
                OldPassword        = oldPassword,
                NewPassword        = "******",
                ConfirmNewPassword = "******"
            };
            var changePasswordRequestString = JsonConvert.SerializeObject(changePasswordRequest);
            var requestContent = new StringContent(changePasswordRequestString, Encoding.UTF8, "application/json");

            client.DefaultRequestHeaders.Add("api-version", "1");

            var response = await client.PostAsync($"api/accounts/{accountEntity.Id}/passwords/changes", requestContent);

            response.StatusCode.Should().BeEquivalentTo(HttpStatusCode.NoContent);
            await factory.DbContext.Entry(accountEntity).ReloadAsync();

            accountEntity.PasswordHash.Should().NotBe(oldPasswordHash);
        }
Exemplo n.º 4
0
        public async Task Should_Delete_Account_When_Requesting_By_User_Client()
        {
            var factory = new UserWebApplicationFactory("UserShouldDeleteAccountIntegrationTest");
            var client  = factory.WithWebHostBuilder(builder => builder.ConfigureWebHostBuilderForIntegrationTest())
                          .CreateClient(new WebApplicationFactoryClientOptions
            {
                AllowAutoRedirect = false
            });
            var accountEntity = await InsertAccountEntityAsync(factory.DbContext);

            client.DefaultRequestHeaders.Add("api-version", "1");

            var response = await client.DeleteAsync($"api/accounts/{accountEntity.Id}");

            response.StatusCode.Should().BeEquivalentTo(HttpStatusCode.Accepted);
        }
Exemplo n.º 5
0
        public async Task Should_Return_Account_When_Requesting_By_User_Client()
        {
            var factory = new UserWebApplicationFactory("UserShouldGetAccountIntegrationTest");
            var client  = factory.WithWebHostBuilder(builder => builder.ConfigureWebHostBuilderForIntegrationTest())
                          .CreateClient(new WebApplicationFactoryClientOptions
            {
                AllowAutoRedirect = false
            });
            var accountEntity = await InsertAccountEntityAsync(factory.DbContext);

            var expectedResponse = PrepareExpectedResponse(accountEntity);

            client.DefaultRequestHeaders.Add("api-version", "1");

            var response = await client.GetAsync($"api/accounts/{accountEntity.Id}");

            var responseContentString = await response.Content.ReadAsStringAsync();

            response.StatusCode.Should().BeEquivalentTo(HttpStatusCode.OK);
            responseContentString.Should().BeEquivalentTo(expectedResponse);
        }
Exemplo n.º 6
0
        public UserControllerTest(UserWebApplicationFactory <UserService.Startup> factory, AuthWebApplicationFactory <AuthenticationService.Startup> authFactory)
        {
            //calling Auth API to get JWT
            AuthenticationService.Models.User user = new AuthenticationService.Models.User {
                UserId = "Mukesh", Password = "******"
            };
            _authclient = authFactory.CreateClient();
            HttpRequestMessage request   = new HttpRequestMessage();
            MediaTypeFormatter formatter = new JsonMediaTypeFormatter();

            // The endpoint or route of the controller action.
            var httpResponse = _authclient.PostAsync <AuthenticationService.Models.User>("/api/auth/login", user, formatter);

            httpResponse.Wait();
            // Deserialize and examine results.
            var stringResponse = httpResponse.Result.Content.ReadAsStringAsync();
            var response       = JsonConvert.DeserializeObject <TokenModel>(stringResponse.Result);

            _client = factory.CreateClient();
            //Attaching token in request header
            _client.DefaultRequestHeaders.Add("Authorization", $"Bearer {response.Token}");
        }
Exemplo n.º 7
0
 public UserControllerTest(UserWebApplicationFactory <Startup> factory)
 {
     _client = factory.CreateClient();
 }