Example #1
0
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            IEnumerable <string> tokenItem = null;

            actionContext.Request.Headers.TryGetValues("token", out tokenItem);
            //没有token
            if (tokenItem == null)
            {
                throwUnanthorizedException();
            }
            string         token          = tokenItem.ToList()[0];
            VolunteerToken volunteerToken = VolunteerService.GetToken(token);

            //数据库没有此token
            if (volunteerToken == null)
            {
                throwUnanthorizedException();
            }
            //token过期
            if (volunteerToken.ExpiredTime < DateTime.UtcNow)
            {
                throwUnanthorizedException();
            }

            Volunteer volunteer = VolunteerService.GetVolunteer(volunteerToken.VolunteerId);

            //volunteer是否存在或有效
            if (volunteer == null || volunteer.Status == EnumUserStatus.注销 || volunteer.Status == EnumUserStatus.密码输入错误临时锁定 || volunteer.Status == EnumUserStatus.注册未验证手机)
            {
                throwUnanthorizedException();
            }

            SetPrincipal(new VolunteerPrincipal(volunteer));

            if (!VolunteerService.UpdateTokenActiveTime(volunteer.Id))
            {
                throwUnanthorizedException();
            }
            base.OnActionExecuting(actionContext);
        }
Example #2
0
        public void GetVolunteer_Success_Test()
        {
            // Arrange
            int         id        = 1;
            R_Volunteer volunteer = SampleVolunteer(id);

            // create mock for repository
            var mock = new Mock <IVolunteerRepository>();

            mock.Setup(s => s.GetVolunteer(Moq.It.IsAny <int>())).Returns(volunteer);

            // service
            VolunteerService volunteerService = new VolunteerService();

            VolunteerService.Repository = mock.Object;

            // Act
            VolunteerDTO result = volunteerService.GetVolunteer(id);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.VolunteerId);
        }