Example #1
0
        public void TestValidAuthenticationRequest()
        {
            MySqlDataManipulator manipulator = new MySqlDataManipulator();

            using (manipulator)
            {
                manipulator.Connect(TestingConstants.ConnectionString);
                Assert.IsTrue(NetTestingUserUtils.LogInTestingUser(TestingUserStorage.ValidUser2));
                OverallUser user              = manipulator.GetUsersWhere(string.Format("Email=\"{0}\"", TestingUserStorage.ValidUser2.Email))[0];
                var         loginTokens       = UserVerificationUtil.ExtractLoginTokens(user);
                var         authReq           = TestingUserStorage.ValidUser2.ConstructAuthenticationRequest(loginTokens.LoginToken, user.UserId);
                object[]    contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage(authReq, "PUT");
                var         ctx = contextAndRequest[0] as HttpListenerContext;
                var         req = contextAndRequest[1] as HttpWebRequest;
                TestApi.PUT(ctx);
                HttpWebResponse resp;
                try
                {
                    resp = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse;
                } catch (WebException e)
                {
                    resp = e.Response as HttpWebResponse;
                    byte[] respData = new byte[resp.ContentLength];
                    resp.GetResponseStream().Read(respData, 0, respData.Length);
                    Console.WriteLine(Encoding.UTF8.GetString(respData));
                    throw e;
                }
                Assert.AreEqual(HttpStatusCode.OK, resp.StatusCode);
            }
        }
        public static bool AuthenticateTestingUser(TestingUserStorage.TestingUser userIn, MySqlDataManipulator manipulatorIn)
        {
            UserAuthApi api = new UserAuthApi(10000);

            if (!LogInTestingUser(userIn))
            {
                return(false);
            }

            var databaseUser = manipulatorIn.GetUsersWhere(string.Format("Email=\"{0}\"", userIn.Email))[0];
            var loginTokens  = UserVerificationUtil.ExtractLoginTokens(databaseUser);

            object[] contextAndRequest = ServerTestingMessageSwitchback.SwitchbackMessage(
                userIn.ConstructAuthenticationRequest(loginTokens.LoginToken, databaseUser.UserId),
                "PUT");
            var ctx = contextAndRequest[0] as HttpListenerContext;
            var req = contextAndRequest[1] as HttpWebRequest;

            api.PUT(ctx);
            HttpWebResponse resp;

            try
            {
                resp = req.EndGetResponse(contextAndRequest[2] as IAsyncResult) as HttpWebResponse;
                return(true);
            }
            catch
            {
                return(false);
            }
        }