Ejemplo n.º 1
0
        public async void PutUserPreferencesAsync_ValidAuthWithData_ReturnsOk()
        {
            // Given
            var databaseName = Guid.NewGuid().ToString();

            var(client, context) = ServiceTestingHelper.BuildServer <Startup, UserDbContext>(databaseName);
            var(token, userId)   = UserIdentity;

            var language           = "DK";
            var existingPreference = CreateUserPreference(userId, language);

            context.Preferences.Add(existingPreference);
            context.SaveChanges();

            var model   = MakePutPreferencesModel("GB");
            var request = CreatePutPreferencesRequest(token, userId, model);

            // When
            var response = await client.SendAsync(request);

            // Then
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            RefreshAll(context);
            Assert.Single(context.Preferences.Where(p => p.UserId == userId && p.PreferredLanguage == model.Language).ToList());
        }
Ejemplo n.º 2
0
        public async void GetUserPreferencesAsync_ValidAuthWithData_ReturnsOk()
        {
            // Given
            var databaseName = Guid.NewGuid().ToString();

            var(client, context) = ServiceTestingHelper.BuildServer <Startup, UserDbContext>(databaseName);
            var(token, userId)   = UserIdentity;

            var language           = "DK";
            var existingPreference = CreateUserPreference(userId, language);

            context.Preferences.Add(existingPreference);
            context.SaveChanges();

            var request = CreateGetPreferencesRequest(token, userId);

            // When
            var response = await client.SendAsync(request);

            // Then
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            var result = response.Content.ReadAsAsync <PreferencesModel>().Result;

            Assert.Equal(userId, result.UserId);
            Assert.Equal(language, result.Language);
        }
Ejemplo n.º 3
0
        public async void DeleteUserPreferencesAsync_ValidAuthNoData_ReturnsOk()
        {
            // Given
            var databaseName = Guid.NewGuid().ToString();

            var(client, context) = ServiceTestingHelper.BuildServer <Startup, UserDbContext>(databaseName);
            var(token, userId)   = AdminIdentity;

            var request = CreateDeletePreferencesRequest(token, Guid.NewGuid());

            // When
            var response = await client.SendAsync(request);

            // Then
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        }
Ejemplo n.º 4
0
        public async void GetUserPreferencesAsync_ExpiredAuth_ReturnsUnauthorized()
        {
            // Given
            var databaseName = Guid.NewGuid().ToString();

            var(client, context) = ServiceTestingHelper.BuildServer <Startup, UserDbContext>(databaseName);
            var(token, userId)   = ExpiredIdentity;

            var request = CreateGetPreferencesRequest(token, Guid.NewGuid());

            // When
            var response = await client.SendAsync(request);

            // Then
            Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
        }
Ejemplo n.º 5
0
        public async void PutUserPreferencesAsync_ValidAuthWrongUser_ReturnsForbid()
        {
            // Given
            var databaseName = Guid.NewGuid().ToString();

            var(client, context) = ServiceTestingHelper.BuildServer <Startup, UserDbContext>(databaseName);
            var(token, userId)   = UserIdentity;

            var model   = MakePutPreferencesModel("DK");
            var request = CreatePutPreferencesRequest(token, Guid.NewGuid(), model);

            // When
            var response = await client.SendAsync(request);

            // Then
            Assert.Equal(HttpStatusCode.Forbidden, response.StatusCode);
        }
Ejemplo n.º 6
0
        public async void PostUserPreferencesAsync_ValidAuthValidModel_ReturnsOk()
        {
            // Given
            var databaseName = Guid.NewGuid().ToString();

            var(client, context) = ServiceTestingHelper.BuildServer <Startup, UserDbContext>(databaseName);
            var(token, userId)   = AdminIdentity;

            var model   = MakePreferencesModel(Guid.NewGuid());
            var request = CreatePostPreferencesRequest(token, model);

            // When
            var response = await client.SendAsync(request);

            // Then
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            RefreshAll(context);
            Assert.Single(context.Preferences.Where(p => p.UserId == model.UserId).ToList());
        }
Ejemplo n.º 7
0
        public async void PostUserPreferencesAsync_ExpiredAuthValidModel_ReturnsUnauthorized()
        {
            // Given
            var databaseName = Guid.NewGuid().ToString();

            var(client, context) = ServiceTestingHelper.BuildServer <Startup, UserDbContext>(databaseName);
            var(token, userId)   = ExpiredIdentity;

            var model   = MakePreferencesModel(Guid.NewGuid());
            var request = CreatePostPreferencesRequest(token, model);

            // When
            var response = await client.SendAsync(request);

            // Then
            Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
            RefreshAll(context);
            Assert.Equal(0, context.Preferences.Count());
        }
Ejemplo n.º 8
0
        public async void PutUserPreferencesAsync_ValidAuthInvalidModel_ReturnsBadRequest()
        {
            // Given
            var databaseName = Guid.NewGuid().ToString();

            var(client, context) = ServiceTestingHelper.BuildServer <Startup, UserDbContext>(databaseName);
            var(token, userId)   = AdminIdentity;

            var model   = new PutPreferencesModel();
            var request = CreatePutPreferencesRequest(token, userId, model);

            // When
            var response = await client.SendAsync(request);

            // Then
            Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
            RefreshAll(context);
            Assert.Equal(0, context.Preferences.Count());
        }
Ejemplo n.º 9
0
        public async void DeleteUserPreferencesAsync_ValidAuthDeletedData_ReturnsOk()
        {
            // Given
            var databaseName = Guid.NewGuid().ToString();

            var(client, context) = ServiceTestingHelper.BuildServer <Startup, UserDbContext>(databaseName);
            var(token, userId)   = AdminIdentity;

            var existingPreference = CreateUserPreference(userId, "DK", true);

            context.Preferences.Add(existingPreference);
            context.SaveChanges();

            var request = CreateDeletePreferencesRequest(token, userId);

            // When
            var response = await client.SendAsync(request);

            // Then
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            RefreshAll(context);
            Assert.Single(context.Preferences.Where(p => p.UserId == userId && p.Deleted).ToList());
        }
Ejemplo n.º 10
0
        public async void PostUserPreferencesAsync_ValidAuthValidModelExistingUser_ReturnsUnauthorized()
        {
            // Given
            var databaseName = Guid.NewGuid().ToString();

            var(client, context) = ServiceTestingHelper.BuildServer <Startup, UserDbContext>(databaseName);
            var(token, userId)   = AdminIdentity;

            var model    = MakePreferencesModel(Guid.NewGuid());
            var request1 = CreatePostPreferencesRequest(token, model);
            var request2 = CreatePostPreferencesRequest(token, model);

            // When
            var response1 = await client.SendAsync(request1);

            var response2 = await client.SendAsync(request2);

            // Then
            Assert.Equal(HttpStatusCode.OK, response1.StatusCode);
            Assert.Equal(HttpStatusCode.BadRequest, response2.StatusCode);
            RefreshAll(context);
            Assert.Single(context.Preferences.Where(p => p.UserId == model.UserId).ToList());
            Assert.Equal(UserConstants.UserExists, await response2.Content.ReadAsStringAsync());
        }