public void GetApiToken_WrongPassword_ReturnedTokenWillBeNull()
        {
            using (var fixture = new FixtureInit("http://localhost"))
            {
                // arrange
                var userRepository = new UsersRepository(fixture.Setup.Context);
                var hashService = new HashService();

                var service = new ApiService(userRepository, hashService);

                var email = "*****@*****.**";
                var password = "******";
                var passwordHash = hashService.CreateMD5Hash(password);
                var apiToken = hashService.CreateApiToken(email, password);

                userRepository.Save(
                    new User
                    {
                        Email = email,
                        PasswordHash = passwordHash,
                        ApiToken = apiToken,
                        Temp = false
                    }
                );

                // act
                var result = service.GetApiToken(email, password + "xx");

                // assert
                Assert.That(result, Is.Null);
            }
        }
        public void InsertUserTwice()
        {
            using (var fixture = new FixtureInit("http://localhost"))
            {
                //INIT
                var register = new UsersRepository(fixture.Setup.Context);

                //ACT / POST
                var user = new User()
                {
                    Email = "email",
                    //SecretPhrase = "sec",
                    //Password = "******"
                };

                register.Save(user);

                var newUser = new User()
                {
                    Email = "email",
                    //SecretPhrase = "sec",
                    //Password = "******"
                };

                register.Save(newUser);
            }
        }
        public void Save_UpdatePost()
        {
            using (var fixture = new FixtureInit("http://localhost"))
            {
                //arrange
                var repository = new BlogPostsRepository(fixture.Setup.Context);
                var post = new BlogPost
                {
                    Url = "Some-new-post",
                    Title = "Some new post",
                    Body = "<p>This is new post in blog</p>",
                    CreatedBy = "AlexanderB",
                    CreatedDate = DateTime.Now
                };

                repository.Save(post);

                var blogPost = repository.BlogPosts.Where(p => p.Url == post.Url).Single();
                
                // act
                blogPost.Title += "(updated)";
                repository.Save(blogPost);

                //assert
                var updatedPost = repository.BlogPosts.Where(p => p.Url == post.Url).Single();
                Assert.That(updatedPost.Title, Is.EqualTo("Some new post(updated)"));
            }
        }
        public void UpdateTask()
        {
            using (var fixture = new FixtureInit("http://localhost"))
            {
                //INIT
                var repository = new TasksRepository(fixture.Setup.Context);

                var task = new Task()
                {
                    Number = 0,
                    UserId = fixture.Setup.User.Id,
                    Description = "My new task",
                    Status = 0,
                    ActualWork = 0
                };

                repository.Save(task);

                //ACT
                task.Description = "My new task (update)";
                repository.Save(task);

                //POST
                var foundTask = repository.Tasks.WithId(task.Id);
                Assert.That(foundTask, Is.Not.Null);
                Assert.That(foundTask.Description, Is.EqualTo("My new task (update)"));
            }

        }
        public void Smoke()
        {
            using (var fixture = new FixtureInit("http://localhost"))
            {
                // arrange
                var userRepository = new UsersRepository(fixture.Setup.Context);
                var hashService = new HashService();

                // act / post
                var service = new ApiService(userRepository, hashService);
                Assert.That(service, Is.Not.Null);
            }
        }
        public void NotifyUserOnRegistration_Integration_EmailSent()
        {
            using (var fixture = new FixtureInit("http://localhost"))
            {
                // assert
                var credentialsRepository = new CredentialsRepository(fixture.Setup.Context);
                var credentialService = new CredentialsService(credentialsRepository);
                var emailService = new EmailService(credentialService);
                var notificationService = new NotificationService(emailService);

                // act / assert
                notificationService.NotifyUserOnRegistration("*****@*****.**", "password");
            }
        }
        public void Credentials_GetCredentials()
        {
            using (var fixture = new FixtureInit("http://localhost"))
            {
                // arrange 
                var repository = new CredentialsRepository(fixture.Setup.Context);

                // act
                var credentials = repository.Credentials;

                // assert
                Assert.That(credentials, Is.Not.Null);
                Assert.That(credentials.Count(), Is.EqualTo(0), "No credentials set in initial database.");
            }
        }
        public void SaveBlogPost()
        {
            using (var fixture = new FixtureInit("http://localhost"))
            {
                //arrange
                var repository = new BlogPostsRepository(fixture.Setup.Context);
                var post = new BlogPost
                {
                    Url = "Some-new-post",
                    Title = "Some new post",
                    Body = "<p>This is new post in blog</p>",
                    CreatedBy = "AlexanderB",
                    CreatedDate = DateTime.Now
                };

                //act
                repository.Save(post);

                //assert
                Assert.That(post.Id, Is.GreaterThan(0));
            }
        }
        public void InsertUser()
        {
            using (var fixture = new FixtureInit("http://localhost"))
            {
                //INIT
                var register = new UsersRepository(fixture.Setup.Context);

                //ACT
                var user = new User()
                {
                    Email = "email",
                    //SecretPhrase = "sec",
                    //Password = "******"
                };

                register.Save(user);

                //POST
                var actual = register.Users.WithEmail("email");
                Assert.That(actual, Is.Not.Null);
            }
        }
        public void DeletePost()
        {
            using (var fixture = new FixtureInit("http://localhost"))
            {
                //arrange
                var repository = new BlogPostsRepository(fixture.Setup.Context);
                var post = new BlogPost
                {
                    Url = "Some-new-post",
                    Title = "Some new post",
                    Body = "<p>This is new post in blog</p>",
                    CreatedBy = "AlexanderB",
                    CreatedDate = DateTime.Now
                };

                repository.Save(post);

                //act
                repository.Delete(post);

                //assert
                var foundTask = repository.BlogPosts.WithId(post.Id);
                Assert.That(foundTask, Is.Null);
            }
        }
        public void Save_CreatedDateInitialized_TaskSavedWithCreatedDate()
        {
            using (var fixture = new FixtureInit("http://localhost"))
            {
                // arrange
                var repository = new TasksRepository(fixture.Setup.Context);

                // act
                var task = new Task { UserId = fixture.Setup.User.Id, Description = "created date test" };
                repository.Save(task);

                // assert
                var found = repository.Tasks.Where(t => t.Description == "created date test").SingleOrDefault();
                Assert.That(found.CreatedDate, Is.Not.Null);
            }
        }
        public void BlogPostsSortedByDate()
        {
            using (var fixture = new FixtureInit("http://localhost"))
            {
                //arrange
                var repository = new BlogPostsRepository(fixture.Setup.Context);

                SubmitTenBlogpostsToRepository(repository);

                //act
                var posts = repository.BlogPosts.ToArray();

                //post
                Assert.That(posts, Is.Not.Null);
                Assert.That(posts[0].CreatedDate, Is.GreaterThan(posts[1].CreatedDate));
                Assert.That(posts[1].CreatedDate, Is.GreaterThan(posts[2].CreatedDate));
                Assert.That(posts[2].CreatedDate, Is.GreaterThan(posts[3].CreatedDate));
            }
        }
        public void PagingGetNonExistingPage()
        {
            using (var fixture = new FixtureInit("http://localhost"))
            {
                //arrange
                var repository = new BlogPostsRepository(fixture.Setup.Context);

                SubmitTenBlogpostsToRepository(repository);

                //act
                var page = repository.BlogPosts.Page(3, 5);

                //assert
                Assert.That(page, Is.Not.Null);
                Assert.That(page.Count(), Is.EqualTo(0));
            }
        }
        public void Tasks_DoneFlag_Set()
        {
            using (var fixture = new FixtureInit("http://localhost"))
            {
                // arrange
                var repository = new TasksRepository(fixture.Setup.Context);

                var task = new Task
                {
                    Description = "Test task",
                    UserId = fixture.Setup.User.Id,
                    Done = true
                };

                // act
                repository.Save(task);

                // assert
                var savedTask = repository.Tasks.Where(t => t.Id == task.Id).SingleOrDefault();
                Assert.That(savedTask.Done, Is.True);
            }

        }
        public void Tasks_SortedByPosition()
        {
            using (var fixture = new FixtureInit("http://localhost"))
            {
                // arrange
                var repository = new TasksRepository(fixture.Setup.Context);

                repository.Save(new Task { UserId = fixture.Setup.User.Id, Position = 3 });
                repository.Save(new Task { UserId = fixture.Setup.User.Id, Position = 2 });
                repository.Save(new Task { UserId = fixture.Setup.User.Id, Position = 1 });

                // act
                var tasks = repository.Tasks.ToArray();

                // assert
                Assert.That(tasks[0].Position, Is.EqualTo(1));
                Assert.That(tasks[1].Position, Is.EqualTo(2));
                Assert.That(tasks[2].Position, Is.EqualTo(3));
            }
        }
        public void WithTempExtension()
        {
            using (var fixture = new FixtureInit("http://localhost"))
            {
                //arrange
                var register = new UsersRepository(fixture.Setup.Context);
                SubmitUsersToRepository(register, 5, 1);

                //act
                var usersCount = register.Users.WithTemp(false).Count();
                var tempCount = register.Users.WithTemp(true).Count();

                //post
                Assert.That(usersCount, Is.EqualTo(6)); 
                Assert.That(tempCount, Is.EqualTo(1));
            }
        }
        public void GetBlogPostByUrl()
        {
            using (var fixture = new FixtureInit("http://localhost"))
            {
                //arrange
                var repository = new BlogPostsRepository(fixture.Setup.Context);

                SubmitTenBlogpostsToRepository(repository);

                //act
                var post = repository.BlogPosts.WithUrl("Url-0");

                //assert
                Assert.That(post, Is.Not.Null);
                Assert.That(post.Url, Is.EqualTo("Url-0"));
            }
        }
        public void SubmitBlogPostWithSameUrl()
        {
            using (var fixture = new FixtureInit("http://localhost"))
            {
                //arrange
                var repository = new BlogPostsRepository(fixture.Setup.Context);

                //act / post
                repository.Save(new BlogPost { Url = "1", Title = "1", Body = "b", CreatedDate = DateTime.Now, CreatedBy = "c"});
                repository.Save(new BlogPost { Url = "1", Title = "1", Body = "b", CreatedDate = DateTime.Now, CreatedBy = "c"});
            }

        }
        public void FindUserById()
        {
            using (var fixture = new FixtureInit("http://localhost"))
            {
                //INIT
                var register = new UsersRepository(fixture.Setup.Context);

                var user = new User()
                {
                    Email = "email",
                    //SecretPhrase = "sec",
                    //Password = "******"
                };

                register.Save(user);

                //ACT
                var foundUser = register.Users.WithId(user.Id);

                //POST
                Assert.That(foundUser, Is.Not.Null);
                Assert.That(foundUser.Id, Is.EqualTo(user.Id));
            }
        }
        public void GetAllTasks()
        {
            using (var fixture = new FixtureInit("http://localhost"))
            {
                //INIT
                var repository = new TasksRepository(fixture.Setup.Context);

                var tasks = new[] { 
                    new Task() { UserId = fixture.Setup.User.Id, Description="test1" } , 
                    new Task() { UserId = fixture.Setup.User.Id, Description="test2" }
                };

                foreach (var task in tasks)
                {
                    repository.Save(task);
                }

                //ACT
                var foundTasks = repository.Tasks;

                //POST
                Assert.That(foundTasks, Is.Not.Null);
                Assert.That(foundTasks.Count(), Is.EqualTo(2));
            }
        }