Exemplo n.º 1
0
        public async Task Update_Note_Successfully()
        {
            var noteId = Guid.NewGuid();

            var options = DbContextHelper.GetTestInMemoryDatabase(nameof(Update_Note_Successfully));

            using (var context = new NoteTakerContext(options, httpContextAccessor.Object))
            {
                context.Add(new Note
                {
                    Id      = noteId,
                    Title   = "Apples",
                    Content = "Oranges",
                    User    = user
                });

                context.SaveChanges();
            };

            using (var context = new NoteTakerContext(options, httpContextAccessor.Object))
            {
                var service = new NotesService(context, httpContextAccessor.Object);

                var updatedNote = await service.UpdateNoteAsync(noteId.ToString(), "New Title", "New Content");

                updatedNote.Should().NotBeNull();
                updatedNote.Title.Should().BeEquivalentTo("New Title");
                updatedNote.Content.Should().BeEquivalentTo("New Content");
            };
        }
        private static void Seed(NoteTakerContext context)
        {
            var note1 = new Note
            {
                Author = new Person
                {
                    Id           = Guid.NewGuid(),
                    EmailAddress = "*****@*****.**",
                    FirstName    = "Brock",
                    MiddleName   = "Brockington",
                    LastName     = "Brockwell",
                    Bio          = "I like sports"
                },
                Text = "This is a note about how much I like sports."
            };
            var note2 = new Note
            {
                Author = new Person
                {
                    Id           = Guid.NewGuid(),
                    EmailAddress = "*****@*****.**",
                    FirstName    = "Guts",
                    MiddleName   = "McGee",
                    LastName     = "Willy",
                    Bio          = "I'll cut you into peices."
                },
                Text = "This is a note about my biker gang."
            };

            context.Note.AddOrUpdate(note1);
            context.Note.AddOrUpdate(note2);
            context.SaveChanges();
        }
Exemplo n.º 3
0
        public async Task Return_Token_After_Authenticated_User_Successfully()
        {
            var options = DbContextHelper.GetTestInMemoryDatabase(nameof(Return_Token_After_Authenticated_User_Successfully));

            var password     = "******";
            var passwordHash = Guid.NewGuid().ToString();

            using (var context = new NoteTakerContext(options, httpContextAccessor.Object))
            {
                context.Users.Add(new User("JohnSmith", "John", "Smith", passwordHash));
                context.SaveChanges();
            }

            using (var context = new NoteTakerContext(options, httpContextAccessor.Object))
            {
                passwordHashingService.Setup(x => x.VerifyPassword(It.Is <string>(y => y.Equals(password)),
                                                                   It.Is <string>(y => y.Equals(passwordHash)))).Returns(true).Verifiable();

                tokenService.Setup(x => x.GetToken(It.IsAny <string>(), It.IsAny <string>())).Returns(Guid.NewGuid().ToString).Verifiable();

                var usersService = new UsersService(context, logger, passwordHashingService.Object, tokenService.Object);

                var authenticatedUser = await usersService.AuthenticateAsync("JohnSmith", password);

                authenticatedUser.Should().NotBeNull();
                authenticatedUser.Token.Should().NotBeNull();

                passwordHashingService.Verify();
                tokenService.Verify();
            }
        }
Exemplo n.º 4
0
        public async Task Delete_Note_Successfully()
        {
            var options = DbContextHelper.GetTestInMemoryDatabase(nameof(Delete_Note_Successfully));

            var noteToDelete = Guid.NewGuid();

            using (var context = new NoteTakerContext(options, httpContextAccessor.Object))
            {
                context.Notes.Add(new Note
                {
                    Id       = noteToDelete,
                    Title    = "Apples",
                    Content  = "Oranges",
                    Created  = DateTime.Now,
                    Modified = DateTime.Now.AddDays(-7),
                    User     = user
                });

                context.SaveChanges();
            };

            using (var context = new NoteTakerContext(options, httpContextAccessor.Object))
            {
                var service = new NotesService(context, httpContextAccessor.Object);

                await service.DeleteNoteAsync(noteToDelete.ToString());

                context.Notes.FirstOrDefault(x => x.Id == noteToDelete).Should().BeNull();
            };
        }
Exemplo n.º 5
0
        private static void InitialiseUsersControllerTestData(NoteTakerContext context)
        {
            // Password is Apples
            var user = new User("JohnSmith", "John", "Smith", "$2b$10$fMcZJqEdLYUkkV0RcOJR0eo.00oiYqpfXD1TH18Q8XEVvO4GVSiKe");

            context.Users.Add(user);

            context.SaveChanges();
        }
Exemplo n.º 6
0
        public async Task Get_Notes_Default_Ordering_To_ModifiedDesc_Successfully()
        {
            var options = DbContextHelper.GetTestInMemoryDatabase(nameof(Get_Notes_Default_Ordering_To_ModifiedDesc_Successfully));

            using (var context = new NoteTakerContext(options, httpContextAccessor.Object))
            {
                context.Notes.AddRange(new List <Note>
                {
                    new Note
                    {
                        Id       = Guid.NewGuid(),
                        Title    = "Apples",
                        Content  = "Oranges",
                        Created  = DateTime.Now,
                        Modified = DateTime.Now.AddDays(-7),
                        User     = user
                    },
                    new Note
                    {
                        Id       = Guid.NewGuid(),
                        Title    = "Pineapples",
                        Content  = "Bananas",
                        Created  = DateTime.Now,
                        Modified = DateTime.Now.AddDays(-3),
                        User     = user
                    },
                    new Note
                    {
                        Id       = Guid.NewGuid(),
                        Title    = "Avocado",
                        Content  = "Broccoli",
                        Created  = DateTime.Now,
                        Modified = DateTime.Now.AddDays(-10),
                        User     = user
                    },
                });

                context.SaveChanges();
            };

            using (var context = new NoteTakerContext(options, httpContextAccessor.Object))
            {
                var service = new NotesService(context, httpContextAccessor.Object);

                var notes = await service.GetNotesAsync(string.Empty, null);

                notes[0].Title.Should().BeEquivalentTo("Pineapples");
                notes[1].Title.Should().BeEquivalentTo("Apples");
                notes[2].Title.Should().BeEquivalentTo("Avocado");
            };
        }
Exemplo n.º 7
0
        private static void InitialiseNotesControllerTestData(NoteTakerContext context)
        {
            var user = new User(new Guid(TestJwtTokenHelper.UserId));

            context.Users.Add(user);

            var notes = GetNotes(50, user).ToList();

            notes[0].Title = "Apples";
            notes[1].Id    = new Guid("11111111-1234-4133-8c69-40ca0509be6a");
            notes[2].Id    = new Guid("22222222-4321-1234-4321-40ca0509be6a");
            notes[3].Id    = new Guid("33333333-4321-1234-4321-40ca0509be6a");
            notes[4].Id    = new Guid("44444444-4321-1234-4321-40ca0509be6a");

            context.Notes.AddRange(notes);

            context.SaveChanges();
        }
Exemplo n.º 8
0
        public async Task Ensure_Unique_Username()
        {
            var options = DbContextHelper.GetTestInMemoryDatabase(nameof(Ensure_Unique_Username));

            using (var context = new NoteTakerContext(options, httpContextAccessor.Object))
            {
                var user = new User
                {
                    Id           = Guid.NewGuid(),
                    Username     = "******",
                    FirstName    = "John",
                    LastName     = "Smith",
                    InternalId   = 1,
                    Created      = DateTime.Now,
                    Modified     = DateTime.Now,
                    PasswordHash = Guid.NewGuid().ToString()
                };

                context.Users.Add(user);
                context.SaveChanges();
            }

            using (var context = new NoteTakerContext(options, httpContextAccessor.Object))
            {
                var createUser = new CreateUser()
                {
                    Username  = "******",
                    FirstName = "John",
                    LastName  = "Smith",
                    Password  = "******"
                };

                var usersService = new UsersService(context, logger, passwordHashingService.Object, tokenService.Object);

                var createdUser = await usersService.CreateUserAsync(createUser);

                createdUser.Should().BeNull();
            }
        }
Exemplo n.º 9
0
        public async Task Return_Null_After_Failed_Authentication()
        {
            var options = DbContextHelper.GetTestInMemoryDatabase(nameof(Return_Null_After_Failed_Authentication));

            using (var context = new NoteTakerContext(options, httpContextAccessor.Object))
            {
                context.Users.Add(new User("JohnSmith", "John", "Smith", Guid.NewGuid().ToString()));
                context.SaveChanges();
            }

            using (var context = new NoteTakerContext(options, httpContextAccessor.Object))
            {
                passwordHashingService.Setup(x => x.VerifyPassword(It.IsAny <string>(), It.IsAny <string>())).Returns(false).Verifiable();

                var usersService = new UsersService(context, logger, passwordHashingService.Object, tokenService.Object);

                var authenticatedUser = await usersService.AuthenticateAsync("JohnSmith", "Apples");

                authenticatedUser.Should().BeNull();

                passwordHashingService.Verify();
            }
        }