Esempio n. 1
0
        public async Task TestCase_InvalidValidity()
        {
            var context = fixture.GetCurrentDataContext();
            UserHabitsController control = new UserHabitsController(context);

            UserHabit habit = new UserHabit()
            {
                ValidFrom  = new DateTime(2021, 11, 1),
                ValidTo    = new DateTime(2020, 11, 1),
                Category   = HabitCategory.Positive,
                Name       = "Test1",
                TargetUser = "******",
                Frequency  = HabitFrequency.Daily,
            };

            //var rst = await control.Post(habit);
            //if (rst != null)
            //{
            //    BadRequestObjectResult badrequest = (BadRequestObjectResult)rst;
            //    Assert.NotNull(badrequest);
            //    Assert.Equal("Invalid Validity", badrequest.Value);
            //}

            try
            {
                await control.Post(habit);
            }
            catch (Exception ex)
            {
                Assert.NotNull(ex);
            }
        }
Esempio n. 2
0
        public void TestCase_UnauthorizeRead()
        {
            var context = fixture.GetCurrentDataContext();

            var control = new UserCollectionsController(context);

            try
            {
                control.Get();
            }
            catch (Exception ex)
            {
                Assert.IsType <UnauthorizedAccessException>(ex);
            }

            try
            {
                control.Get(1);
            }
            catch (Exception ex)
            {
                Assert.IsType <UnauthorizedAccessException>(ex);
            }

            context.Dispose();
        }
Esempio n. 3
0
        public async Task TestCase_GetList(String usr)
        {
            var context = fixture.GetCurrentDataContext();

            fixture.InitializeTestData();

            var control = new AwardUsersController(context);

            try
            {
                control.Get();
            }
            catch (Exception ex)
            {
                Assert.NotNull(ex);
            }

            if (!String.IsNullOrEmpty(usr))
            {
                var userclaim = DataSetupUtility.GetClaimForUser(usr);
                control.ControllerContext = new ControllerContext()
                {
                    HttpContext = new DefaultHttpContext()
                    {
                        User = userclaim
                    }
                };

                var getrst = control.Get();
                Assert.NotNull(getrst);

                if (usr == DataSetupUtility.UserA)
                {
                    Assert.Equal(1, getrst.Count());
                }
                else if (usr == DataSetupUtility.UserB)
                {
                    Assert.Equal(0, getrst.Count());
                }
            }

            await context.DisposeAsync();
        }
        public async Task TestCase_GetList(String usr)
        {
            var context = fixture.GetCurrentDataContext();

            fixture.InitializeTestData();

            var control = new InvitedUsersController(context);

            try
            {
                control.Get();
            }
            catch (Exception ex)
            {
                Assert.IsType <UnauthorizedAccessException>(ex);
            }

            if (!String.IsNullOrEmpty(usr))
            {
                var userclaim = DataSetupUtility.GetClaimForUser(usr);
                control.ControllerContext = new ControllerContext()
                {
                    HttpContext = new DefaultHttpContext()
                    {
                        User = userclaim
                    }
                };

                var getrst = control.Get();
                Assert.NotNull(getrst);
                // Why just retun the useritself?
                Assert.Equal(1, getrst.Count());
            }

            await context.DisposeAsync();
        }
Esempio n. 5
0
        public async Task TestCase_CRUD()
        {
            var context = fixture.GetCurrentDataContext();
            ExerciseItemsController control = new ExerciseItemsController(context);
            var userclaim = DataSetupUtility.GetClaimForUser(DataSetupUtility.UserA);

            control.ControllerContext = new ControllerContext()
            {
                HttpContext = new DefaultHttpContext()
                {
                    User = userclaim
                }
            };

            // Step 1. Read all
            var rsts    = control.Get();
            var rstscnt = await rsts.CountAsync();

            var existcnt = context.ExerciseItems.Count();

            Assert.Equal(existcnt, rstscnt);

            // Step 2. Create one know ledge item
            var ki = new ExerciseItem()
            {
                ExerciseType = ExerciseItemType.Question,
                Content      = "New Test 1 Content",
            };

            ki.Answer = new ExerciseItemAnswer
            {
                Content = "New Answer"
            };
            ki.Tags.Add(new ExerciseTag()
            {
                TagTerm = DataSetupUtility.Tag1,
            });
            var rst = await control.Post(ki);

            Assert.NotNull(rst);
            var rst2 = Assert.IsType <CreatedODataResult <ExerciseItem> >(rst);

            Assert.Equal(rst2.Entity.Content, ki.Content);
            var firstid = rst2.Entity.ID;

            Assert.True(firstid > 0);
            objectsCreated.Add(firstid);
            // Check the answer
            var answerctrl   = new ExerciseItemAnswersController(context);
            var answergetrst = answerctrl.Get(firstid);

            Assert.NotNull(answergetrst);
            var answergetrstresult = Assert.IsType <SingleResult <ExerciseItemAnswer> >(answergetrst);
            var answerobj          = answergetrstresult.Queryable.ToList().ElementAtOrDefault(0);

            Assert.Equal(ki.Answer.Content, answerobj.Content);

            // Check the tag
            var exertagctrl   = new ExerciseTagsController(context);
            var exertaggetrst = exertagctrl.Get();

            Assert.NotNull(exertaggetrst);
            var exertagobj = exertaggetrst.ToList().Find(p => p.RefID == firstid);

            Assert.NotNull(exertagobj);
            Assert.Equal(DataSetupUtility.Tag1, exertagobj.TagTerm);

            // Step 3. Read all - 1
            rsts    = control.Get();
            rstscnt = await rsts.CountAsync();

            Assert.Equal(existcnt + 1, rstscnt);

            // Step 3a. Read single
            var singlerst    = control.Get(firstid);
            var singleresult = Assert.IsType <SingleResult <ExerciseItem> >(singlerst);
            var readitem     = singleresult.Queryable.ToList().ElementAtOrDefault(0);

            Assert.NotNull(readitem);
            Assert.Equal(firstid, readitem.ID);
            Assert.Equal(ki.ExerciseType, readitem.ExerciseType);
            Assert.Equal(ki.Content, readitem.Content);

            // Step 4. Change the exist one by Put
            readitem.Content += "Updated by PUT";
            if (readitem.Tags == null)
            {
                readitem.Tags = new List <ExerciseTag>();
            }
            readitem.Tags.Add(new ExerciseTag
            {
                RefID   = firstid,
                TagTerm = DataSetupUtility.Tag2
            });
            var rstPut = await control.Put(firstid, readitem);

            Assert.NotNull(rstPut);
            // Due to the fact that updated result is empty
            // Check the result in database directly
            var dbkis = context.ExerciseItems.Where(p => p.ID == firstid).ToList <ExerciseItem>();

            Assert.NotEmpty(dbkis);
            Assert.Equal(readitem.Content, dbkis[0].Content); // Check content only!
            var dbtags = context.ExerciseTags.Where(p => p.RefID == firstid).ToList <ExerciseTag>();

            Assert.NotEmpty(dbtags);
            Assert.Equal(2, dbtags.Count);
            Assert.True(dbtags.Find(p => p.TagTerm == DataSetupUtility.Tag1) != null);
            Assert.True(dbtags.Find(p => p.TagTerm == DataSetupUtility.Tag2) != null);

            // Step 4a. Change the tags again
            readitem.Tags.Clear();
            readitem.Tags.Add(new ExerciseTag {
                RefID = firstid, TagTerm = DataSetupUtility.Tag2
            });
            rstPut = await control.Put(firstid, readitem);

            Assert.NotNull(rstPut);
            // Due to the fact that updated result is empty
            // Check the result in database directly
            dbtags = context.ExerciseTags.Where(p => p.RefID == firstid).ToList <ExerciseTag>();
            Assert.NotEmpty(dbtags);
            Assert.Single(dbtags);
            Assert.True(dbtags.Find(p => p.TagTerm == DataSetupUtility.Tag1) == null);
            Assert.True(dbtags.Find(p => p.TagTerm == DataSetupUtility.Tag2) != null);
            var tagviewcontrol = new ExerciseItemWithTagViewsController(context);
            var tagviewgetrst  = tagviewcontrol.Get();

            Assert.NotNull(tagviewgetrst);

            // Step 5. Change the object via PATCH
            var delta = new Delta <ExerciseItem>();

            delta.UpdatableProperties.Clear();
            delta.UpdatableProperties.Add("Content");
            readitem.Content += "Changed for PATCH";
            delta.TrySetPropertyValue("Content", readitem.Content);
            var patchrst = control.Patch(firstid, delta);

            dbkis = context.ExerciseItems.Where(p => p.ID == firstid).ToList <ExerciseItem>();
            Assert.NotEmpty(dbkis);
            Assert.Equal(readitem.Content, dbkis[0].Content); // Check content only!

            // Step 6. Delete the object
            var delrst = control.Delete(firstid);

            Assert.NotNull(delrst);
            dbkis = context.ExerciseItems.Where(p => p.ID == firstid).ToList <ExerciseItem>();
            Assert.Empty(dbkis);

            await context.DisposeAsync();
        }
        public async Task TestCase_CRUD()
        {
            var context = fixture.GetCurrentDataContext();
            KnowledgeItemsController control = new KnowledgeItemsController(context);
            var userclaim = DataSetupUtility.GetClaimForUser(DataSetupUtility.UserA);

            control.ControllerContext = new ControllerContext()
            {
                HttpContext = new DefaultHttpContext()
                {
                    User = userclaim
                }
            };

            // Step 1. Read all - 0
            var rsts    = control.Get();
            var rstscnt = await rsts.CountAsync();

            var existcnt = context.KnowledgeItems.Count();

            Assert.Equal(existcnt, rstscnt);

            // Step 2. Create one know ledge item
            var ki = new KnowledgeItem()
            {
                Category = KnowledgeItemCategory.Concept,
                Title    = "New Test 1",
                Content  = "New Test 1 Content",
            };
            var rst = await control.Post(ki);

            Assert.NotNull(rst);
            var rst2 = Assert.IsType <CreatedODataResult <KnowledgeItem> >(rst);

            Assert.Equal(rst2.Entity.Title, ki.Title);
            Assert.Equal(rst2.Entity.Content, ki.Content);
            var firstid = rst2.Entity.ID;

            Assert.True(firstid > 0);
            objectsCreated.Add(firstid);

            // Step 3. Read all - 1
            rsts    = control.Get();
            rstscnt = await rsts.CountAsync();

            Assert.Equal(existcnt + 1, rstscnt);

            // Step 3a. Read single
            var getrst = control.Get(firstid);

            Assert.NotNull(getrst);
            var getrstresult = Assert.IsType <SingleResult <KnowledgeItem> >(getrst);
            var readitem     = getrstresult.Queryable.ToList().ElementAtOrDefault(0);

            Assert.NotNull(readitem);
            Assert.Equal(firstid, readitem.ID);
            Assert.Equal(ki.Category, readitem.Category);
            Assert.Equal(ki.Title, readitem.Title);
            Assert.Equal(ki.Content, readitem.Content);

            // Step 4. Change the exist one by Put
            readitem.Content += "Updated by PUT";
            if (readitem.Tags == null)
            {
                readitem.Tags = new List <KnowledgeTag>();
            }
            readitem.Tags.Add(new KnowledgeTag
            {
                RefID   = firstid,
                TagTerm = DataSetupUtility.Tag1
            });
            var rstPut = await control.Put(firstid, readitem);

            Assert.NotNull(rstPut);
            // Due to the fact that updated result is empty
            // Check the result in database directly
            var dbkis = context.KnowledgeItems.Where(p => p.ID == firstid).ToList <KnowledgeItem>();

            Assert.NotEmpty(dbkis);
            Assert.Equal(readitem.Content, dbkis[0].Content); // Check content only!

            // Check the tags view
            var tagcontrol = new KnowledgeTagsController(context);
            var tagsread   = tagcontrol.Get();

            Assert.NotNull(tagsread);
            var tagsreadresult = tagsread.ToList();
            var findtag        = tagsreadresult.Find(p => p.RefID == firstid && p.TagTerm == DataSetupUtility.Tag1);

            Assert.NotNull(findtag);

            // Step 5. PATCH .
            var delta = new Delta <KnowledgeItem>();

            delta.UpdatableProperties.Clear();
            delta.UpdatableProperties.Add("Content");
            readitem.Content += "Changed for PATCH";
            delta.TrySetPropertyValue("Content", readitem.Content);
            var patchresult = await control.Patch(firstid, delta);

            Assert.NotNull(patchresult);
            dbkis = context.KnowledgeItems.Where(p => p.ID == firstid).ToList <KnowledgeItem>();
            Assert.NotEmpty(dbkis);
            Assert.Equal(readitem.Content, dbkis[0].Content); // Check content only!

            // Step 5. Delete
            rst = await control.Delete(firstid);

            Assert.NotNull(rst);
            this.objectsCreated.Remove(firstid);

            rsts    = control.Get();
            rstscnt = await rsts.CountAsync();

            Assert.Equal(existcnt, rstscnt);

            await context.DisposeAsync();
        }