Beispiel #1
0
        public async Task GetSingularPostWithID()
        {
            HttpClient client = _testServer.CreateClient(1);

            Post        post;
            WikiArticle wikiArticle;
            PostType    postType;

            using (WediumContext db = new WediumContext(_wediumContextOptions))
            {
                post        = db.Post.Where(p => p.PostId == 1).Include(p => p.WikiArticle).Include(p => p.PostType).First <Post>();
                wikiArticle = post.WikiArticle;
                postType    = post.PostType;
            }

            HttpResponseMessage response = await client.GetAsync(_apiEndpoint + $"api/Post/get/{post.PostId}");

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

            PostDto postDto = await response.Content.ReadAsAsync <PostDto>();

            Assert.AreEqual(post.Title, postDto.Title);
            Assert.AreEqual(wikiArticle.ArticleTitle, postDto.ArticleTitle);
            Assert.AreEqual(postType.PostTypeValue, postDto.PostType);
            Assert.AreEqual(wikiArticle.Url, postDto.ArticleUrl);
            Assert.AreEqual(wikiArticle.ArticleImageUrl, postDto.ArticleImageUrl);
            Assert.AreEqual(wikiArticle.ArticleBody, postDto.ArticleBody);
        }
Beispiel #2
0
        public void JoinTest()
        {
            User testUser = new User
            {
                FirstName = "Test",
                LastName  = "User",
                Email     = "*****@*****.**"
            };

            WikiArticle testArticle = new WikiArticle
            {
                Url          = "http://test",
                ArticleDate  = DateTime.Now,
                ArticleBody  = "Test Body",
                ArticleTitle = "Test Title"
            };

            using (WediumContext db = new WediumContext(_wediumContextOptions))
            {
                var postType = db.PostType.First();

                Post testPost = new Post
                {
                    Date        = DateTime.Now,
                    Title       = "Urzababa The Great",
                    Description = "The Life of Urzababa",
                    PostTypeId  = postType.PostTypeId
                };

                Favourite testFavourite = new Favourite
                {
                    Date = DateTime.Now
                };

                db.User.Add(testUser);
                db.WikiArticle.Add(testArticle);
                db.SaveChanges();

                WikiArticle wikiArticle = db.WikiArticle.First(wa => wa.ArticleDate == testArticle.ArticleDate);
                testPost.WikiArticleId = wikiArticle.WikiArticleId;

                User user = db.User.First(u => u.Email == testUser.Email);
                testPost.UserId = user.UserId;

                db.Post.Add(testPost);
                db.SaveChanges();

                Post post = db.Post.First(p => p.Title == testPost.Title);
                testFavourite.PostId = post.PostId;
                testFavourite.UserId = user.UserId;

                db.Favourite.Add(testFavourite);
                db.SaveChanges();

                Favourite favourite = db.Favourite.First(f => f.Date == testFavourite.Date);
                Assert.AreEqual(testUser.UserId, favourite.UserId);
                Assert.AreEqual(testPost.PostId, favourite.PostId);
            }
        }
Beispiel #3
0
        public PostService(WediumContext wediumContext, IWikiMediaApiService wikiMediaApiService, IOptions <Options> options)
        {
            _db = wediumContext;
            _wikiMediaApiService = wikiMediaApiService;
            _options             = options.Value;

            Settings GetDefaultThumbnailSettings = _db.Settings
                                                   .First(s => s.Key == "WIKIARTICLE_DEFAULT_THUMBNAIL");

            WIKIARTICLE_DEFAULT_THUMBNAIL = GetDefaultThumbnailSettings.Value;
        }
Beispiel #4
0
        public static void InitializeDB(DbContextOptions <WediumContext> options)
        {
            using (WediumContext db = new WediumContext(options))
            {
                foreach (Settings setting in InitialSettings())
                {
                    db.Settings.Add(setting);
                }

                foreach (PostType postType in InitialPostTypes())
                {
                    db.PostType.Add(postType);
                }

                foreach (User user in InitialUsers())
                {
                    db.User.Add(user);
                }

                foreach (WikiArticle wikiArticle in InitialWikiArticles())
                {
                    db.WikiArticle.Add(wikiArticle);
                }

                foreach (Post post in InitializePosts())
                {
                    db.Post.Add(post);
                }

                foreach (Favourite favourite in InitializeFavourites())
                {
                    db.Favourite.Add(favourite);
                }

                foreach (PostLike postLike in InitializePostLikes())
                {
                    db.PostLike.Add(postLike);
                }

                foreach (CommentType commentType in InitializeCommentTypes())
                {
                    db.CommentType.Add(commentType);
                }

                foreach (Comment comment in InitializeComments())
                {
                    db.Comment.Add(comment);
                }

                db.SaveChanges();
            }
        }
        public WikiMediaApiService(WediumContext wediumContext)
        {
            _db = wediumContext;

            Settings GetContentSettings = _db.Settings
                                          .First(s => s.Key == "WIKIMEDIA_GET_CONTENT_ENDPOINT");

            Settings GetThumbnailSettings = _db.Settings
                                            .First(s => s.Key == "WIKIMEDIA_GET_THUMBNAIL_ENDPOINT");

            Settings GetLatestDateSettings = _db.Settings
                                             .First(s => s.Key == "WIKIMEDIA_GET_LATEST_DATE_ENDPOINT");

            WIKIMEDIA_GET_CONTENT_ENDPOINT     = GetContentSettings.Value;
            WIKIMEDIA_GET_THUMBNAIL_ENDPOINT   = GetThumbnailSettings.Value;
            WIKIMEDIA_GET_LATEST_DATE_ENDPOINT = GetLatestDateSettings.Value;
        }
Beispiel #6
0
        public async Task GetCreatedPostInvalidPostId()
        {
            HttpClient client = _testServer.CreateClient(2);

            int count = 0;

            using (WediumContext db = new WediumContext(_wediumContextOptions))
            {
                count = db.Post.Count(p => p.UserId == 2);
            }

            HttpResponseMessage response = await client.GetAsync(_apiEndpoint + "api/Post/GetCreated?");

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

            List <PostDto> content = await response.Content.ReadAsAsync <List <PostDto> >();

            Assert.AreEqual(count, content.Count);
        }
Beispiel #7
0
        public async Task DeletePostUnauthorized()
        {
            Post post;

            using (WediumContext db = new WediumContext(_wediumContextOptions))
            {
                post = db.Post.First();
            }

            HttpClient          client   = _testServer.CreateClient();
            HttpResponseMessage response = await client.DeleteAsync(_apiEndpoint + "api/Post/Delete/1");

            Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode);

            _testServer.AuthenticateClient(client, 1);
            HttpResponseMessage successfulResponse = await client.DeleteAsync(_apiEndpoint + "api/Post/Delete/1");

            Assert.AreEqual(HttpStatusCode.OK, successfulResponse.StatusCode);
        }
Beispiel #8
0
        public async Task DeletePostSuccessfully()
        {
            Post post;

            using (WediumContext db = new WediumContext(_wediumContextOptions))
            {
                post = db.Post.First();
            }

            HttpClient client = _testServer.CreateClient(post.UserId);

            HttpResponseMessage response = await client.DeleteAsync(_apiEndpoint + "api/Post/Delete/1");

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

            HttpResponseMessage getResponse = await client.GetAsync(_apiEndpoint + $"api/Post/get/1");

            Assert.AreEqual(HttpStatusCode.NotFound, getResponse.StatusCode);
        }
Beispiel #9
0
        public void PersistanceTest()
        {
            // Adding a User to the DB
            User testUser = new User
            {
                FirstName = "Test",
                LastName  = "User",
                Email     = "*****@*****.**"
            };

            using (WediumContext db = new WediumContext(_wediumContextOptions))
            {
                try
                {
                    db.User.Add(testUser);
                    db.SaveChanges();

                    var user = db.User.FirstOrDefault(u => u.FirstName == "Test");
                    Assert.AreEqual(testUser.FirstName, user.FirstName);
                    Assert.AreEqual(testUser.LastName, user.LastName);
                    Assert.AreEqual(testUser.Email, user.Email);

                    user.FirstName = "Updated Firstname";
                    user.LastName  = "Updated Lastname";

                    db.SaveChanges();

                    Assert.AreEqual("Updated Firstname", user.FirstName);
                    Assert.AreEqual("Updated Lastname", user.LastName);
                }
                finally
                {
                    // Deleting the User from the database
                    db.User.Remove(testUser);
                    db.SaveChanges();

                    var removedUser = db.User.FirstOrDefault(u => u.FirstName == "Test user");
                    Assert.AreEqual(null, removedUser);
                }
            }
        }
Beispiel #10
0
        public async Task GetCommentsForPostWithComment()
        {
            HttpClient client = _testServer.CreateClient(139);

            Comment comment;

            using (WediumContext db = new WediumContext(_wediumContextOptions))
            {
                comment = db.Comment.First();
            }

            // Test post with single comment
            HttpResponseMessage response = await client.GetAsync(_apiEndpoint + $"api/Comment/Get/{comment.PostId}");

            IEnumerable <CommentDto> responseComments = await response.Content.ReadAsAsync <IEnumerable <CommentDto> >();

            Assert.NotNull(responseComments);
            Assert.AreEqual(1, responseComments.Count());
            Assert.AreEqual(comment.PostId, responseComments.First().PostId);
            Assert.AreEqual(comment.CommentId, responseComments.First().CommentId);
            Assert.AreEqual(comment.UserId, responseComments.First().UserId);
            Assert.AreEqual(comment.Body, responseComments.First().Body);
            Assert.AreEqual(comment.ParentCommentId, responseComments.First().ParentCommentId);
        }
Beispiel #11
0
 public PostLikeService(WediumContext wediumContext, IPostService postService, IOptions <Options> options)
 {
     _db          = wediumContext;
     _postService = postService;
     _options     = options.Value;
 }
Beispiel #12
0
 public CommentService(WediumContext database)
 {
     _db = database;
 }
Beispiel #13
0
 public PostTypeService(WediumContext wediumContext)
 {
     _db = wediumContext;
 }
Beispiel #14
0
 public UserService(WediumContext wediumContext)
 {
     _wediumContext = wediumContext;
 }
 public CommentLikeService(WediumContext wediumContext, ICommentService commentService)
 {
     _db             = wediumContext;
     _commentService = commentService;
 }