public void TestUploadRepairJobsSimilarJobsGetList() { 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); manipulator.AddDataEntry(1, TestingRepairJobStorage.RepairJob1.CreateEntry(), true); manipulator.AddDataEntry(1, TestingRepairJobStorage.RepairJob2.CreateEntry(), false); object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage( TestingRepairJobStorage.SimilarJob1.ConstructCreationMessage( uploadingUser.UserId, loginTokens.LoginToken, 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; Assert.Fail("Expected an error message but never received one"); } catch (WebException e) { resp = e.Response as HttpWebResponse; string message = e.Message; } Assert.AreEqual(HttpStatusCode.Conflict, resp.StatusCode); } }
/// <summary> /// Constructs a <see cref="RepairJobEntry"/> from the fields of this object /// and adds it to the database in the specified location /// </summary> /// <param name="manipulator"><see cref="MySqlDataManipulator"/> used to add the <see cref="RepairJobEntry"/> to the database</param> public override void PerformFunction(MySqlDataManipulator manipulator) { RepairJobEntry e = new RepairJobEntry() { JobId = JobId, Make = Make, Model = Model, Complaint = Complaint, Problem = Problem }; if (!manipulator.AddDataEntry(CompanyId, e, IsValidated)) { Console.WriteLine("Failed to add data entry"); } Console.WriteLine("Successfully added data entry"); }
public override void PerformFunction(MySqlDataManipulator manipulator) { StreamReader fileReader = new StreamReader(FilePath); string jsonString = fileReader.ReadToEnd(); fileReader.Close(); List <RepairJobEntry> loadedData = JsonDataObjectUtil <List <RepairJobEntry> > .ParseObject(jsonString); foreach (RepairJobEntry entry in loadedData) { entry.Make = entry.Make.ToLower(); entry.Model = entry.Model.ToLower(); entry.Complaint = entry.Complaint.ToLower(); entry.Problem = entry.Problem.ToLower(); if (!manipulator.AddDataEntry(CompanyId, entry, validated: true)) { Console.WriteLine(JsonDataObjectUtil <RepairJobEntry> .ConvertObject(entry) + " was not added to the database"); } } }
/// <summary> /// Request for adding a repair job entry. Documention is found in the Web API Enumeration file /// in the /RepairJob tab, starting at row 1 /// </summary> /// <param name="ctx">The 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; } RepairJobApiRequest entry = JsonDataObjectUtil <RepairJobApiRequest> .ParseObject(ctx); if (!ValidateFullRequest(entry)) { WriteBodyResponse(ctx, 400, "Bad Request", "Incorrect Format"); return; } #endregion //Otherwise we have a valid entry, validate user MySqlDataManipulator connection = new MySqlDataManipulator(); using (connection) { bool res = connection.Connect(MySqlDataManipulator.GlobalConfiguration.GetConnectionString()); if (!res) { WriteBodyResponse(ctx, 500, "Unexpected ServerError", "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 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; } #endregion #region Input Sanitation if (entry.ContainedEntry.Complaint.Contains('<')) { WriteBodyResponse(ctx, 400, "Bad Request", "Request contained the < character, which is disallowed due to cross site scripting attacks"); return; } if (entry.ContainedEntry.Problem.Contains('<')) { WriteBodyResponse(ctx, 400, "Bad Request", "Request contained the < character, which is disallowed due to cross site scripting attacks"); return; } if (entry.ContainedEntry.Make.Contains('<')) { WriteBodyResponse(ctx, 400, "Bad Request", "Request contained the < character, which is disallowed due to cross site scripting attacks"); return; } if (entry.ContainedEntry.Model.Contains('<')) { WriteBodyResponse(ctx, 400, "Bad Request", "Request contained the < character, which is disallowed due to cross site scripting attacks"); return; } if (entry.ContainedEntry.JobId.Contains('<')) { WriteBodyResponse(ctx, 400, "Bad Request", "Request contained the < character, which is disallowed due to cross site scripting attacks"); return; } #endregion #region Action Handling #region Forced Upload if (!(entry.Duplicate == 0)) { //Now that we know the user is good, actually do the addition. res = connection.AddDataEntry(mappedUser.Company, entry.ContainedEntry); if (!res) { WriteBodyResponse(ctx, 500, "Unexpected Server Error", connection.LastException.Message); return; } WriteBodylessResponse(ctx, 200, "OK"); } #endregion else { //test if there exists similar string whereString = "Make =\"" + entry.ContainedEntry.Make + "\" AND " + "Model =\"" + entry.ContainedEntry.Model + "\""; //whereString += "AND"+entry.ContainedEntry.Year+">="+(entry.ContainedEntry.Year-2)+"AND"+entry.ContainedEntry.Year+"<="+(entry.ContainedEntry.Year+2); List <RepairJobEntry> dataCollectionsWhere = connection.GetDataEntriesWhere(mappedUser.Company, whereString, true); List <RepairJobEntry> data2 = connection.GetDataEntriesWhere(mappedUser.Company, whereString, false); foreach (RepairJobEntry x in data2) { dataCollectionsWhere.Add(x); } #region No Similar Jobs //if none force through if (dataCollectionsWhere.Count == 0) { res = connection.AddDataEntry(mappedUser.Company, entry.ContainedEntry); if (!res) { WriteBodyResponse(ctx, 500, "Unexpected Server Error", connection.LastException.Message); return; } WriteBodylessResponse(ctx, 200, "OK"); } #endregion #region Similar Jobs Return //if yes 409 with similar jobs else { JsonListStringConstructor retConstructor = new JsonListStringConstructor(); List <EntrySimilarity> ret = getSimilar(entry.ContainedEntry, dataCollectionsWhere, 3); if (ret.Count == 0) { res = connection.AddDataEntry(mappedUser.Company, entry.ContainedEntry); if (!res) { WriteBodyResponse(ctx, 500, "Unexpected Server Error", connection.LastException.Message); return; } WriteBodylessResponse(ctx, 200, "OK"); } ret.ForEach(obj => retConstructor.AddElement(ConvertEntrySimilarity(obj))); WriteBodyResponse(ctx, 409, "Conflict", retConstructor.ToString(), "application/json"); JsonDictionaryStringConstructor ConvertEntrySimilarity(EntrySimilarity e) { JsonDictionaryStringConstructor r = new JsonDictionaryStringConstructor(); r.SetMapping("Make", e.Entry.Make); r.SetMapping("Model", e.Entry.Model); r.SetMapping("Complaint", e.Entry.Complaint); r.SetMapping("Problem", e.Entry.Problem); if (e.Entry.Year == -1) { r.SetMapping("Year", "Unknown"); } else { r.SetMapping("Year", e.Entry.Year); } r.SetMapping("Id", e.Entry.Id); return(r); } } #endregion } #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); } }