Exemplo n.º 1
0
        public void ChangeUserOnEntrySucceedsIfAdmin()
        {
            DateTime dtInitial = new DateTime(2020, 1, 1);
            DateTime dtChanged = new DateTime(2020, 2, 2);

            Entry entry;

            _context.Add(entry = new Entry()
            {
                Id = 123, Date = dtInitial, UserId = _regularUser1.Id, DistanceInMeters = 55, TimeInSeconds = 99
            });
            _context.SaveChanges();

            _userService.SetUser(_adminUser);
            EntryDataDTO updateDTO;
            var          res = _controller.Update(entry.Id, updateDTO = new EntryDataDTO()
            {
                DistanceInMeters = entry.DistanceInMeters + 101,
                TimeInSeconds    = entry.TimeInSeconds + 102,
                UserId           = _regularUser2.Id,
                Date             = dtChanged
            });

            Assert.IsInstanceOfType(res, typeof(OkResult));

            var res2 = ((_controller.Get(dtChanged, dtChanged, _regularUser2.Id) as OkObjectResult).Value as IEnumerable <EntryDTO>).ToArray();

            Assert.AreEqual(entry.Id, res2.Single().Id);
            Assert.AreEqual(updateDTO.DistanceInMeters, res2.Single().DistanceInMeters);
            Assert.AreEqual(updateDTO.TimeInSeconds, res2.Single().TimeInSeconds);
            Assert.AreEqual(updateDTO.Date, res2.Single().Date);
        }
Exemplo n.º 2
0
        public void AdminCanCreateEntryForAnotherUser()
        {
            _userService.SetUser(_adminUser);
            EntryDataDTO entryNew;
            var          res = _controller.Create(entryNew = new EntryDataDTO()
            {
                DistanceInMeters = 101,
                TimeInSeconds    = 102,
                UserId           = _regularUser2.Id,//other user
                Date             = new DateTime(2020, 1, 1)
            });

            Assert.IsInstanceOfType(res, typeof(OkResult));
        }
Exemplo n.º 3
0
        public void UserCantCreateEntryForAnotherUser()
        {
            _userService.SetUser(_regularUser1);
            EntryDataDTO entryNew;
            var          res = _controller.Create(entryNew = new EntryDataDTO()
            {
                DistanceInMeters = 101,
                TimeInSeconds    = 102,
                UserId           = _regularUser2.Id,//other user
                Date             = new DateTime(2020, 1, 1)
            });

            Assert.AreEqual("Access denied.", (res as BadRequestObjectResult).Value);
        }
Exemplo n.º 4
0
        public void NewEntryWithBadUserIdData()
        {
            EntryDataDTO entryNew;

            _userService.SetUser(_regularUser1);
            var res = _controller.Create(entryNew = new EntryDataDTO()
            {
                DistanceInMeters = 101,
                TimeInSeconds    = 102,
                UserId           = 999999,//bad id
                Date             = new DateTime(2020, 1, 1)
            });

            Assert.AreEqual("Invalid userId.", (res as BadRequestObjectResult).Value);
        }
Exemplo n.º 5
0
        public void NewEntryWithValidData()
        {
            EntryDataDTO entryNew;

            _userService.SetUser(_regularUser1);
            _controller.Create(entryNew = new EntryDataDTO()
            {
                DistanceInMeters = 101,
                TimeInSeconds    = 102,
                UserId           = _regularUser1.Id,
                Date             = new DateTime(2020, 1, 1)
            });

            var res = ((_controller.Get(entryNew.Date, entryNew.Date, _regularUser1.Id) as OkObjectResult).Value as IEnumerable <EntryDTO>).ToArray();

            Assert.AreEqual(entryNew.DistanceInMeters, res.Single().DistanceInMeters);
            Assert.AreEqual(entryNew.TimeInSeconds, res.Single().TimeInSeconds);
            Assert.AreEqual(entryNew.Date, res.Single().Date);
        }