Exemple #1
0
        public async Task apiSubscribeTest()
        {
            memMoq.Setup(m => m.CreateEntry(It.IsAny <object>())).Returns(new Mock <ICacheEntry>().Object);
            LogIn(users[1]);
            // open, but user[1] is participant
            UserContext _context = GetContext();

            int c = await _context.GroupSubscriber.CountAsync();

            JsonResult res = await controller.apiSubscribe(groups[0].Id);

            Assert.AreEqual(c, subscribers.Count);
            // closed (no subscribers)
            c   = GetGroupSubCount(groups[1].Id);
            res = await controller.apiSubscribe(groups[1].Id);

            Assert.AreEqual(c, GetGroupSubCount(groups[1].Id));
            // private (no subscribers)
            c   = GetGroupSubCount(groups[1].Id);
            res = await controller.apiSubscribe(groups[2].Id);

            Assert.AreEqual(c, GetGroupSubCount(groups[2].Id));

            LogIn(users[2]);
            // open, but user[1] is subscriber
            c   = GetGroupSubCount(groups[0].Id);
            res = await controller.apiSubscribe(groups[0].Id);

            Assert.AreEqual(c - 1, GetGroupSubCount(groups[0].Id));
            res = await controller.apiSubscribe(groups[0].Id);

            Assert.AreEqual(c, GetGroupSubCount(groups[0].Id));
            // closed (no subscribers)
            c   = GetGroupSubCount(groups[1].Id);
            res = await controller.apiSubscribe(groups[1].Id);

            Assert.AreEqual(c, GetGroupSubCount(groups[1].Id));
            // private (no subscribers)
            c   = GetGroupSubCount(groups[2].Id);
            res = await controller.apiSubscribe(groups[2].Id);

            Assert.AreEqual(c, GetGroupSubCount(groups[2].Id));

            LogIn(users[3]);
            // open
            c   = GetGroupSubCount(groups[0].Id);
            res = await controller.apiSubscribe(groups[0].Id);

            Assert.AreEqual(c + 1 + groups[0].Admins.Count, new List <UserModel>((IEnumerable <UserModel>)res.Value.GetType().GetProperty("Subscribers").GetValue(res.Value)).Count);
            Assert.True((bool)res.Value.GetType().GetProperty("IsSubscribed").GetValue(res.Value));
            Assert.AreEqual(c + 1, GetGroupSubCount(groups[0].Id));
            // unsubscribe
            res = await controller.apiSubscribe(groups[0].Id);

            Assert.AreEqual(c + groups[0].Admins.Count, new List <UserModel>((IEnumerable <UserModel>)res.Value.GetType().GetProperty("Subscribers").GetValue(res.Value)).Count);
            Assert.False((bool)res.Value.GetType().GetProperty("IsSubscribed").GetValue(res.Value));
            Assert.AreEqual(c, GetGroupSubCount(groups[0].Id));
            // closed (no subscribers)
            c   = GetGroupSubCount(groups[1].Id);
            res = await controller.apiSubscribe(groups[1].Id);

            Assert.IsInstanceOf <JsonError>(res.Value);
            Assert.AreEqual(c, GetGroupSubCount(groups[1].Id));
            // private (no subscribers)
            c   = GetGroupSubCount(groups[2].Id);
            res = await controller.apiSubscribe(groups[2].Id);

            Assert.IsInstanceOf <JsonError>(res.Value);
            Assert.AreEqual(c, GetGroupSubCount(groups[2].Id));

            res = await controller.apiSubscribe(1000);

            Assert.IsInstanceOf <JsonError>(res.Value);
        }