// Imitates the successful login sequence, returns the session. private static Mock <IWebClient> SuccessfullyLogin(string multifactorPassword, ResponseOrException response, out Session session) { var webClient = SetupLogin(IterationsResponse, response); session = Fetcher.Login(Username, Password, ClientInfo, SetupUi(multifactorPassword), webClient.Object); return(webClient); }
private static void FetchAndVerifyException <TInnerExceptionType>(ResponseOrException responseOrException, FetchException.FailureReason reason, string message) { var webClient = SetupFetch(responseOrException); var exception = Assert.Throws <FetchException>(() => Fetcher.Fetch(Session, webClient.Object)); Assert.AreEqual(reason, exception.Reason); Assert.AreEqual(message, exception.Message); Assert.IsInstanceOf <TInnerExceptionType>(exception.InnerException); }
// Fail in login request and verify the exception. // Response-or-exception argument should either a string // with the provided response or an exception to be thrown. // The iterations request is not supposed to fail and it's // given a valid server response with the proper iteration count. private static void LoginAndVerifyExceptionInLoginRequest(ResponseOrException loginResponseOrException, LoginException.FailureReason reason, string message, Action <Exception> verifyInnerException) { LoginAndVerifyException(IterationsResponse, loginResponseOrException, reason, message, verifyInnerException); }
// Try to login and expect an exception, which is later validated by the caller. private static LoginException LoginAndFailWithException(string multifactorPassword, ResponseOrException iterationsResponseOrException, ResponseOrException loginResponseOrException = null) { var webClient = SetupLogin(iterationsResponseOrException, loginResponseOrException); return(Assert.Throws <LoginException>(() => Fetcher.Login(Username, Password, multifactorPassword, webClient.Object))); }
// Verify the session is correct. private static void LoginAndVerifySession(string multifactorPassword, ResponseOrException response, string expectedPrivateKey) { Session session; SuccessfullyLogin(multifactorPassword, response, out session); Assert.AreEqual(SessionId, session.Id); Assert.AreEqual(IterationCount, session.KeyIterationCount); Assert.AreEqual(expectedPrivateKey, session.EncryptedPrivateKey); }
// // Helpers // private static Mock <IWebClient> SetupFetch(ResponseOrException responseOrException, WebHeaderCollection headers = null) { var webClient = new Mock <IWebClient>(); webClient .SetupGet(x => x.Headers) .Returns(headers ?? new WebHeaderCollection()); responseOrException.ReturnOrThrow(webClient.Setup(x => x.DownloadData(It.IsAny <string>()))); return(webClient); }
// The most generic version. It expects on the requests to fail with an exception. // The exception is verified against the expectations. private static void LoginAndVerifyException(ResponseOrException iterationsResponseOrException, ResponseOrException loginResponseOrException, LoginException.FailureReason reason, string message, Action <Exception> verifyInnerException) { var exception = LoginAndFailWithException(NoMultifactorPassword, iterationsResponseOrException, loginResponseOrException); Assert.AreEqual(reason, exception.Reason); Assert.AreEqual(message, exception.Message); verifyInnerException(exception.InnerException); }
// Set up the login process. Response-or-exception parameters provide either // response or exception depending on the desired behavior. The login process // is two phase: request iteration count, then log in receive the session id. // Each of the stages might fail because of the network problems or some other // reason. private static Mock <IWebClient> SetupLogin(ResponseOrException iterationsResponseOrException, ResponseOrException loginResponseOrException = null) { var webClient = new Mock <IWebClient>(); var sequence = webClient.SetupSequence(x => x.UploadValues(It.IsAny <string>(), It.IsAny <NameValueCollection>())); iterationsResponseOrException.ReturnOrThrow(sequence); if (loginResponseOrException != null) { loginResponseOrException.ReturnOrThrow(sequence); } return(webClient); }
// // Helpers // private static Mock <IWebClient> SetupLogout(ResponseOrException responseOrException, WebHeaderCollection headers = null) { var webClient = new Mock <IWebClient>(); webClient .SetupGet(x => x.Headers) .Returns(headers ?? new WebHeaderCollection()); responseOrException.ReturnOrThrow( webClient.Setup(x => x.UploadValues(It.IsAny <string>(), It.IsAny <NameValueCollection>()))); return(webClient); }
// See the overload with an action. private static void LoginAndVerifyExceptionInLoginRequest(ResponseOrException loginResponseOrException, LoginException.FailureReason reason, string message) { LoginAndVerifyExceptionInLoginRequest(loginResponseOrException, reason, message, Assert.IsNull); }
// See the overload with an action. private static void LoginAndVerifyExceptionInLoginRequest <TInnerExceptionType>(ResponseOrException loginResponseOrException, LoginException.FailureReason reason, string message) { LoginAndVerifyExceptionInLoginRequest(loginResponseOrException, reason, message, Assert.IsInstanceOf <TInnerExceptionType>); }