Beispiel #1
0
        public async Task TestRetrieveWords()
        {
            using (var context = new LanguageAPIContext(options))
            {
                //Given
                LanguageItem languageItem1 = new LanguageItem()
                {
                    userId       = 1,
                    rank         = 1,
                    languageName = "Chinese (Simplified)",
                    languageCode = "zh-CN",
                    word         = "球"
                };
                LanguageItem languageItem2 = new LanguageItem()
                {
                    userId       = 1,
                    rank         = 2,
                    languageName = "Chinese (Simplified)",
                    languageCode = "zh-CN",
                    word         = "帽子"
                };

                //When
                LanguageItemsController languageItemsController = new LanguageItemsController(context);
                IActionResult           result = await languageItemsController.RetrieveFavouriteWords(languageItem1.userId, languageItem1.languageName);

                //Then
                Assert.IsInstanceOfType(result, typeof(OkObjectResult));
                Assert.IsNotNull(result);

                var checkResponse = result as OkObjectResult;
                Assert.IsNotNull(checkResponse);

                var languageItems = checkResponse.Value as List <LanguageItem>;
                for (var i = 0; i < languageItems.Count; i++)
                {
                    bool matchFound = false;
                    for (var j = 0; j < languageItems.Count; j++)
                    {
                        if (languageItems[i].languageName == languageItemList[j].languageName)
                        {
                            matchFound = true;
                        }
                        if (languageItems[i].languageCode == languageItemList[j].languageCode)
                        {
                            matchFound = true;
                        }
                        if (languageItems[i].rank == languageItemList[j].rank)
                        {
                            matchFound = true;
                        }
                        if (languageItems[i].word == languageItemList[j].word)
                        {
                            matchFound = true;
                        }
                    }
                    Assert.IsTrue(matchFound);
                }
            }
        }
Beispiel #2
0
        public async Task TestAddFavouriteWord()
        {
            using (var context = new LanguageAPIContext(options))
            {
                //Given
                LanguageItem languageItem = new LanguageItem()
                {
                    userId       = 1,
                    rank         = 3,
                    languageName = "English",
                    languageCode = "en",
                    word         = "Amazing"
                };
                //When
                LanguageItemsController languageItemsController = new LanguageItemsController(context);
                IActionResult           result = await languageItemsController.AddFavouriteWord(languageItem) as IActionResult;

                //Then
                Assert.IsNotNull(result);
                Assert.IsInstanceOfType(result, typeof(CreatedAtActionResult));

                var exists = await context.LanguageItem.Where(l =>
                                                              l.userId == languageItem.userId &&
                                                              l.rank == languageItem.rank &&
                                                              l.languageName == languageItem.languageName &&
                                                              l.languageCode == languageItem.languageCode &&
                                                              l.word == languageItem.word
                                                              ).AnyAsync();

                Assert.IsNotNull(exists);
                Assert.IsTrue(exists);
            }
        }
Beispiel #3
0
 public void SetupDb()
 {
     using (var context = new LanguageAPIContext(options))
     {
         LanguageItem languageItem1 = new LanguageItem()
         {
             userId       = 1,
             rank         = 1,
             languageName = "Chinese (Simplified)",
             languageCode = "zh-CN",
             word         = "球"
         };
         LanguageItem languageItem2 = new LanguageItem()
         {
             userId       = 1,
             rank         = 2,
             languageName = "Chinese (Simplified)",
             languageCode = "zh-CN",
             word         = "帽子"
         };
         context.LanguageItem.Add(languageItem1);
         context.LanguageItem.Add(languageItem2);
         context.SaveChanges();
     }
 }
Beispiel #4
0
        public async Task testAuthenticateNonExistentUser()
        {
            using (var context = new LanguageAPIContext(options))
            {
                //Given
                UserInfo user = new UserInfo()
                {
                    username = "******",
                    password = "******"
                };
                //When
                UserInfoController userInfoController = new UserInfoController(context);
                IActionResult      result             = await userInfoController.AuthenticateUser(user);

                var userExists = await context.UserInfo.Where(u =>
                                                              u.username == user.username
                                                              ).AnyAsync();

                var dbUser = await context.UserInfo.Where(u =>
                                                          u.username == user.username
                                                          ).FirstOrDefaultAsync();

                Assert.IsNull(dbUser);
                Assert.IsNotNull(result);
                Assert.IsInstanceOfType(result, typeof(BadRequestResult));
                Assert.IsTrue(!userExists);

                context.SaveChanges();
            }
        }
Beispiel #5
0
        public async Task testAddUserwithNoPassword()
        {
            using (var context = new LanguageAPIContext(options))
            {
                //Given
                UserInfo user = new UserInfo()
                {
                    username = "******",
                    password = ""
                };
                //When
                UserInfoController userInfoController = new UserInfoController(context);
                IActionResult      result             = await userInfoController.RegisterUser(user);

                var userExists = await context.UserInfo.Where(u =>
                                                              u.username == user.username
                                                              ).AnyAsync();

                var dbUser = await context.UserInfo.Where(u =>
                                                          u.username == user.username
                                                          ).FirstOrDefaultAsync();

                Assert.IsNotNull(result);
                Assert.IsInstanceOfType(result, typeof(UnprocessableEntityResult));
                Assert.IsTrue(!userExists);

                Assert.IsNull(dbUser);


                context.SaveChanges();
            }
        }
Beispiel #6
0
        public async Task testAuthenticateUserWrongPassword()
        {
            using (var context = new LanguageAPIContext(options))
            {
                //Given
                UserInfo user = new UserInfo()
                {
                    username = "******",
                    password = "******"
                };
                //When
                UserInfoController userInfoController = new UserInfoController(context);
                IActionResult      result             = await userInfoController.AuthenticateUser(user);

                var userExists = await context.UserInfo.Where(u =>
                                                              u.username == user.username
                                                              ).AnyAsync();

                var dbUser = await context.UserInfo.Where(u =>
                                                          u.username == user.username
                                                          ).FirstOrDefaultAsync();

                Assert.IsNotNull(result);
                Assert.IsInstanceOfType(result, typeof(UnauthorizedResult));
                Assert.IsTrue(userExists);

                Assert.AreEqual(dbUser.username, user.username);
                Assert.AreNotEqual(dbUser.password, user.password);

                context.SaveChanges();
            }
        }
Beispiel #7
0
 public void ClearDb()
 {
     using (var context = new LanguageAPIContext(options))
     {
         context.LanguageItem.RemoveRange(context.LanguageItem);
         context.SaveChanges();
     }
 }
Beispiel #8
0
 public void SetupDb()
 {
     using (var context = new LanguageAPIContext(options))
     {
         UserInfo user1 = new UserInfo()
         {
             username = "******",
             password = "******"
         };
         UserInfo user2 = new UserInfo()
         {
             username = "******",
             password = "******"
         };
         context.UserInfo.Add(user1);
         context.UserInfo.Add(user2);
         context.SaveChanges();
     }
 }
Beispiel #9
0
        public async Task testRegisterExistingUser()
        {
            using (var context = new LanguageAPIContext(options)){
                //Given
                UserInfo user = new UserInfo()
                {
                    username = "******",
                    password = "******"
                };

                //When
                UserInfoController userInfoController = new UserInfoController(context);
                IActionResult      result             = await userInfoController.RegisterUser(user);

                //Then
                Assert.IsNotNull(result);
                Assert.IsInstanceOfType(result, typeof(ConflictResult));

                var userList = await context.UserInfo.Where(l => l.username == user.username).ToListAsync();

                Assert.AreEqual(1, userList.Count);
            }
        }
Beispiel #10
0
 public UserInfoController(LanguageAPIContext context)
 {
     _context = context;
 }
Beispiel #11
0
 public LanguageItemsController(LanguageAPIContext context)
 {
     _context = context;
 }