public void TestBadRequestInvalidPassword() { var loginMessage = TestingUserStorage.ValidUser1.ConstructLoginRequest(); loginMessage.SetMapping("Password", ""); object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage( loginMessage, "PUT"); var ctx = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; TestApi.PUT(ctx); HttpWebResponse resp; try { resp = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; Assert.Fail("Expected the response to be an error response. This was not the case."); } catch (WebException e) { resp = e.Response as HttpWebResponse; } Assert.AreEqual(HttpStatusCode.BadRequest, resp.StatusCode); }
public void LoginNonExistantUser() { try { using (MySqlDataManipulator manipulator = new MySqlDataManipulator()) { manipulator.Connect(TestingConstants.ConnectionString); Assert.IsTrue(manipulator.RemoveUserByEmail(TestingUserStorage.ValidUser2.Email)); object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage( TestingUserStorage.ValidUser2.ConstructLoginRequest(), "PUT"); var ctx = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; TestApi.PUT(ctx); HttpWebResponse resp; try { resp = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; Assert.Fail("Expected the response to be an error response. This was not the case."); } catch (WebException e) { resp = e.Response as HttpWebResponse; } Assert.AreEqual(HttpStatusCode.NotFound, resp.StatusCode); } } finally { TestingDatabaseCreationUtils.InitializeUsers(); } }
public void TestRetrieveRedirectHtml() { object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage( new JsonDictionaryStringConstructor(), "GET"); var ctx = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; TestApi.GET(ctx); HttpWebResponse resp = null; try { resp = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; } catch (Exception e) { Assert.Fail(e.Message); } using (resp) { Assert.AreEqual(HttpStatusCode.OK, resp.StatusCode); byte[] data = new byte[resp.ContentLength]; resp.GetResponseStream().Read(data, 0, data.Length); string received = Encoding.UTF8.GetString(data); Assert.AreEqual(ExpectedHtml, received); } }
public void TestRetrieveUserSettingsDefaultValues() { using (MySqlDataManipulator manipulator = new MySqlDataManipulator()) { manipulator.Connect(TestingConstants.ConnectionString); Assert.IsTrue(NetTestingUserUtils.LogInTestingUser(TestingUserStorage.ValidUser1)); var user = manipulator.GetUsersWhere(string.Format("Email=\"{0}\"", TestingUserStorage.ValidUser1.Email))[0]; var currentSettings = user.Settings; object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage( TestingUserStorage.ValidUser1.ConstructRetrieveSettingsRequest( user.UserId, UserVerificationUtil.ExtractLoginTokens(user).LoginToken), "PUT"); var ctx = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; TestApi.PUT(ctx); HttpWebResponse resp = null; try { resp = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; } catch (Exception e) { Assert.Fail(e.Message); } using (resp) { Assert.AreEqual(HttpStatusCode.OK, resp.StatusCode); byte[] data = new byte[resp.ContentLength]; resp.GetResponseStream().Read(data, 0, data.Length); string received = Encoding.UTF8.GetString(data); Assert.AreEqual(currentSettings, received); } } }
public void TestValidAuthenticationRequest() { MySqlDataManipulator manipulator = new MySqlDataManipulator(); using (manipulator) { manipulator.Connect(TestingConstants.ConnectionString); Assert.IsTrue(NetTestingUserUtils.AuthenticateTestingUser(TestingUserStorage.ValidUser2, manipulator)); OverallUser user = manipulator.GetUsersWhere(string.Format("Email=\"{0}\"", TestingUserStorage.ValidUser2.Email))[0]; var loginTokens = UserVerificationUtil.ExtractLoginTokens(user); var authReq = TestingUserStorage.ValidUser2.ConstructCheckAuthenticationStatusRequest(loginTokens.LoginToken, loginTokens.AuthToken, user.UserId); object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage(authReq, "PUT"); var ctx = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; TestApi.PUT(ctx); HttpWebResponse resp; try { resp = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; } catch (WebException e) { resp = e.Response as HttpWebResponse; byte[] respData = new byte[resp.ContentLength]; resp.GetResponseStream().Read(respData, 0, respData.Length); Console.WriteLine(Encoding.UTF8.GetString(respData)); throw e; } Assert.AreEqual(HttpStatusCode.OK, resp.StatusCode); } }
public void TestUnauthorizedOnNonLoggedInUser() { MySqlDataManipulator manipulator = new MySqlDataManipulator(); Assert.IsTrue(manipulator.Connect(TestingConstants.ConnectionString)); using (manipulator) { PartCatalogueEntry entry = manipulator.GetPartCatalogueEntriesWhere(1, string.Format("PartId=\"{0}\"", TestingPartEntry.ValidPartEntry1.PartId) )[0]; Assert.IsTrue(NetTestingUserUtils.AuthenticateTestingUser(TestingUserStorage.ValidUser3, manipulator)); OverallUser validUser1 = manipulator.GetUsersWhere( string.Format("Email=\"{0}\"", TestingUserStorage.ValidUser3.Email) )[0]; var loginTokens = UserVerificationUtil.ExtractLoginTokens(validUser1); var message = TestingPartEntry.ValidPartEntry1.ConstructDeletionRequest( validUser1.UserId, loginTokens.LoginToken, loginTokens.AuthToken, entry.Id ); message["LoginToken"] = "x'abacbadabac'"; object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage( message, "PATCH" ); var context = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; TestApi.PATCH(context); HttpWebResponse response; try { response = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; Assert.Fail("Expected and error response, but did not receive one"); } catch (WebException e) { response = e.Response as HttpWebResponse; } Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode); } }
public void TestUnauthorizedOnNonLoggedInUser() { using (MySqlDataManipulator manipulator = new MySqlDataManipulator()) { manipulator.Connect(TestingConstants.ConnectionString); Assert.IsTrue(NetTestingUserUtils.LogInTestingUser(TestingUserStorage.ValidUser1)); var user = manipulator.GetUsersWhere(string.Format("Email=\"{0}\"", TestingUserStorage.ValidUser1.Email))[0]; List <PreviousUserRequest> currentRequests = user.DecodeRequests(); object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage( TestingUserStorage.ValidUser1.ConstructRetrievePreviousRequestsRequest( user.UserId, "x'ababbbaacbaba'"), "PUT"); var ctx = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; TestApi.PUT(ctx); HttpWebResponse resp = null; try { resp = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; Assert.Fail("Expected an error response, but did not receive one"); } catch (WebException e) { resp = e.Response as HttpWebResponse; } Assert.AreEqual(HttpStatusCode.Unauthorized, resp.StatusCode); } }
public void TestBadRequestOnAllEmptyFields() { try { MySqlDataManipulator manipulator = new MySqlDataManipulator(); using (manipulator) { manipulator.Connect(TestingConstants.ConnectionString); Assert.IsTrue(manipulator.RemoveUserByEmail(TestingUserStorage.ValidUser1.Email)); var creationMessage = new JsonDictionaryStringConstructor(); object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage( creationMessage, "POST"); var ctx = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; HttpWebResponse resp; TestApi.POST(ctx); try { resp = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; } catch (WebException e) { resp = e.Response as HttpWebResponse; } Assert.AreEqual(HttpStatusCode.BadRequest, resp.StatusCode); } } finally { TestingDatabaseCreationUtils.InitializeUsers(); } }
public void BadRequestOnInvalidUserId() { MySqlDataManipulator manipulator = new MySqlDataManipulator(); using (manipulator) { manipulator.Connect(TestingConstants.ConnectionString); Assert.IsTrue(NetTestingUserUtils.AuthenticateTestingUser(TestingUserStorage.ValidUser2, manipulator)); OverallUser user = manipulator.GetUsersWhere(string.Format("Email=\"{0}\"", TestingUserStorage.ValidUser2.Email))[0]; var loginTokens = UserVerificationUtil.ExtractLoginTokens(user); var authReq = TestingUserStorage.ValidUser2.ConstructCheckAuthenticationStatusRequest(loginTokens.LoginToken, loginTokens.AuthToken, user.UserId); authReq.SetMapping("UserId", 0); object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage(authReq, "PUT"); var ctx = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; TestApi.PUT(ctx); HttpWebResponse resp; try { resp = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; Assert.Fail(); } catch (WebException e) { resp = e.Response as HttpWebResponse; } Assert.AreEqual(HttpStatusCode.BadRequest, resp.StatusCode); } }
public void TestNotFoundOnNonExistentUser() { using (MySqlDataManipulator manipulator = new MySqlDataManipulator()) { manipulator.Connect(TestingConstants.ConnectionString); NetTestingUserUtils.AuthenticateTestingUser(TestingUserStorage.ValidUser1, manipulator); var reportingUser = manipulator.GetUsersWhere( string.Format("Email=\"{0}\"", TestingUserStorage.ValidUser1.Email) )[0]; var loginTokens = UserVerificationUtil.ExtractLoginTokens(reportingUser); object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage( TestingUserStorage.ValidUser1.ConstructReportMessage( 10000000, loginTokens.LoginToken, loginTokens.AuthToken, "TerribleName" ), "POST" ); var ctx = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; HttpWebResponse resp = null; TestApi.POST(ctx); try { resp = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; Assert.Fail("Expected an error message but never received one"); } catch (WebException e) { resp = e.Response as HttpWebResponse; } Assert.AreEqual(HttpStatusCode.NotFound, resp.StatusCode); } }
public void TestNotAuthorizedOnNonLoggedUser() { using (MySqlDataManipulator manipulator = new MySqlDataManipulator()) { manipulator.Connect(TestingConstants.ConnectionString); var user = manipulator.GetUsersWhere(string.Format("Email=\"{0}\"", TestingUserStorage.ValidUser1.Email))[0]; object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage( TestingUserStorage.ValidUser1.ConstructSecurityQuestionRequest("x'abaababaaababaaba'", user.UserId), "POST"); var ctx = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; HttpWebResponse resp; TestApi.POST(ctx); try { resp = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; Assert.Fail("Expected an error message, but didn't receive one."); } catch (WebException e) { resp = e.Response as HttpWebResponse; } Assert.AreEqual(HttpStatusCode.Unauthorized, resp.StatusCode); } }
public void TestValidRequest() { using (MySqlDataManipulator manipulator = new MySqlDataManipulator()) { manipulator.Connect(TestingConstants.ConnectionString); Assert.IsTrue(NetTestingUserUtils.LogInTestingUser(TestingUserStorage.ValidUser1)); var user = manipulator.GetUsersWhere(string.Format("Email=\"{0}\"", TestingUserStorage.ValidUser1.Email))[0]; var loginTokens = UserVerificationUtil.ExtractLoginTokens(user); object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage( TestingUserStorage.ValidUser1.ConstructSecurityQuestionRequest(loginTokens.LoginToken, user.UserId), "POST"); var ctx = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; HttpWebResponse resp; TestApi.POST(ctx); try { resp = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; } catch (WebException e) { resp = e.Response as HttpWebResponse; byte[] respData = new byte[resp.ContentLength]; resp.GetResponseStream().Read(respData, 0, respData.Length); Console.WriteLine(Encoding.UTF8.GetString(respData)); throw e; } byte[] data = new byte[resp.ContentLength]; resp.GetResponseStream().Read(data, 0, data.Length); string receivedData = Encoding.UTF8.GetString(data); Assert.AreEqual(HttpStatusCode.OK, resp.StatusCode); Assert.AreEqual(TestingUserStorage.ValidUser1.SecurityQuestion, receivedData); } }
public void TestNotFoundOnNonExistentUser() { using (MySqlDataManipulator manipulator = new MySqlDataManipulator()) { manipulator.Connect(TestingConstants.ConnectionString); Assert.IsTrue(NetTestingUserUtils.LogInTestingUser(TestingUserStorage.ValidUser1)); var user = manipulator.GetUsersWhere(string.Format("Email=\"{0}\"", TestingUserStorage.ValidUser1.Email))[0]; var currentSettings = user.Settings; object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage( TestingUserStorage.ValidUser1.ConstructRetrieveSettingsRequest( 100000, UserVerificationUtil.ExtractLoginTokens(user).LoginToken), "PUT"); var ctx = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; TestApi.PUT(ctx); HttpWebResponse resp = null; try { resp = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; Assert.Fail("Expected error response, but did not receive one"); } catch (WebException e) { resp = e.Response as HttpWebResponse; } Assert.AreEqual(HttpStatusCode.NotFound, resp.StatusCode); } }
public void TestUploadRepairJobSimilarJobsForced() { using (MySqlDataManipulator manipulator = new MySqlDataManipulator()) { manipulator.Connect(TestingConstants.ConnectionString); NetTestingUserUtils.AuthenticateTestingUser(TestingUserStorage.ValidUser1, manipulator); var uploadingUser = manipulator.GetUsersWhere( string.Format("Email=\"{0}\"", TestingUserStorage.ValidUser1.Email) )[0]; var loginTokens = UserVerificationUtil.ExtractLoginTokens(uploadingUser); object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage( TestingRepairJobStorage.RepairJob1.ConstructCreationMessage( uploadingUser.UserId, loginTokens.LoginToken, loginTokens.AuthToken, 1 ), "POST" ); var ctx = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; HttpWebResponse resp = null; TestApi.POST(ctx); try { resp = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; } catch (WebException) { Assert.Fail("Received an error message when one was not expected"); } Assert.AreEqual(HttpStatusCode.OK, resp.StatusCode); } }
public void TestUnauthorizedOnNotLoggedInUser() { using (MySqlDataManipulator manipulator = new MySqlDataManipulator()) { manipulator.Connect(TestingConstants.ConnectionString); NetTestingUserUtils.AuthenticateTestingUser(TestingUserStorage.ValidUser1, manipulator); var uploadingUser = manipulator.GetUsersWhere( string.Format("Email=\"{0}\"", TestingUserStorage.ValidUser1.Email) )[0]; var loginTokens = UserVerificationUtil.ExtractLoginTokens(uploadingUser); object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage( TestingRepairJobStorage.RepairJob1.ConstructCreationMessage( uploadingUser.UserId, "I'm Logged-In I Swear!", loginTokens.AuthToken, 0 ), "POST" ); var ctx = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; HttpWebResponse resp = null; TestApi.POST(ctx); try { resp = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; } catch (WebException e) { resp = e.Response as HttpWebResponse; string message = e.Message; } Assert.AreEqual(HttpStatusCode.Unauthorized, resp.StatusCode); } }
public void TestBadRequestOnInvalidUserId() { MySqlDataManipulator manipulator = new MySqlDataManipulator(); Assert.IsTrue(manipulator.Connect(TestingConstants.ConnectionString)); using (manipulator) { Assert.IsTrue(NetTestingUserUtils.AuthenticateTestingUser(TestingUserStorage.ValidUser3, manipulator)); OverallUser validUser1 = manipulator.GetUsersWhere( string.Format("Email=\"{0}\"", TestingUserStorage.ValidUser3.Email) )[0]; var loginTokens = UserVerificationUtil.ExtractLoginTokens(validUser1); var message = TestingPartEntry.ValidPartEntry1.ConstructAdditionRequest( validUser1.UserId, loginTokens.LoginToken, loginTokens.AuthToken ); message["UserId"] = 0; object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage( message, "POST" ); var context = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; TestApi.POST(context); HttpWebResponse response; try { response = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; Assert.Fail("Expected an error response, but did not receive one"); } catch (WebException e) { response = e.Response as HttpWebResponse; } Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); } }
public void TestValidRequest() { MySqlDataManipulator manipulator = new MySqlDataManipulator(); Assert.IsTrue(manipulator.Connect(TestingConstants.ConnectionString)); using (manipulator) { PartCatalogueEntry entry = manipulator.GetPartCatalogueEntriesWhere(1, string.Format("PartId=\"{0}\"", TestingPartEntry.ValidPartEntry1.PartId) )[0]; Assert.IsTrue(manipulator.RemovePartCatalogueEntry( 1, entry.Id )); var entryList = manipulator.GetPartCatalogueEntriesWhere( 1, string.Format("PartId=\"{0}\"", TestingPartEntry.ValidPartEntry1.PartId) ); Assert.AreEqual(0, entryList.Count); try { Assert.IsTrue(NetTestingUserUtils.AuthenticateTestingUser(TestingUserStorage.ValidUser3, manipulator)); OverallUser validUser1 = manipulator.GetUsersWhere( string.Format("Email=\"{0}\"", TestingUserStorage.ValidUser3.Email) )[0]; var loginTokens = UserVerificationUtil.ExtractLoginTokens(validUser1); var message = TestingPartEntry.ValidPartEntry1.ConstructAdditionRequest( validUser1.UserId, loginTokens.LoginToken, loginTokens.AuthToken ); object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage( message, "POST" ); var context = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; TestApi.POST(context); HttpWebResponse response; try { response = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; } catch (WebException e) { response = e.Response as HttpWebResponse; Assert.Fail("Server sent back an error response: {0}", response.StatusCode); } Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); var addedEntryList = manipulator.GetPartCatalogueEntriesWhere( 1, string.Format("PartId=\"{0}\"", TestingPartEntry.ValidPartEntry1.PartId) ); Assert.AreEqual(1, addedEntryList.Count); } finally { if ( manipulator.GetPartCatalogueEntriesWhere(1, string.Format("PartId=\"{0}\"", TestingPartEntry.ValidPartEntry1.PartId) ).Count == 0 ) { Assert.IsTrue(TestingDatabaseCreationUtils.InitializePartCatelogueEntries()); } } } }
public void TestValidReport() { using (MySqlDataManipulator manipulator = new MySqlDataManipulator()) { manipulator.Connect(TestingConstants.ConnectionString); var reportedUser = manipulator.GetUsersWhere(string.Format("Email=\"{0}\"", TestingUserStorage.ValidUser2.Email))[0]; try { reportedUser.UpdateSettings(UserSettingsEntryKeys.DisplayName, "TerribleName"); Assert.IsTrue(manipulator.UpdateUsersSettings(reportedUser)); NetTestingUserUtils.AuthenticateTestingUser(TestingUserStorage.ValidUser1, manipulator); var reportingUser = manipulator.GetUsersWhere( string.Format("Email=\"{0}\"", TestingUserStorage.ValidUser1.Email) )[0]; var loginTokens = UserVerificationUtil.ExtractLoginTokens(reportingUser); object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage( TestingUserStorage.ValidUser1.ConstructReportMessage( reportingUser.UserId, loginTokens.LoginToken, loginTokens.AuthToken, "TerribleName" ), "POST" ); var ctx = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; HttpWebResponse resp = null; TestApi.POST(ctx); try { resp = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; } catch (WebException) { Assert.Fail("Received an error message when one was not expected"); } Assert.AreEqual(HttpStatusCode.OK, resp.StatusCode); reportedUser = manipulator.GetUsersWhere(string.Format("Email=\"{0}\"", TestingUserStorage.ValidUser2.Email))[0]; var reportedUserSettings = JsonDataObjectUtil <List <UserSettingsEntry> > .ParseObject(reportedUser.Settings); bool foundDisplayName = false; foreach (UserSettingsEntry entry in reportedUserSettings) { if (entry.Key == UserSettingsEntryKeys.DisplayName) { foundDisplayName = true; Assert.AreEqual("Default User " + reportedUser.UserId, entry.Value); break; } } Assert.IsTrue(foundDisplayName); } finally { reportedUser.Settings = OverallUser.GenerateDefaultSettings(); Assert.IsTrue(manipulator.UpdateUsersSettings(reportedUser)); } } }
public void TestRetrievePastRequests() { using (MySqlDataManipulator manipulator = new MySqlDataManipulator()) { manipulator.Connect(TestingConstants.ConnectionString); Assert.IsTrue(NetTestingUserUtils.LogInTestingUser(TestingUserStorage.ValidUser1)); var user = manipulator.GetUsersWhere(string.Format("Email=\"{0}\"", TestingUserStorage.ValidUser1.Email))[0]; List <PreviousUserRequest> currentRequests = user.DecodeRequests(); currentRequests.Add(new PreviousUserRequest() { Request = new RequestString() { Company = 1, Type = "TestingRequest" }, RequestStatus = "Completed" }); user.EncodeRequests(currentRequests); Assert.IsTrue(manipulator.UpdateUserPreviousRequests(user)); object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage( TestingUserStorage.ValidUser1.ConstructRetrievePreviousRequestsRequest( user.UserId, UserVerificationUtil.ExtractLoginTokens(user).LoginToken), "PUT"); var ctx = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; TestApi.PUT(ctx); HttpWebResponse resp = null; try { resp = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; } catch (Exception e) { Assert.Fail(e.Message); } using (resp) { Assert.AreEqual(HttpStatusCode.OK, resp.StatusCode); byte[] data = new byte[resp.ContentLength]; resp.GetResponseStream().Read(data, 0, data.Length); string received = Encoding.UTF8.GetString(data); var receivedRequests = JsonDataObjectUtil <List <PreviousUserRequest> > .ParseObject(received); Assert.AreEqual(currentRequests.Count, receivedRequests.Count); for (int i = 0; i < currentRequests.Count; i++) { Assert.AreEqual(currentRequests[i], receivedRequests[i]); } } } }
public void TestUpdateUserSetting() { using (MySqlDataManipulator manipulator = new MySqlDataManipulator()) { manipulator.Connect(TestingConstants.ConnectionString); Assert.IsTrue(NetTestingUserUtils.AuthenticateTestingUser(TestingUserStorage.ValidUser1, manipulator)); var user = manipulator.GetUsersWhere(string.Format("Email=\"{0}\"", TestingUserStorage.ValidUser1.Email))[0]; user.Settings = OverallUser.GenerateDefaultSettings(); Assert.IsTrue(manipulator.UpdateUsersSettings(user)); var currentSettings = user.Settings; var loginTokens = UserVerificationUtil.ExtractLoginTokens(user); object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage( TestingUserStorage.ValidUser1.ConstructChangeSettingRequest( user.UserId, loginTokens.LoginToken, loginTokens.AuthToken, UserSettingsEntryKeys.DisplayName, "New Name #2!"), "PATCH"); var ctx = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; TestApi.PATCH(ctx); HttpWebResponse resp = null; try { resp = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; } catch (Exception e) { Assert.Fail(e.Message); } using (resp) Assert.AreEqual(HttpStatusCode.OK, resp.StatusCode); user = manipulator.GetUsersWhere(string.Format("Email=\"{0}\"", TestingUserStorage.ValidUser1.Email))[0]; var newSettings = JsonDataObjectUtil <List <UserSettingsEntry> > .ParseObject(user.Settings); foreach (UserSettingsEntry entry in newSettings) { if (entry.Key == UserSettingsEntryKeys.DisplayName) { Assert.AreEqual("New Name #2!", entry.Value); break; } } } }
public void TestCreateValidUser1() { MySqlDataManipulator manipulator = new MySqlDataManipulator(); using (manipulator) { manipulator.Connect(TestingConstants.ConnectionString); Assert.IsTrue(manipulator.RemoveUserByEmail(TestingUserStorage.ValidUser1.Email)); object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage( TestingUserStorage.ValidUser1.ConstructCreationMessage(), "POST"); var ctx = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; TestApi.POST(ctx); HttpWebResponse resp; try { resp = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; } catch (WebException e) { resp = e.Response as HttpWebResponse; } try { Assert.AreEqual(HttpStatusCode.OK, resp.StatusCode); } catch (AssertFailedException e) { byte[] respData = new byte[resp.ContentLength]; resp.GetResponseStream().Read(respData, 0, respData.Length); Console.WriteLine(Encoding.UTF8.GetString(respData)); TestingDatabaseCreationUtils.InitializeUsers(); throw e; } var createdUser = manipulator.GetUsersWhere(string.Format("Email = \"{0}\"", TestingUserStorage.ValidUser1.Email)); Assert.IsNotNull(createdUser); Assert.AreEqual(1, createdUser.Count); Assert.AreEqual(TestingUserStorage.ValidUser1.Email, createdUser[0].Email); } }
public void TestBadRequestOnBadUserId() { object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage( TestingUserStorage.ValidUser1.ConstructCheckLoginStatusRequest(0, "x'acbad13475adbasbsdsa'"), "PUT"); var ctx = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; TestApi.PUT(ctx); HttpWebResponse resp; try { resp = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; Assert.Fail("Expected a failed response, but this did not occur"); } catch (WebException e) { resp = e.Response as HttpWebResponse; } Assert.AreEqual(HttpStatusCode.BadRequest, resp.StatusCode); }
public void TestBadFormatOnEmptyLoginToken() { object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage( TestingUserStorage.ValidUser1.ConstructSecurityQuestionRequest("", 1), "POST"); var ctx = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; HttpWebResponse resp; TestApi.POST(ctx); try { resp = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; Assert.Fail("Expected an error message, but didn't receive one."); } catch (WebException e) { resp = e.Response as HttpWebResponse; } Assert.AreEqual(HttpStatusCode.BadRequest, resp.StatusCode); }
public void TestConflictOnDuplicateUserCreation() { object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage( TestingUserStorage.ValidUser1.ConstructCreationMessage(), "POST"); var ctx = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; HttpWebResponse resp; TestApi.POST(ctx); try { resp = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; } catch (WebException e) { resp = e.Response as HttpWebResponse; } Assert.AreEqual(HttpStatusCode.Conflict, resp.StatusCode); }
public void TestLoginUser() { object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage( TestingUserStorage.ValidUser1.ConstructLoginRequest(), "PUT"); var ctx = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; TestApi.PUT(ctx); HttpWebResponse resp; try { resp = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; } catch (WebException e) { resp = e.Response as HttpWebResponse; } try { Assert.AreEqual(HttpStatusCode.OK, resp.StatusCode); } catch (AssertFailedException e) { byte[] respData = new byte[resp.ContentLength]; resp.GetResponseStream().Read(respData, 0, respData.Length); Console.WriteLine(Encoding.UTF8.GetString(respData)); throw e; } using (MySqlDataManipulator manipulator = new MySqlDataManipulator()) { manipulator.Connect(TestingConstants.ConnectionString); manipulator.UpdateUsersLoginToken( manipulator.GetUsersWhere( string.Format("Email=\"{0}\"", TestingUserStorage.ValidUser1.Email) )[0], new LoginStatusTokens()); } }
public void TestBadRequestOnInvalidUserId() { using (MySqlDataManipulator manipulator = new MySqlDataManipulator()) { manipulator.Connect(TestingConstants.ConnectionString); Assert.IsTrue(NetTestingUserUtils.AuthenticateTestingUser(TestingUserStorage.ValidUser1, manipulator)); var user = manipulator.GetUsersWhere(string.Format("Email=\"{0}\"", TestingUserStorage.ValidUser1.Email))[0]; user.Settings = OverallUser.GenerateDefaultSettings(); Assert.IsTrue(manipulator.UpdateUsersSettings(user)); var currentSettings = user.Settings; var loginTokens = UserVerificationUtil.ExtractLoginTokens(user); object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage( TestingUserStorage.ValidUser1.ConstructChangeSettingRequest( 0, loginTokens.LoginToken, loginTokens.AuthToken, UserSettingsEntryKeys.DisplayName, "New Name #2!"), "PATCH"); var ctx = contextAndRequest[0] as HttpListenerContext; var req = contextAndRequest[1] as HttpWebRequest; TestApi.PATCH(ctx); HttpWebResponse resp = null; try { resp = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse; Assert.Fail("Expected an error response, but did not receive one"); } catch (WebException e) { resp = e.Response as HttpWebResponse; } using (resp) Assert.AreEqual(HttpStatusCode.BadRequest, resp.StatusCode); } }