public async Task CreateDeck_InvalidObjectPassed_ReturnsBadRequest()
        {
            // Arrange
            var controller      = new DecksController(flashcardDbContextMock.Object, orderService, logger);
            var nameMissingDeck = new DeckDTO
            {
                Content         = "Uj Teszt csomag tartalma",
                Module          = Module.B,
                Deck_number     = 3,
                ApplicationUser = new ApplicationUserDTO
                {
                    Id                 = "268e543e-2020-ue63-a111-98321798c21",
                    Email              = "*****@*****.**",
                    Name               = "Teszt User 2",
                    Phone              = "063012345672",
                    Workplace          = "Teszt kornyezet2",
                    Create_module      = Module.E,
                    Will_create_module = Module.F,
                    Activated          = true
                }
            };

            controller.ModelState.AddModelError("Name", "Required");

            // Act
            var result = await controller.CreateDeck(nameMissingDeck);

            // Asssert
            Assert.IsType <BadRequestObjectResult>(result.Result);
        }
        public async Task UpdateDeck_UnknownIdPassed_ReturnsNotFound()
        {
            // Arrange
            var controller = new DecksController(flashcardDbContextMock.Object, orderService, logger);
            int unknownId  = 15;
            var rightDeck  = new DeckDTO
            {
                Name            = "Uj csomag neve",
                Content         = "Uj Teszt csomag tartalma",
                Module          = Module.B,
                Deck_number     = 3,
                ApplicationUser = new ApplicationUserDTO
                {
                    Id                 = "268e543e-2020-ue63-a111-98321798c21",
                    Email              = "*****@*****.**",
                    Name               = "Teszt User 2",
                    Phone              = "063012345672",
                    Workplace          = "Teszt kornyezet2",
                    Create_module      = Module.E,
                    Will_create_module = Module.F,
                    Activated          = true
                }
            };

            // Act
            var result = await controller.UpdateDeck(unknownId, rightDeck);

            // Asssert
            Assert.IsType <NotFoundResult>(result);
        }
        public async Task AddDeck_Adds_And_Returns_New_Deck()
        {
            //Arrange
            DeckRequest newDeckRequest = new DeckRequest()
            {
                Name = "ASP.NET Core"
            };
            var mockRepository = new Mock <IFlashcardDataRepository>();

            mockRepository.Setup(x => x.AddDeck(newDeckRequest.Name)).Returns((string deckName) =>
            {
                Deck deck = new Deck
                {
                    Id   = GetFakeDecks().Count + 1,
                    Name = deckName
                };

                return(Task.FromResult <Deck>(deck));
            });


            DecksController decksController = new DecksController(mockRepository.Object, iMapper);
            //Act
            var result = await decksController.AddDeck(newDeckRequest);

            //Assert
            var okObjectResult = Assert.IsType <CreatedAtActionResult>(result);
            var returnDeck     = Assert.IsType <DeckResponseDto>(okObjectResult.Value);

            Assert.Equal("ASP.NET Core", returnDeck.Name);
            Assert.IsType <int>(returnDeck.Id);
            Assert.True(returnDeck.Id > 0);
        }
Esempio n. 4
0
        public void TestInitialize()
        {
            _fixture            = new Fixture();
            _deckControlService = new Mock <IDeckService>();

            _controllerUnderTest = new DecksController(_deckControlService.Object);
        }
        public async Task GetDecksWithSearchTextTest()
        {
            using DbContext context = CreateContext();
            DecksController             controller = CreateController(context);
            Dictionary <string, object> parameters = new Dictionary <string, object>()
            {
                { EntityControllerBase <Deck, long> .SearchTextParameter, "test123" }
            };

            //No cards matching the search text
            ActionResult <List <Deck> > result = await controller.GetAsync(parameters);

            Assert.AreEqual(0, result.Value.Count);

            //Search text is deck id
            parameters[EntityControllerBase <Card, long> .SearchTextParameter] = "1";
            result = await controller.GetAsync(parameters);

            Assert.AreEqual(1, result.Value.Count);
            Assert.AreEqual(deck.DeckId, result.Value[0].DeckId);

            //title contains the search text
            parameters[EntityControllerBase <Card, long> .SearchTextParameter] = "fault";
            result = await controller.GetAsync(parameters);

            Assert.AreEqual(1, result.Value.Count);
            Assert.AreEqual(deck.DeckId, result.Value[0].DeckId);

            //title of default card template contains the search text
            parameters[EntityControllerBase <Card, long> .SearchTextParameter] = "emp";
            result = await controller.GetAsync(parameters);

            Assert.AreEqual(1, result.Value.Count);
            Assert.AreEqual(deck.DeckId, result.Value[0].DeckId);
        }
        public async Task GetDeck_ExistingIdPassed_ReturnsRightDeck()
        {
            // Arrange
            var controller     = new DecksController(flashcardDbContextMock.Object, orderService, logger);
            int existingDeckId = 2;

            // Act
            var result = await controller.GetDeck(existingDeckId);

            // Asssert
            Assert.Equal("Teszt csomag 2", result.Value.Name);
        }
Esempio n. 7
0
    public void OnMouseUp()
    {
        controller = GameObject.Find("Canvas").GetComponent <DecksController>();
        Card     card   = gameObject.GetComponent <Card>();
        CardBean leader = new CardBean();

        leader.id       = card.Id;
        leader.name     = card.Name;
        leader.strength = card.Strength;
        controller.setLeaderDeck(leader);
        controller.createDeck();
    }
        public async Task UpdateActivateDeck_UnknownIdPassed_ReturnsNotFound()
        {
            // Arrange
            var controller = new DecksController(flashcardDbContextMock.Object, orderService, logger);
            int unknownId  = 15;

            // Act
            var result = await controller.UpdateActivateDeck(unknownId);

            // Asssert
            Assert.IsType <NotFoundResult>(result);
        }
        public async Task GetAllDecks_ReturnsAllDecks()
        {
            // Arrange
            var controller = new DecksController(flashcardDbContextMock.Object, orderService, logger);

            // Act
            var result = await controller.GetAllDecks();

            // Asssert
            var cards = Assert.IsType <List <DeckDTO> >(result.Value);

            Assert.Equal(4, cards.Count);
        }
Esempio n. 10
0
        public async Task GetMyDecks_ReturnsAllDecksByUserId()
        {
            // Arrange
            var    controller     = new DecksController(flashcardDbContextMock.Object, orderService, logger);
            string existingUserId = "5049ed9e-7929-4fae-a693-6449097059a8";

            // Act
            var result = await controller.GetMyDecks(existingUserId);

            // Asssert
            var cards = Assert.IsType <List <DeckDTO> >(result.Value);

            Assert.Equal(2, cards.Count);
        }
Esempio n. 11
0
        public async Task GetDecksByModule_ReturnsAllActivatedDecksByModule()
        {
            // Arrange
            var controller = new DecksController(flashcardDbContextMock.Object, orderService, logger);
            int module     = 1;

            // Act
            var result = await controller.GetDecksByModule(module);

            // Asssert
            var cards = Assert.IsType <List <DeckMobileDTO> >(result.Value);

            Assert.Equal(2, cards.Count);
        }
        public async Task DeleteDeck_Returns_204_When_Deck_Exists()
        {
            //Arrange
            var mockRepository = new Mock <IFlashcardDataRepository>();

            mockRepository.Setup(x => x.DeleteDeckById(11)).Returns((int id) => Task.FromResult <bool>(true));

            DecksController decksController = new DecksController(mockRepository.Object, iMapper);

            //Act
            var result = await decksController.DeleteDeckById(11);

            //Assert
            var noContentResult = Assert.IsType <NoContentResult>(result);
        }
        public async Task UpdateDeck_Returns_204_When_Deck_Exists()
        {
            //Arrange
            var mockRepository = new Mock <IFlashcardDataRepository>();

            mockRepository.Setup(x => x.UpdateDeckNameById(11, "any")).Returns((int id, string name) => Task.FromResult(true));

            DecksController decksController = new DecksController(mockRepository.Object, iMapper);

            //Act
            var result = await decksController.UpdateDeckById(11, new Deck { Name = "any" });

            //Assert
            var noContentResult = Assert.IsType <NoContentResult>(result);
        }
Esempio n. 14
0
        public static void DecksControllerInitialize(TestContext context)
        {
            _flashCardRepository = new FlashCardRepository("./Data/testData.json");

            var decksProfile  = new Profiles.DecksProfile();
            var cardsProfile  = new Profiles.CardsProfile();
            var configuration = new MapperConfiguration(config =>
            {
                config.AddProfile(decksProfile);
                config.AddProfile(cardsProfile);
            });

            _mapper = new Mapper(configuration);

            _decks = new DecksController(_flashCardRepository, _mapper);
        }
        public async Task GetDeckById_Returns_NotFound_When_Deck_NotExist()
        {
            //Arrange
            var mockRepository = new Mock <IFlashcardDataRepository>();

            mockRepository.Setup(x => x.GetDeckById(1)).Returns((int id) => Task.FromResult <Deck>(null));

            DecksController decksController = new DecksController(mockRepository.Object, iMapper);

            //Act
            var result = await decksController.GetDeckById(1);

            //Assert
            var notFoundResult   = Assert.IsType <NotFoundObjectResult>(result);
            var returnedResponse = Assert.IsType <ErrorResponse>(notFoundResult.Value);

            Assert.Equal("Deck with Id 1 not found", returnedResponse.Errors[0]);
        }
        public async Task Get_Method_Returns_Ok()
        {
            //Arrange
            var mockRepository = new Mock <IFlashcardDataRepository>();

            mockRepository.Setup(x => x.GetAllDecks()).Returns(Task.FromResult(GetFakeDecks()));

            DecksController decksController = new DecksController(mockRepository.Object, iMapper);

            //Act
            var result = await decksController.Get();

            //Assert
            var okObjectResult = Assert.IsType <OkObjectResult>(result);
            var returnDecks    = Assert.IsType <DeckResponse>(okObjectResult.Value);

            Assert.Equal(2, returnDecks.Decks.Count);
        }
        public async Task UpdateDeck_Returns_NotFound_When_Deck_NotExist()
        {
            //Arrange
            var mockRepository = new Mock <IFlashcardDataRepository>();

            mockRepository.Setup(x => x.UpdateDeckNameById(10, "any")).Returns((int id, string name) => Task.FromResult(false));

            DecksController decksController = new DecksController(mockRepository.Object, iMapper);

            //Act
            var result = await decksController.UpdateDeckById(10, new Deck { Name = "any" });

            //Assert
            var notFoundObjectResult = Assert.IsType <NotFoundObjectResult>(result);
            var errorResponse        = Assert.IsType <ErrorResponse>(notFoundObjectResult.Value);

            Assert.Equal("Deck with Id 10 not found", errorResponse.Errors[0]);
        }
        public async Task PutDeckTest()
        {
            using DbContext context = CreateContext();
            DecksController controller = CreateController(context);

            //null as parameter -> bad request
            ActionResult <Deck> result = await controller.PutAsync(null);

            Assert.IsTrue(result.Result is BadRequestResult);

            //deck does not exist in db -> not found
            Deck newDeck = new Deck()
            {
                DeckId = 5,
                Title  = "dfsdf",
                DefaultCardTemplateId = template.CardTemplateId
            };

            result = await controller.PutAsync(newDeck);

            Assert.IsTrue(result.Result is NotFoundResult);

            //Save changed entity
            deck.Title = "New Title";
            result     = await controller.PutAsync(deck);

            Assert.IsNotNull(result.Value);
            Deck deck1 = context.Find <Deck>(deck.DeckId);

            Assert.AreEqual("New Title", deck1.Title);

            //Invalid deck is validated
            bool wasThrown = false;

            try
            {
                result = await controller.PostAsync(new Deck());
            }
            catch (NotifyException)
            {
                wasThrown = true;
            }
            Assert.IsTrue(wasThrown);
        }
        public async Task GetDeckById_Returns_ValidResponse_When_Deck_Exist()
        {
            //Arrange
            var mockRepository = new Mock <IFlashcardDataRepository>();

            mockRepository.Setup(x => x.GetDeckById(1)).Returns((int id) => Task.FromResult(GetFakeDecks().Find(deck => deck.Id == id)));

            DecksController decksController = new DecksController(mockRepository.Object, iMapper);

            //Act
            var result = await decksController.GetDeckById(1);

            //Assert
            var okObjectResult   = Assert.IsType <OkObjectResult>(result);
            var returnedResponse = Assert.IsType <DeckResponseDto>(okObjectResult.Value);

            Assert.Equal("JavaScript", returnedResponse.Name);
            Assert.Equal(1, returnedResponse.Id);
        }
Esempio n. 20
0
        public void SetUp()
        {
            _userManager = Substitute.For <UserManager <ApplicationUser> >
                           (
                Substitute.For <IUserStore <ApplicationUser> >(),
                Substitute.For <IOptions <IdentityOptions> >(),
                Substitute.For <IPasswordHasher <ApplicationUser> >(),
                new IUserValidator <ApplicationUser> [0],
                new IPasswordValidator <ApplicationUser> [0],
                Substitute.For <ILookupNormalizer>(),
                Substitute.For <IdentityErrorDescriber>(),
                Substitute.For <IServiceProvider>(),
                Substitute.For <ILogger <UserManager <ApplicationUser> > >()
                           );


            _mediator = Substitute.For <IMediator>();

            _sut = new DecksController(_mediator, _userManager);
        }
        public async Task GetDecksTest()
        {
            using DbContext context = CreateContext();
            DecksController             controller = CreateController(context);
            Dictionary <string, object> parameters = new Dictionary <string, object>();

            //Get all decks of user
            ActionResult <List <Deck> > result = await controller.GetAsync(parameters);

            Assert.AreEqual(1, result.Value.Count);
            Assert.AreEqual(deck.DeckId, result.Value[0].DeckId);
            Assert.AreEqual(2, result.Value[0].CardCount);
            Assert.AreEqual(1, result.Value[0].DueCardCount); //Only fields with a value are counted

            //Get all pinned decks
            parameters.Add(nameof(Deck.IsPinned), true);
            result = await controller.GetAsync(parameters);

            Assert.AreEqual(0, result.Value.Count);
        }
        public async Task PostDeckTest()
        {
            using DbContext context = CreateContext();
            DecksController controller = CreateController(context);

            //null as parameter -> bad request
            ActionResult <Deck> result = await controller.PostAsync(null);

            Assert.IsTrue(result.Result is BadRequestResult);

            //Create new valid entity
            Deck deck1 = new Deck()
            {
                DeckId = 3,
                Title  = "test123",
                DefaultCardTemplateId = template.CardTemplateId
            };

            result = await controller.PostAsync(deck1);

            Assert.IsNotNull(result.Value);
            deck1 = context.Find <Deck>((long)3);
            Assert.IsNotNull(deck1);
            Assert.AreEqual(User.UserId, deck1.UserId);

            //Invalid deck is validated
            bool wasThrown = false;

            try
            {
                result = await controller.PostAsync(new Deck());
            }
            catch (NotifyException)
            {
                wasThrown = true;
            }
            Assert.IsTrue(wasThrown);
        }
        public async Task DeleteDeckTest()
        {
            using DbContext context = CreateContext();
            DecksController controller = CreateController(context);

            //null as parameter -> bad request
            IActionResult result = await controller.DeleteAsync(null);

            Assert.IsTrue(result is BadRequestResult);

            //deck does not exist -> not found
            result = await controller.DeleteAsync(new Deck());

            Assert.IsTrue(result is NotFoundResult);

            //delete exitsting entity
            result = await controller.DeleteAsync(deck);

            Assert.IsTrue(result is OkResult);
            Deck deck1 = context.Find <Deck>((long)1);

            Assert.IsNull(deck1);
        }
        public async Task GetDeckByIdTest()
        {
            using DbContext context = CreateContext();
            DecksController controller = CreateController(context);

            //get deck successfully
            ActionResult <Deck> result = await controller.GetAsync(1);

            Assert.IsNotNull(result.Value);
            Assert.AreEqual(1, result.Value.Cards.Count);
            Assert.AreEqual(card.CardId, result.Value.Cards[0].CardId);
            Assert.AreEqual(1, result.Value.Cards[0].Fields.Count);
            Assert.AreEqual(fieldDefinition1.FieldName, result.Value.Cards[0].Fields[0].CardFieldDefinition.FieldName);

            //Deck of other user -> unauthorized
            result = await controller.GetAsync(2);

            Assert.IsTrue(result.Result is UnauthorizedResult);

            //Deck does not exist -> not found
            result = await controller.GetAsync(3);

            Assert.IsTrue(result.Result is NotFoundResult);
        }
 public void SetUp()
 {
     deckStorageMock = new Mock <IDeckStorage>();
     shufflerMock    = new Mock <IShuffler>();
     controller      = new DecksController(deckStorageMock.Object, shufflerMock.Object);
 }