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());
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// DELETE request format located in the Web Api Enumeration v2
        /// under the tab Company/Parts, starting row 51
        /// </summary>
        /// <param name="ctx">HttpListenerContext to respond to</param>
        private void HandleDeleteRequest(HttpListenerContext ctx, CompanyPartsApiDeleteRequest req)
        {
            try
            {
                #region Input Validation
                if (!ValidateDeleteRequest(req))
                {
                    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(req.UserId);
                    if (mappedUser == null)
                    {
                        WriteBodyResponse(ctx, 404, "Not Found", "User was not found on on the server");
                        return;
                    }
                    if (!UserVerificationUtil.LoginTokenValid(mappedUser, req.LoginToken))
                    {
                        WriteBodyResponse(ctx, 401, "Not Authorized", "Login token was incorrect.");
                        return;
                    }
                    if (!UserVerificationUtil.AuthTokenValid(mappedUser, req.AuthToken))
                    {
                        WriteBodyResponse(ctx, 401, "Not Authorized", "Auth token was ezpired or incorrect");
                        return;
                    }
                    if ((mappedUser.AccessLevel & AccessLevelMasks.PartMask) == 0)
                    {
                        WriteBodyResponse(ctx, 401, "Not Autherized", "Not marked as a Parts User");
                        return;
                    }
                    #endregion

                    #region Delete Parts Request
                    if (connection.GetPartCatalogueEntryById(mappedUser.Company, req.PartEntryId) == null)
                    {
                        WriteBodyResponse(ctx, 404, "Not Found", "Part entry with the given id was not found");
                        return;
                    }

                    if (!connection.RemovePartCatalogueEntry(mappedUser.Company, req.PartEntryId))
                    {
                        WriteBodyResponse(ctx, 500, "Internal Server Error", "Error occured while removing part entry: " + 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);
            }
        }