Пример #1
0
        public async void GetArticleInvalidIdTest()
        {
            var controller = new ArticlesController(_context, _userManager);
            var result     = await controller.GetArticle(40);

            Assert.IsType <NotFoundResult>(result);

            var result2 = await controller.GetArticle(-1);

            Assert.IsType <BadRequestResult>(result2);
        }
Пример #2
0
        public async void PutImageTest()
        {
            var controller = new ArticlesController(_context, _userManager);

            var article = _articleDTOs.First(a => a.IsHighlighted == true);

            ImageDTO newImage = new ImageDTO()
            {
                Name   = "New Image",
                Base64 = article.Images.First().Base64
            }
            ;

            //POST TEST (Highlight)
            var result = await controller.PutImage(article.Id, newImage);

            var objectResult = Assert.IsType <OkObjectResult>(result);
            var model        = Assert.IsAssignableFrom <ImageDTO>(objectResult.Value);

            newImage.Id = model.Id;
            Assert.Equal(newImage, model);


            //GET TEST (Image added to Article)
            article.Images.Add(newImage);
            var result2 = await controller.GetArticle(article.Id);

            var objectResult2 = Assert.IsType <OkObjectResult>(result2);
            var model2        = Assert.IsAssignableFrom <ArticleDTO>(objectResult2.Value);

            Assert.Equal(article, model2);
        }
Пример #3
0
        public async void HighlightArticleNotPublishedTest()
        {
            var controller = new ArticlesController(_context, _userManager);

            var article = _articleDTOs.First(a => a.IsHighlighted == false && a.IsPublished == false);

            Assert.True(article.IsHighlighted == false);
            Assert.True(article.HighlightedAt == null);
            Assert.True(article.IsPublished == false);
            Assert.True(article.PublishedAt == null);

            //POST TEST (Highlight)
            var result = await controller.PostHighlightArticle(article.Id);

            Assert.IsType <BadRequestResult>(result);

            //GET TEST (Article unchanged)
            var result2 = await controller.GetArticle(article.Id);

            var objectResult2 = Assert.IsType <OkObjectResult>(result2);
            var model         = Assert.IsAssignableFrom <ArticleDTO>(objectResult2.Value);

            Assert.True(article.IsHighlighted == false);
            Assert.True(article.HighlightedAt == null);
        }
Пример #4
0
        public async void PostArticleInvalidIdTest()
        {
            var controller = new ArticlesController(_context, _userManager);

            var article    = new ArticleDTO(_articleDTOs.First());
            int originalId = article.Id;

            article.Lead = "Change";

            //Id does not match article Id
            var result = await controller.PostArticle(article.Id + 1, article);

            Assert.IsType <BadRequestResult>(result);


            //Id matches but does not exist
            article.Id = 40;

            var result2 = await controller.PostArticle(article.Id, article);

            Assert.IsType <NotFoundResult>(result2);


            //GET TEST (Article unchanged)
            var result3 = await controller.GetArticle(originalId);

            var objectResult3 = Assert.IsType <OkObjectResult>(result3);
            var model         = Assert.IsAssignableFrom <ArticleDTO>(objectResult3.Value);

            Assert.Equal(_articleDTOs.First(), model);
        }
        public async Task GetArticle_WithExistingId_ReturnsTheCorrectArticleDto()
        {
            // arrange
            var existingId = 2;

            _singleQueryMock
            .Setup(m => m.Execute(It.IsAny <int>()))
            .Returns(Task.FromResult(_articlesDtoPagedList[existingId - 1]));

            // act
            var result = await _articlesController.GetArticle(existingId);

            // assert
            _singleQueryMock.Verify(m => m.Execute(2), Times.Once);
            Assert.That(result, Is.Not.Null);
            Assert.That(result.GetType(), Is.EqualTo(typeof(ActionResult <ArticleDto>)));
            Assert.That(((ObjectResult)result.Result).Value.GetType(), Is.EqualTo(typeof(ArticleDto)));
        }
Пример #6
0
        public void ControlerShouldReturnLastArticle()
        {
            var _articlesController = new ArticlesController(articleService);

            var result = _articlesController.GetArticle(3) as OkNegotiatedContentResult <Article>;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.Content.Title, randomArticles.Last().Title);
        }
Пример #7
0
        public async void GetArticleTest()
        {
            var controller = new ArticlesController(_context, _userManager);
            var result     = await controller.GetArticle(1);

            // Assert
            var objectResult = Assert.IsType <OkObjectResult>(result);
            var model        = Assert.IsAssignableFrom <ArticleDTO>(objectResult.Value);

            Assert.Equal(_articleDTOs.First(), model);
        }
Пример #8
0
        public async void HighlightArticleTest()
        {
            var controller = new ArticlesController(_context, _userManager);

            var article            = _articleDTOs.First(a => a.IsHighlighted == false && a.IsPublished == true);
            var highlightedArticle = _articleDTOs.First(a => a.IsHighlighted == true);

            Assert.True(article.IsHighlighted == false);
            Assert.True(article.HighlightedAt == null);

            Assert.True(article.IsPublished == true);
            Assert.True(article.PublishedAt != null);

            Assert.True(highlightedArticle.IsHighlighted == true);
            Assert.True(highlightedArticle.HighlightedAt != null);

            var result = await controller.PostHighlightArticle(article.Id);

            var objectResult = Assert.IsType <OkResult>(result);


            //GET TEST (Article highlighted)
            var result2 = await controller.GetArticle(article.Id);

            var objectResult2 = Assert.IsType <OkObjectResult>(result2);
            var model2        = Assert.IsAssignableFrom <ArticleDTO>(objectResult2.Value);

            Assert.True(model2.IsHighlighted == true);
            Assert.True(model2.HighlightedAt != null);



            //GET TEST 2 (Highlighted article no longer highlighted)
            var result3 = await controller.GetArticle(highlightedArticle.Id);

            var objectResult3 = Assert.IsType <OkObjectResult>(result3);
            var model3        = Assert.IsAssignableFrom <ArticleDTO>(objectResult3.Value);

            Assert.True(model3.IsHighlighted == false);
            Assert.True(model3.HighlightedAt == null);
        }
Пример #9
0
        public async Task GetArticle_WithIdEqualsTo2_ReturnsTheCorrectArticleDto()
        {
            // arrange

            // act
            var result = await _articlesController.GetArticle(2);

            // assert
            _articleRepositoryMock.Verify(m => m.GetArticleByIdAsync(It.IsAny <int>()), Times.Once);
            Assert.That(result, Is.Not.Null);
            Assert.That(result.GetType(), Is.EqualTo(typeof(ActionResult <ArticleDto>)));
            Assert.That(result.Value.GetType(), Is.EqualTo(typeof(ArticleDto)));
            Assert.That(result.Value.Id, Is.EqualTo(2));
        }
Пример #10
0
        public async void DeleteArticleTest()
        {
            var controller = new ArticlesController(_context, _userManager);

            var article = _articleDTOs.First();


            var result = await controller.DeleteArticle(article.Id);

            Assert.IsType <OkResult>(result);

            var result2 = await controller.GetArticle(article.Id);

            Assert.IsType <NotFoundResult>(result2);
        }
        public void GetArticleTest_ReturnsFoundArticle()
        {
            var article = ArticleHelper.GetRandomTestArticles(1).FirstOrDefault();

            _mockArticleService.Setup(serv => serv.GetEntityAsync(article.Id)).ReturnsAsync(article);

            var resultArticle = (_controller.GetArticle(article.Id).Result.Result as OkObjectResult)?.Value as Article;

            Assert.Equal(article.Category, resultArticle.Category);
            Assert.Equal(article.UserName, resultArticle.UserName);
            Assert.Equal(article.Content, resultArticle.Content);
            Assert.Equal(article.CreatedDate, resultArticle.CreatedDate);
            Assert.Equal(article.Id, resultArticle.Id);
            Assert.Equal(article.Title, resultArticle.Title);
        }
Пример #12
0
        public async void PostArticleTest()
        {
            var controller = new ArticlesController(_context, _userManager);
            var article    = _articleDTOs.First();

            article.Name = "Changed test name";
            article.Lead = "Changed up the lead as well";
            article.Images.RemoveAt(0);

            var result = await controller.PostArticle(article.Id, article);

            // Assert
            var objectResult = Assert.IsType <NoContentResult>(result);

            var result2 = await controller.GetArticle(article.Id);

            var objectResult2 = Assert.IsType <OkObjectResult>(result2);
            var model         = Assert.IsAssignableFrom <ArticleDTO>(objectResult2.Value);

            Assert.Equal(article, model);
        }
Пример #13
0
        public async void PostArticleEmptyFieldTest()
        {
            var controller = new ArticlesController(_context, _userManager);

            //POST TEST 1 (Lead empty)
            var article = new ArticleDTO(_articleDTOs.First());

            article.Lead = "";

            var result = await controller.PostArticle(article.Id, article);

            var objectResult = Assert.IsType <BadRequestResult>(result);

            //POST TEST 2 (Name empty)
            var article2 = new ArticleDTO(_articleDTOs.First());

            article.Name = "";

            var result2 = await controller.PostArticle(article.Id, article);

            var objectResult2 = Assert.IsType <BadRequestResult>(result2);

            //POST TEST 3 (Content empty)
            var article3 = new ArticleDTO(_articleDTOs.First());

            article.Content = "";

            var result3 = await controller.PostArticle(article.Id, article);

            var objectResult3 = Assert.IsType <BadRequestResult>(result3);


            //GET TEST (Article unchanged)
            var result4 = await controller.GetArticle(article.Id);

            var objectResult4 = Assert.IsType <OkObjectResult>(result4);
            var model         = Assert.IsAssignableFrom <ArticleDTO>(objectResult4.Value);

            Assert.Equal(_articleDTOs.First(), model);
        }
Пример #14
0
        public async void UnPublishArticleTest()
        {
            var controller = new ArticlesController(_context, _userManager);

            var article = _articleDTOs.First(a => a.IsPublished == true);

            Assert.True(article.IsPublished == true);
            Assert.True(article.PublishedAt != null);

            //POST TEST (Lead empty)
            var result = await controller.PostUnPublishArticle(article.Id);

            var objectResult = Assert.IsType <OkResult>(result);

            //GET TEST (Article published)
            var result2 = await controller.GetArticle(article.Id);

            var objectResult2 = Assert.IsType <OkObjectResult>(result2);
            var model         = Assert.IsAssignableFrom <ArticleDTO>(objectResult2.Value);

            Assert.True(model.IsPublished == false);
            Assert.True(model.PublishedAt == null);
        }
Пример #15
0
        public async void HighlightArticleAlreadyHighlightedTest()
        {
            var controller = new ArticlesController(_context, _userManager);

            var article = _articleDTOs.First(a => a.IsHighlighted == true);

            Assert.True(article.IsHighlighted == true);
            Assert.True(article.HighlightedAt != null);

            //POST TEST (Highlight)
            var result = await controller.PostHighlightArticle(article.Id);

            var objectResult = Assert.IsType <OkResult>(result);


            //GET TEST (Article still highlighted)
            var result2 = await controller.GetArticle(article.Id);

            var objectResult2 = Assert.IsType <OkObjectResult>(result2);
            var model         = Assert.IsAssignableFrom <ArticleDTO>(objectResult2.Value);

            Assert.True(article.IsHighlighted == true);
            Assert.True(article.HighlightedAt != null);
        }
Пример #16
0
        public List <Categorie> Categorie()
        {
            List <Categorie> resultat = new List <Categorie>();
            List <Article>   article;
            var uneCategorie = new Categorie();

            uneCategorie.Name = "Politique";
            resultat.Add(uneCategorie);
            uneCategorie      = new Categorie();
            uneCategorie.Name = "Sport";
            resultat.Add(uneCategorie);
            uneCategorie      = new Categorie();
            uneCategorie.Name = "Science";
            resultat.Add(uneCategorie);
            uneCategorie      = new Categorie();
            uneCategorie.Name = "Technologie";
            resultat.Add(uneCategorie);
            uneCategorie      = new Categorie();
            uneCategorie.Name = "Art";
            uneCategorie      = new Categorie();
            uneCategorie.Name = "Faits divers";
            resultat.Add(uneCategorie);

            List <Article> articles = new List <Article>();
            Article        art      = new Article();

            art.Titre     = "Tennis";
            art.Categorie = "Sport";
            articles.Add(art);
            art           = new Article();
            art.Titre     = "Football";
            art.Categorie = "Sport";
            articles.Add(art);
            art           = new Article();
            art.Titre     = "Parti de gauche";
            art.Categorie = "Politique";
            articles.Add(art);

            ArticlesController   h    = new ArticlesController();
            IQueryable <Article> arts = h.GetArticle();

            articles = arts.ToList <Article>();

            for (int i = 0; i < resultat.Count(); i++) //pour le nb de categories
            {
                article = new List <Article>();
                for (int j = 0; j < articles.Count(); j++)// pour le nb d'articles
                {
                    if (resultat[i].Name == articles[j].Categorie)
                    {
                        if (article.Count() < 3)
                        {
                            article.Add(articles[j]);
                        }
                    }
                }
                resultat[i].Articles = article;
                article = null;
            }

            return(resultat);

            /*var unArticle = new Article();
             *
             * unArticle.Titre = "Parti de gauche";
             * article.Add(unArticle);
             * unArticle = new Article();
             * unArticle.Titre = "Parti de droite";
             * article.Add(unArticle);
             * unArticle = new Article();
             * unArticle.Titre = "Parti de centre";
             * article.Add(unArticle);
             * uneCategorie.Name = "Politique";
             * uneCategorie.Articles = article;
             * resultat.Add(uneCategorie);
             *
             * unArticle = new Article();
             * unArticle.Titre = "Le football";
             * article.Add(unArticle);
             * unArticle = new Article();
             * unArticle.Titre = "Le basket-ball";
             * article.Add(unArticle);
             * unArticle = new Article();
             * unArticle.Titre = "Tennis";
             * article.Add(unArticle);
             * uneCategorie = new Categorie();
             * uneCategorie.Name = "Sport";
             * uneCategorie.Articles = article;
             * resultat.Add(uneCategorie);
             *
             * unArticle = new Article();
             * unArticle.Titre = "Corps humain";
             * article.Add(unArticle);
             * unArticle = new Article();
             * unArticle.Titre = "Les organes vitaux";
             * article.Add(unArticle);
             * unArticle = new Article();
             * unArticle.Titre = "La chirurgie";
             * article.Add(unArticle);
             * uneCategorie = new Categorie();
             * uneCategorie.Name = "Science";
             * uneCategorie.Articles = article;
             * resultat.Add(uneCategorie);
             *
             * unArticle = new Article();
             * unArticle.Titre = "Lunettes 3D";
             * article.Add(unArticle);
             * unArticle = new Article();
             * unArticle.Titre = "Robot domestique";
             * article.Add(unArticle);
             * unArticle = new Article();
             * unArticle.Titre = "Fusée";
             * article.Add(unArticle);
             * uneCategorie = new Categorie();
             * uneCategorie.Name = "Technologie";
             * uneCategorie.Articles = article;
             * resultat.Add(uneCategorie);
             *
             * unArticle = new Article();
             * unArticle.Titre = "Peinture";
             * article.Add(unArticle);
             * unArticle = new Article();
             * unArticle.Titre = "Cinéma";
             * article.Add(unArticle);
             * unArticle = new Article();
             * unArticle.Titre = "Musique";
             * article.Add(unArticle);
             * uneCategorie = new Categorie();
             * uneCategorie.Name = "Art";
             * uneCategorie.Articles = article;
             * resultat.Add(uneCategorie);
             *
             * return resultat;
             *
             * }
             *
             * [HttpGet]
             * public List<OtherCategorie> OtherCategorie()
             * {
             * List<OtherCategorie> resultat = new List<OtherCategorie>();
             * List<Article> article = new List<Article>();
             *
             * var unArticle = new Article();
             * var uneCategorie = new OtherCategorie();
             *
             * unArticle = new Article();
             * unArticle.Titre = "Le Groenland";
             * article.Add(unArticle);
             * unArticle = new Article();
             * unArticle.Titre = "L'Australie";
             * article.Add(unArticle);
             * unArticle = new Article();
             * unArticle.Titre = "Les canaries";
             * article.Add(unArticle);
             * uneCategorie = new OtherCategorie();
             * uneCategorie.Name = "Géographie";
             * uneCategorie.Articles = article;
             * resultat.Add(uneCategorie);
             *
             * unArticle = new Article();
             * unArticle.Titre = "La première guerre mondiale";
             * article.Add(unArticle);
             * unArticle = new Article();
             * unArticle.Titre = "La chute du mur de berlin";
             * article.Add(unArticle);
             * unArticle = new Article();
             * unArticle.Titre = "Le roi soleil";
             * article.Add(unArticle);
             * uneCategorie = new OtherCategorie();
             * uneCategorie.Name = "Histoire";
             * uneCategorie.Articles = article;
             * resultat.Add(uneCategorie);
             *
             * unArticle = new Article();
             * unArticle.Titre = "Les lasagnes";
             * article.Add(unArticle);
             * unArticle = new Article();
             * unArticle.Titre = "Les escargots";
             * article.Add(unArticle);
             * unArticle = new Article();
             * unArticle.Titre = "Le big mac";
             * article.Add(unArticle);
             * uneCategorie = new OtherCategorie();
             * uneCategorie.Name = "Cuisine";
             * uneCategorie.Articles = article;
             * resultat.Add(uneCategorie);
             *
             * unArticle = new Article();
             * unArticle.Titre = "Baudelaire";
             * article.Add(unArticle);
             * unArticle = new Article();
             * unArticle.Titre = "Zola";
             * article.Add(unArticle);
             * unArticle = new Article();
             * unArticle.Titre = "Maupassant";
             * article.Add(unArticle);
             * uneCategorie = new OtherCategorie();
             * uneCategorie.Name = "Littérature";
             * uneCategorie.Articles = article;
             * resultat.Add(uneCategorie);
             *
             * unArticle = new Article();
             * unArticle.Titre = "La police et le pélican sauvage";
             * article.Add(unArticle);
             * unArticle = new Article();
             * unArticle.Titre = "Le traitre";
             * article.Add(unArticle);
             * unArticle = new Article();
             * unArticle.Titre = "L'arnaque";
             * article.Add(unArticle);
             * uneCategorie = new OtherCategorie();
             * uneCategorie.Name = "Cinéma";
             * uneCategorie.Articles = article;
             * resultat.Add(uneCategorie);
             *
             * unArticle = new Article();
             * unArticle.Titre = "Classique";
             * article.Add(unArticle);
             * unArticle = new Article();
             * unArticle.Titre = "Jazz";
             * article.Add(unArticle);
             * unArticle = new Article();
             * unArticle.Titre = "Rap";
             * article.Add(unArticle);
             * uneCategorie = new OtherCategorie();
             * uneCategorie.Name = "Musique";
             * uneCategorie.Articles = article;
             * resultat.Add(uneCategorie);
             *
             * return resultat;*/
        }