public void TestServerListHappyPath() { // in an actual desktop app this will need to be setup as static var cookieContainer = new System.Net.CookieContainer(); using (var handler = new System.Net.Http.HttpClientHandler() { CookieContainer = cookieContainer }) using (var client = new System.Net.Http.HttpClient(handler)) { var header = new NameValueCollection(); header.Add("VpnAuthToken", Setup.token1); header.Add("VpnUserId", Setup.userid.ToString()); var mock = new Mock <Majorsilence.Vpn.Site.Helpers.ISessionVariables>(); mock.SetupAllProperties(); Majorsilence.Vpn.Site.Helpers.ISessionVariables sessionVars = mock.Object; var controller = new Majorsilence.Vpn.Site.Controllers.ApiV2Controller(sessionVars); FakeControllerContext.SetContext(controller, header); var data = controller.Servers(); Assert.That(controller.Response.StatusCode, Is.EqualTo((int)System.Net.HttpStatusCode.OK)); var deserializedContent = Newtonsoft.Json.JsonConvert.DeserializeObject <IEnumerable <Majorsilence.Vpn.Logic.Accounts.UserServerDetailsInfo> >(data.Content); Assert.That(deserializedContent.Count() > 1); var defaultVagrantServer = (from a in deserializedContent where a.Address == "127.0.0.1" && a.VpnServerName == "default vagrant testing vpn authority" select a); Assert.That(defaultVagrantServer.Count() == 1); } }
private void RetrieveLoginTokenAndAssert() { var peterAccount = new Majorsilence.Vpn.Logic.Accounts.CreateAccount( new Majorsilence.Vpn.Logic.Accounts.CreateAccountInfo() { Email = emailAddress, EmailConfirm = emailAddress, Firstname = "Peter", Lastname = "Gill", Password = password, PasswordConfirm = password, BetaKey = betaKey } , true, Majorsilence.Vpn.Logic.InitializeSettings.Email); userid = peterAccount.Execute(); // in an actual desktop app this will need to be setup as static var cookieContainer = new System.Net.CookieContainer(); using (var handler = new System.Net.Http.HttpClientHandler() { CookieContainer = cookieContainer }) using (var client = new System.Net.Http.HttpClient(handler)) { var byteArray = System.Text.Encoding.UTF8.GetBytes(string.Format("{0}:{1}", emailAddress, password)); var headerAuth = new System.Net.Http.Headers.AuthenticationHeaderValue( "Basic", Convert.ToBase64String(byteArray)); client.DefaultRequestHeaders.Authorization = headerAuth; var mock = new Mock <Majorsilence.Vpn.Site.Helpers.ISessionVariables>(); mock.SetupAllProperties(); Majorsilence.Vpn.Site.Helpers.ISessionVariables sessionVars = mock.Object; var controller = new Majorsilence.Vpn.Site.Controllers.ApiV2Controller(sessionVars); var header = new NameValueCollection(); header.Add("Authorization", headerAuth.ToString()); // See http://stephenwalther.com/archive/2008/07/01/asp-net-mvc-tip-12-faking-the-controller-context FakeControllerContext.SetContext(controller, header); var blah = controller.Auth(); //controller.Auth(); System.Console.WriteLine(blah.Content); // mock.VerifySet(framework => framework.IsAdmin = true); Assert.That(sessionVars.LoggedIn, Is.EqualTo(true)); Assert.That(sessionVars.Username, Is.EqualTo(emailAddress)); Assert.That(sessionVars.UserId, Is.EqualTo(userid)); Assert.That(sessionVars.IsAdmin, Is.EqualTo(true)); Assert.That(controller.Response.StatusCode, Is.EqualTo((int)System.Net.HttpStatusCode.OK)); var content = Newtonsoft.Json.JsonConvert.DeserializeObject <Majorsilence.Vpn.Logic.DTO.ApiAuthResponse>(blah.Content); Assert.That(string.IsNullOrEmpty(content.Token1), Is.EqualTo(false)); Assert.That(string.IsNullOrEmpty(content.Token2), Is.EqualTo(false)); Assert.That(content.Token1ExpireUtc, Is.GreaterThan(DateTime.UtcNow)); Assert.That(content.Token1ExpireUtc, Is.LessThan(DateTime.UtcNow.AddDays(1))); Assert.That(content.Token2ExpireUtc, Is.GreaterThan(DateTime.UtcNow.AddDays(1))); Assert.That(content.Token2ExpireUtc, Is.LessThan(DateTime.UtcNow.AddDays(2))); token1 = content.Token1; token2 = content.Token2; // See http://haacked.com/archive/2007/06/19/unit-tests-web-code-without-a-web-server-using-httpsimulator.aspx/ // var responseString = response.Content.ReadAsStringAsync(); } }