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 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 TestRetrieveUserSettingsModifiedSettings() { 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]; user.UpdateSettings(UserSettingsEntryKeys.DisplayName, "New Name!"); manipulator.UpdateUsersSettings(user); 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); user.Settings = OverallUser.GenerateDefaultSettings(); Assert.IsTrue(manipulator.UpdateUsersSettings(user)); 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 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); } }