public void IsValid_WhenIfSessionSpecifiedAndNoMnoSession_ItShouldReturnTrue()
        {
            // Http context
            Helpers.destroyMnoSession();
            var httpSession = Helpers.FakeHttpSessionState();

            // test
            Session mnoSession = new Session(httpSession);
            Assert.IsTrue(mnoSession.IsValid(ifSession: true));
        }
        public void IsValid_WhenNoRecheckRequired_ItShouldReturnTrue()
        {
            // Http context
            var recheck = DateTime.UtcNow.AddMinutes(1);
            Helpers.injectMnoSession(recheck);
            var httpSession = Helpers.FakeHttpSessionState();

            // test
            Session mnoSession = new Session(httpSession);
            Assert.IsTrue(mnoSession.IsValid());
        }
        public void IsRemoteCheckRequired_ItReturnsTrueIfRecheckIsBeforeNow()
        {
            // Http context
            var recheck = DateTime.UtcNow.AddMinutes(-2);
            Helpers.injectMnoSession(recheck);
            var httpSession = Helpers.FakeHttpSessionState();

            // test
            Session mnoSession = new Session(httpSession);
            Assert.IsTrue(mnoSession.isRemoteCheckRequired());
        }
Exemple #4
0
 /// <summary>
 /// Set the maestrano user in session
 /// </summary>
 public void SetSession(HttpSessionState httpSessionObj, User user)
 {
     var mnoSession = new Session(httpSessionObj, user);
     mnoSession.Save();
 }
        /// <summary>
        /// Scope a Session to a specific configuration preset
        /// </summary>
        /// <param name="presetName"></param>
        /// <returns></returns>
        public static Session With(string presetName = "maestrano")
        {
            Session scopedSession = new Session((HttpSessionStateBase)null);
            scopedSession.presetName = presetName;

            return scopedSession;
        }
        public void IsValid_WhenRecheckRequiredAndInvalid_ItShouldReturnFalse()
        {
            // Response preparation
            RestResponse response = new RestResponse();
            var datetime = DateTime.UtcNow;
            JObject respObj = new JObject(new JProperty("valid", "false"), new JProperty("recheck", datetime.ToString("s")));
            response.Content = respObj.ToString();
            response.ResponseStatus = ResponseStatus.Completed;

            // Client mock
            var mockRestClient = new Mock<RestClient>();
            mockRestClient.Setup(c => c.Execute(It.IsAny<RestRequest>())).Returns(response);

            // Http context
            var recheck = DateTime.UtcNow.AddMinutes(-1);
            Helpers.injectMnoSession(recheck);
            var httpSession = Helpers.FakeHttpSessionState();

            // test
            Session mnoSession = new Session(httpSession);
            Assert.IsFalse(mnoSession.IsValid(mockRestClient.Object));
        }
        public void Save_ItShouldSaveTheMaestranoSessionInHttpSession()
        {
            // Http context
            var recheck = DateTime.UtcNow.AddMinutes(1);
            Helpers.injectMnoSession(recheck);
            var httpSession = Helpers.FakeHttpSessionState();

            // Create Mno session and save it
            Session mnoSession = new Session(httpSession);
            mnoSession.SessionToken = "anothertoken";
            mnoSession.Save();

            // Decrypt session and test
            var enc = System.Text.Encoding.UTF8;
            var json = enc.GetString(Convert.FromBase64String(httpSession["maestrano"].ToString()));
            var mnoObj = JObject.Parse(json);

            Assert.AreEqual(mnoSession.SessionToken, mnoObj.Value<String>("session"));
            Assert.AreEqual(mnoSession.Uid, mnoObj.Value<String>("uid"));
            Assert.AreEqual(mnoSession.GroupUid, mnoObj.Value<String>("group_uid"));
            Assert.AreEqual(mnoSession.Recheck, mnoObj.Value<DateTime>("session_recheck"));
        }
        public void PerformRemoteCheck_WhenValid_ItShouldReturnTrueAndAssignRecheckIfValid()
        {
            // Response preparation
            RestResponse response = new RestResponse();
            var datetime = DateTime.UtcNow;
            JObject respObj = new JObject(new JProperty("valid", "true"), new JProperty("recheck", datetime.ToString("s")));
            response.Content = respObj.ToString();
            response.ResponseStatus = ResponseStatus.Completed;

            // Client mock
            var mockRestClient = new Mock<RestClient>();
            mockRestClient.Setup(c => c.Execute(It.IsAny<RestRequest>())).Returns(response);

            // Http context
            Helpers.injectMnoSession();
            var httpSession = Helpers.FakeHttpSessionState();
            Session mnoSession = new Session(httpSession);

            // Tests
            Assert.IsTrue(mnoSession.PerformRemoteCheck(mockRestClient.Object));
            Assert.AreEqual(DateTime.Parse(datetime.ToString("s")), mnoSession.Recheck);
        }
        public void ItContructsAnInstanceFromHttpSessionStateObjectAndSsoUser()
        {
            // Http context
            Helpers.injectMnoSession();
            var httpSession = Helpers.FakeHttpSessionState();

            // User
            var samlResp = new SsoResponseStub();
            var user = new User(samlResp);

            Session mnoSession = new Session(httpSession, user);

            Assert.AreEqual(httpSession, mnoSession.HttpSession);
            Assert.AreEqual(user.Uid, mnoSession.Uid);
            Assert.AreEqual(user.GroupUid, mnoSession.GroupUid);
            Assert.AreEqual(user.SsoSession, mnoSession.SessionToken);
            Assert.AreEqual(user.SsoSessionRecheck, mnoSession.Recheck);
        }
        public void ItContructsAnInstanceFromHttpSessionStateObject()
        {
            Helpers.injectMnoSession();
            Session mnoSession = new Session(Helpers.FakeHttpSessionState());

            Assert.AreEqual("usr-1", mnoSession.Uid);
            Assert.AreEqual("cld-1", mnoSession.GroupUid);
            Assert.AreEqual("sessiontoken", mnoSession.SessionToken);
            Assert.AreEqual(DateTime.Parse("2014-06-22T01:00:00Z").ToUniversalTime(), mnoSession.Recheck);
        }
        public void IsValid_WhenRecheckRequiredAndValid_ItShouldReturnTrueAndSaveTheSession()
        {
            // Response preparation
            RestResponse response = new RestResponse();
            var datetime = DateTime.UtcNow;
            JObject respObj = new JObject(
              new JProperty("valid", "true"),
              new JProperty("recheck", datetime.ToString("s"))
              );
            response.Content = respObj.ToString();
            response.ResponseStatus = ResponseStatus.Completed;

            // Client mock
            var mockRestClient = new Mock<RestClient>();
            mockRestClient.Setup(c => c.Execute(It.IsAny<RestRequest>())).Returns(response);

            // Http context
            var recheck = DateTime.UtcNow.AddMinutes(-1);
            Helpers.injectMnoSession(recheck);
            var httpSession = Helpers.FakeHttpSessionState();

            // Test mno session
            Session mnoSession = new Session(httpSession);
            Assert.IsTrue(mnoSession.IsValid(mockRestClient.Object));

            // Decrypt session and test recheck
            var enc = System.Text.Encoding.UTF8;
            var json = enc.GetString(Convert.FromBase64String(httpSession["maestrano"].ToString()));
            var mnoObj = JObject.Parse(json);

            Assert.AreEqual(datetime.ToString("s"), mnoObj.Value<DateTime>("session_recheck").ToString("s"));
        }