示例#1
0
        public void Logout()
        {
            OauthSess.GrantCode    = "GrantCode1234";
            OauthSess.BearerToken  = "BearerToken1234";
            OauthSess.RefreshToken = "RefreshToken1234";
            OauthSess.Expiration   = DateTime.UtcNow.AddDays(2);

            var expectResult = new FabOauthLogout();
            var expectSess   = ((this as TAppSession) == null ? SessionType.Person : SessionType.App);

            MockOauth
            .Setup(x => x.Logout.Get(It.IsAny <string>(), expectSess))
            .Returns(expectResult);

            FabOauthLogout result = OauthSess.Logout();

            Assert.AreEqual(expectResult, result, "Incorrect result.");

            Assert.Null(OauthSess.GrantCode, "GrantCode should be null.");
            Assert.Null(OauthSess.BearerToken, "BearerToken should be null.");
            Assert.Null(OauthSess.RefreshToken, "RefreshToken should be null.");
            CheckExpiration(DateTime.UtcNow);

            Assert.False(OauthSess.IsAuthenticated, "Incorrect IsAuthenticated.");
        }
        public virtual void HandleGrantCodeRedirect()
        {
            const string grantCode = "GrantCode1234";

            var q = new NameValueCollection();

            q.Add("code", grantCode);
            q.Add("state", PersonSess.SessionId);

            var mockReq = new Mock <HttpRequestBase>(MockBehavior.Strict);

            mockReq.SetupGet(x => x.QueryString).Returns(q);

            var expectResult = NewFabOauthAccess();

            MockOauth
            .Setup(x => x.AccessTokenAuthCode.Get(
                       grantCode,
                       MockConfig.Object.AppSecret,
                       MockConfig.Object.GetOauthRedirectUri(),
                       SessionType.Person
                       ))
            .Returns(expectResult);

            FabOauthAccess result = PersonSess.HandleGrantCodeRedirect(mockReq.Object);

            Assert.AreEqual(expectResult, result, "Incorrect result.");
            Assert.AreEqual(grantCode, PersonSess.GrantCode, "Incorrect GrantCode.");
        }
        public virtual void RefreshTokenIfNecessary(bool pHasRefresh, int pAddSeconds,
                                                    string pApiPath, bool pExpectRefresh)
        {
            string   refresh = (pHasRefresh ? "OldRefreshToken" : null);
            DateTime expiry  = DateTime.UtcNow.AddSeconds(pAddSeconds);

            PersonSess.RefreshToken = refresh;
            PersonSess.Expiration   = expiry;

            FabOauthAccess expectResult = NewFabOauthAccess();

            MockOauth
            .Setup(x => x.AccessTokenRefresh.Get(
                       refresh,
                       MockConfig.Object.AppSecret,
                       MockConfig.Object.GetOauthRedirectUri(),
                       SessionType.Person
                       ))
            .Returns(expectResult);

            bool result = PersonSess.RefreshTokenIfNecessary(pApiPath);

            Assert.AreEqual(pExpectRefresh, result, "Incorrect result.");

            if (pExpectRefresh)
            {
                CheckSessionProperties(expectResult);
            }
            else
            {
                Assert.AreEqual(refresh, PersonSess.RefreshToken, "Incorrect RefreshToken.");
                CheckExpiration(expiry);
            }
        }
        /*--------------------------------------------------------------------------------------------*/
        protected void SetupMockAccessToken(FabOauthAccess pResult, Action pCallback = null)
        {
            IReturnsResult <IOauthService> rr = MockOauth
                                                .Setup(x => x.AccessTokenClientCredentials.Get(
                                                           MockConfig.Object.AppId,
                                                           MockConfig.Object.AppSecret,
                                                           MockConfig.Object.GetOauthRedirectUri(),
                                                           SessionType.App
                                                           ))
                                                .Returns(pResult);

            if (pCallback != null)
            {
                rr.Callback(pCallback);
            }
        }