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]; 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.ConstructDeletionRequest( validUser1.UserId, loginTokens.LoginToken, loginTokens.AuthToken, entry.Id ); 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; } 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(0, addedEntryList.Count); } finally { if ( manipulator.GetPartCatalogueEntriesWhere(1, string.Format("PartId=\"{0}\"", TestingPartEntry.ValidPartEntry1.PartId) ).Count == 0 ) { Assert.IsTrue(manipulator.AddPartEntry(1, entry)); } else { Assert.Fail("Removal of part entry failed"); } } } }
/// <summary> /// POST request format located in the Web Api Enumeration v2 /// under the tab Company/Parts, starting row 1 /// </summary> /// <param name="ctx">HttpListenerContext to respond to</param> private void HandlePostRequest(HttpListenerContext ctx) { try { #region Input Validation if (!ctx.Request.HasEntityBody) { WriteBodyResponse(ctx, 400, "Bad Request", "No Body"); return; } CompanyPartsApiFullPostRequest entry = JsonDataObjectUtil <CompanyPartsApiFullPostRequest> .ParseObject(ctx); if (!ValidateFullPostRequest(entry)) { WriteBodyResponse(ctx, 400, "Bad Request", "Incorrect Format"); return; } #endregion MySqlDataManipulator connection = new MySqlDataManipulator(); using (connection) { bool res = connection.Connect(MySqlDataManipulator.GlobalConfiguration.GetConnectionString()); if (!res) { WriteBodyResponse(ctx, 500, "Unexpected Server Error", "Connection to database failed"); return; } #region User Validation OverallUser mappedUser = connection.GetUserById(entry.UserId); if (mappedUser == null) { WriteBodyResponse(ctx, 404, "Not Found", "User was not found on on the server"); return; } if (!UserVerificationUtil.LoginTokenValid(mappedUser, entry.LoginToken)) { WriteBodyResponse(ctx, 401, "Not Authorized", "Login token was incorrect."); return; } if (!UserVerificationUtil.AuthTokenValid(mappedUser, entry.AuthToken)) { WriteBodyResponse(ctx, 401, "Not Authorized", "Auth token was expired or incorrect"); return; } if ((mappedUser.AccessLevel & AccessLevelMasks.PartMask) == 0) { WriteBodyResponse(ctx, 401, "Not Authorized", "Not marked as a Parts User"); return; } #endregion #region Post Part PartCatalogueEntry ent = new PartCatalogueEntry(entry.Make, entry.Model, entry.Year, entry.PartId, entry.PartName); res = connection.AddPartEntry(mappedUser.Company, ent); if (!res) { WriteBodyResponse(ctx, 500, "Unexpected Server Error", connection.LastException.Message); return; } WriteBodylessResponse(ctx, 200, "OK"); #endregion } } catch (HttpListenerException) { //HttpListeners dispose themselves when an exception occurs, so we can do no more. } catch (Exception e) { WriteBodyResponse(ctx, 500, "Internal Server Error", e.Message); } }
public static bool InitializePartCatelogueEntries() { if (!DatabaseInitialized) { return(false); } MySqlDataManipulator manipulator = new MySqlDataManipulator(); if (!manipulator.Connect(TestingConstants.ConnectionString)) { return(false); } using (manipulator) { if (manipulator.GetPartCatalogueEntries(1).Count == 0) { if (!manipulator.AddPartEntry( 1, new PartCatalogueEntry() { Make = TestingPartEntry.ValidPartEntry1.Make, Model = TestingPartEntry.ValidPartEntry1.Model, Year = TestingPartEntry.ValidPartEntry1.Year, PartId = TestingPartEntry.ValidPartEntry1.PartId, PartName = TestingPartEntry.ValidPartEntry1.PartName } )) { Console.WriteLine(manipulator.LastException.Message); return(false); } if (!manipulator.AddPartEntry( 1, new PartCatalogueEntry() { Make = TestingPartEntry.ValidPartEntry2.Make, Model = TestingPartEntry.ValidPartEntry2.Model, Year = TestingPartEntry.ValidPartEntry2.Year, PartId = TestingPartEntry.ValidPartEntry2.PartId, PartName = TestingPartEntry.ValidPartEntry2.PartName } )) { Console.WriteLine(manipulator.LastException.Message); return(false); } if (!manipulator.AddPartEntry( 1, new PartCatalogueEntry() { Make = TestingPartEntry.ValidPartEntry3.Make, Model = TestingPartEntry.ValidPartEntry3.Model, Year = TestingPartEntry.ValidPartEntry3.Year, PartId = TestingPartEntry.ValidPartEntry3.PartId, PartName = TestingPartEntry.ValidPartEntry3.PartName } )) { Console.WriteLine(manipulator.LastException.Message); return(false); } } } return(true); }