public async Task GetById_ShouldSuccess()
        {
            var client = this.factory.CreateClient();

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                "Bearer",
                await AuthenticationUtilities.GetTestUserAccessTokenAsync(client));

            MessengerContext context = this.factory.Server.Host.Services.GetService(typeof(MessengerContext))
                                       as MessengerContext;

            this.SeedDataForMessagesTesting(context);
            User testUser = await context.Users.FirstAsync();

            Message message = await context.Messages.Skip(1).FirstAsync();

            var response = await client.GetAsync($"/api/messages/{message.MessageId}");

            response.EnsureSuccessStatusCode();

            var responseMessage = response.Content.ReadAsAsync(typeof(EditMessageViewModel)).Result as EditMessageViewModel;

            Assert.NotNull(responseMessage);
            Assert.Equal(message.Content, message.Content);
        }
        public async Task Delete_ShouldSuccess()
        {
            var client = this.factory.CreateClient();

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                "Bearer",
                await AuthenticationUtilities.GetTestUserAccessTokenAsync(client));

            MessengerContext context = this.factory.Server.Host.Services.GetService(typeof(MessengerContext))
                                       as MessengerContext;

            this.SeedDataForMessagesTesting(context);
            User testUser = await context.Users.FirstAsync();

            Message message = await context.Messages.Skip(1).FirstAsync();

            int previousCount = await context.Messages.CountAsync();

            var response = await client.DeleteAsync($"/api/messages/{message.MessageId}");

            response.EnsureSuccessStatusCode();

            int currentCount = await context.Messages.CountAsync();

            Assert.NotEqual(previousCount, currentCount);
            Assert.Equal(previousCount, currentCount + 1);
            Assert.False(await context.Messages.AnyAsync(x => x.MessageId == message.MessageId));
        }
        public async Task GetByConversation_ShouldSuccess()
        {
            var client = this.factory.CreateClient();

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                "Bearer",
                await AuthenticationUtilities.GetTestUserAccessTokenAsync(client));

            MessengerContext context = this.factory.Server.Host.Services.GetService(typeof(MessengerContext))
                                       as MessengerContext;

            this.SeedDataForMessagesTesting(context);
            User testUser = await context.Users.FirstAsync();

            Conversation testConversation = await context.Conversations.FirstAsync();

            var response = await client.GetAsync($"/api/messages/Conversation/{testConversation.ConversationId}");

            response.EnsureSuccessStatusCode();

            var messages = response.Content.ReadAsAsync(typeof(IEnumerable <EditMessageViewModel>)).Result as IEnumerable <EditMessageViewModel>;

            int messagesCount = await context.Messages.CountAsync(x => x.OwnerId == testUser.Id &&
                                                                  x.Conversation.ConversationId == testConversation.ConversationId);

            Assert.NotNull(messages);
            Assert.Equal(messagesCount, messages.Count());
        }
Ejemplo n.º 4
0
        public async Task GetUserNameContains_ShouldSuccess()
        {
            var client = this.factory.CreateClient();

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                "Bearer",
                await AuthenticationUtilities.GetTestUserAccessTokenAsync(client));

            MessengerContext context = this.factory.Server.Host.Services.GetService(typeof(MessengerContext))
                                       as MessengerContext;

            UserManager <User> userManager = this.factory.Server.Host.Services.GetService(typeof(UserManager <User>))
                                             as UserManager <User>;

            foreach (var item in this.GenerateUsers(5))
            {
                await userManager.CreateAsync(item);
            }

            var dbUser = await context.Users.Skip(2).FirstAsync();

            var searchString = dbUser.UserName.Substring(0, 2);

            var response = await client.GetAsync($"/api/users/UserNameContains/{searchString}");

            response.EnsureSuccessStatusCode();

            var users = response.Content.ReadAsAsync(typeof(IEnumerable <UserViewModel>)).Result as IEnumerable <UserViewModel>;

            Assert.True(users.Any(x => x.UserName == dbUser.UserName));
        }
        public async Task PutUpdate_ShouldSuccess()
        {
            var client = this.factory.CreateClient();

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                "Bearer",
                await AuthenticationUtilities.GetTestUserAccessTokenAsync(client));

            MessengerContext context = this.factory.Server.Host.Services.GetService(typeof(MessengerContext))
                                       as MessengerContext;

            this.SeedDataForMessagesTesting(context);
            User testUser = await context.Users.FirstAsync();

            Message message = await context.Messages.Skip(1).FirstAsync();

            string newContent = "new content";

            var requestBody = new EditMessageViewModel {
                MessageId = message.MessageId, Content = newContent
            };
            var requestContent = new StringContent(JsonConvert.SerializeObject(requestBody), Encoding.UTF8, "application/json");
            var response       = await client.PutAsync($"/api/messages/{message.MessageId}", requestContent);

            response.EnsureSuccessStatusCode();

            Message updated = await context.Messages.SingleAsync(x => x.MessageId == message.MessageId);

            Assert.Equal(newContent, updated.Content);
        }
Ejemplo n.º 6
0
        public async Task GetById_ShouldSuccess()
        {
            var client = this.factory.CreateClient();

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                "Bearer",
                await AuthenticationUtilities.GetTestUserAccessTokenAsync(client));

            MessengerContext context = this.factory.Server.Host.Services.GetService(typeof(MessengerContext))
                                       as MessengerContext;

            await context.Users.AddRangeAsync(this.GenerateUsers(5));

            await context.SaveChangesAsync();

            var dbUser = await context.Users.Skip(2).FirstAsync();

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

            response.EnsureSuccessStatusCode();

            var user = response.Content.ReadAsAsync(typeof(UserViewModel)).Result as UserViewModel;

            Assert.NotNull(user);
            Assert.Equal(dbUser.UserName, user.UserName);
        }
Ejemplo n.º 7
0
        public async Task Delete_ShouldSuccess()
        {
            var client = this.factory.CreateClient();

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                "Bearer",
                await AuthenticationUtilities.GetTestUserAccessTokenAsync(client));

            MessengerContext context = this.factory.Server.Host.Services.GetService(typeof(MessengerContext))
                                       as MessengerContext;

            await context.Users.AddRangeAsync(this.GenerateUsers(5));

            await context.SaveChangesAsync();

            var dbUser = await context.Users.Skip(2).FirstAsync();

            int previousCount = await context.Users.CountAsync();

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

            response.EnsureSuccessStatusCode();

            int currentCount = await context.Users.CountAsync();

            Assert.NotEqual(previousCount, currentCount);
            Assert.Equal(previousCount, currentCount + 1);
            Assert.False(await context.Users.AnyAsync(x => x.Id == dbUser.Id));
        }
Ejemplo n.º 8
0
        public async Task Post_ShouldSuccess()
        {
            var client = this.factory.CreateClient();

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                "Bearer",
                await AuthenticationUtilities.GetTestUserAccessTokenAsync(client));

            MessengerContext context = this.factory.Server.Host.Services.GetService(typeof(MessengerContext))
                                       as MessengerContext;

            User user = this.GenerateUsers(1).Single();

            var requestBody = new UserCredentialsViewModel {
                Password = "******", UserName = user.UserName, Name = user.Name
            };
            var requestContent = new StringContent(JsonConvert.SerializeObject(requestBody), Encoding.UTF8, "application/json");
            var response       = await client.PostAsync($"/api/users", requestContent);

            response.EnsureSuccessStatusCode();

            UserManager <User> userManager = this.factory.Server.Host.Services.GetService(typeof(UserManager <User>))
                                             as UserManager <User>;

            var insertedUser = await userManager.FindByNameAsync(user.UserName);

            Assert.NotNull(insertedUser);
        }
Ejemplo n.º 9
0
        public async Task PutUpdate_ShouldSuccess()
        {
            var client = this.factory.CreateClient();

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                "Bearer",
                await AuthenticationUtilities.GetTestUserAccessTokenAsync(client));

            MessengerContext context = this.factory.Server.Host.Services.GetService(typeof(MessengerContext))
                                       as MessengerContext;

            UserManager <User> userManager = this.factory.Server.Host.Services.GetService(typeof(UserManager <User>))
                                             as UserManager <User>;

            foreach (var item in this.GenerateUsers(5))
            {
                await userManager.CreateAsync(item);
            }

            var dbUser = await context.Users.Skip(2).FirstAsync();

            string newName = "New Name";

            var requestBody = new UserViewModel {
                Id = dbUser.Id, UserName = dbUser.UserName, Name = newName
            };
            var requestContent = new StringContent(JsonConvert.SerializeObject(requestBody), Encoding.UTF8, "application/json");
            var response       = await client.PutAsync($"/api/users/{dbUser.Id}", requestContent);

            response.EnsureSuccessStatusCode();

            userManager = this.factory.Server.Host.Services.GetService(typeof(UserManager <User>))
                          as UserManager <User>;

            var updatedUser = await userManager.FindByIdAsync(dbUser.Id);

            Assert.Equal(newName, updatedUser.Name);
        }
Ejemplo n.º 10
0
        public async Task Get_ShouldSuccess()
        {
            var client = this.factory.CreateClient();

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                "Bearer",
                await AuthenticationUtilities.GetTestUserAccessTokenAsync(client));

            MessengerContext context = this.factory.Server.Host.Services.GetService(typeof(MessengerContext))
                                       as MessengerContext;

            await context.Users.AddRangeAsync(this.GenerateUsers(5));

            var response = await client.GetAsync("/api/users");

            response.EnsureSuccessStatusCode();

            var users = response.Content.ReadAsAsync(typeof(IEnumerable <UserViewModel>)).Result as IEnumerable <UserViewModel>;

            int usersCount = await context.Users.CountAsync();

            Assert.NotNull(users);
            Assert.Equal(usersCount, users.Count());
        }