コード例 #1
0
        public void TestBadRequestOnInvalidUserId()
        {
            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["UserId"] = 0;
                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.BadRequest, response.StatusCode);
            }
        }
コード例 #2
0
        public void TestDeserialize()
        {
            Assert.AreEqual(1, PartCatalogueEntry.Manipulator.InsertDataInto(TestConnection, TableName, Id1));
            PartCatalogueEntry toTest = PartCatalogueEntry.Manipulator.RetrieveDataWhere(TestConnection, TableName, "PartId=\"" + Id1.PartId + "\"")[0];

            Assert.AreEqual(Id1, toTest);
            Assert.AreEqual(1, PartCatalogueEntry.Manipulator.RemoveDataWhere(TestConnection, TableName, "PartId=\"" + Id1.PartId + "\""));
        }
コード例 #3
0
        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());
                    }
                }
            }
        }
コード例 #4
0
        private JsonDictionaryStringConstructor WritePartCatelogueEntryToOutput(PartCatalogueEntry entryOut)
        {
            JsonDictionaryStringConstructor ret = new JsonDictionaryStringConstructor();

            ret.SetMapping("Make", entryOut.Make);
            ret.SetMapping("Model", entryOut.Model);
            if (entryOut.Year == -1)
            {
                ret.SetMapping("Year", "Unknown");
            }
            else
            {
                ret.SetMapping("Year", entryOut.Year);
            }
            ret.SetMapping("PartId", entryOut.PartId);
            ret.SetMapping("PartName", entryOut.PartName);
            return(ret);
        }
コード例 #5
0
        /// <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);
            }
        }
コード例 #6
0
 public void Init()
 {
     Id1 = new PartCatalogueEntry("Autocar", "xpeditor", -1, "AX358", "Head Gasket");
     Id2 = new PartCatalogueEntry("Autocar", "xpeditor", 1998, "AX359", "Head Gasket");
     Id3 = new PartCatalogueEntry("Autocar", "xpeditor", -1, "AX358", "Head Gasket");
 }