private void SetUp(object sender, PreWebTestEventArgs e)
 {
     try
     {
         MTApiFunctionalities mtApi = new MTApiFunctionalities();
         JObject userInfo           = mtApi.GenerateUserInfo();
         username = userInfo["username"].ToString();
         password = userInfo["password"].ToString();
         email    = userInfo["email"].ToString();
         HttpWebResponse httpResCreate = mtApi.CreateUser(mtUrl, username, password, email);
         httpResCreate.Close();
         HttpWebResponse httpResLogin      = mtApi.LoginUser(mtUrl, username, password);
         JObject         jsonResponseLogin = mtApi.JsonParseHttpRes(httpResLogin);
         userId = jsonResponseLogin["id"].ToString();
         httpResLogin.Close();
         HttpWebResponse httpResAdminLogin      = mtApi.LoginUser(mtUrl, adminUsername, adminPassword);
         JObject         jsonResponseAdminLogin = mtApi.JsonParseHttpRes(httpResAdminLogin);
         adminLoginToken = jsonResponseAdminLogin["token"].ToString();
         httpResAdminLogin.Close();
     }
     catch (WebException webExc)
     {
         Stop();                           // Stop test on exception
         Outcome           = Outcome.Fail; // Fail web test due to exception
         this.PostWebTest += TearDown;     // Add TearDown to make sure if user exists, it gets deleted
     }
 }
Exemple #2
0
        private void TearDown(object sender, PostWebTestEventArgs e)
        {
            MTApiFunctionalities mtApi        = new MTApiFunctionalities();
            HttpWebResponse      httpResLogin = mtApi.LoginUser(mtUrl, username, password);
            JObject jsonResponse = mtApi.JsonParseHttpRes(httpResLogin);
            string  userId       = jsonResponse["id"].ToString();

            httpResLogin.Close();
            HttpWebResponse httpResAdminLogin      = mtApi.LoginUser(mtUrl, adminUsername, adminPassword); // Login as admin to get admin token to delete user
            JObject         jsonResponseAdminLogin = mtApi.JsonParseHttpRes(httpResAdminLogin);
            string          adminLoginToken        = jsonResponseAdminLogin["token"].ToString();

            httpResAdminLogin.Close();
            HttpWebResponse httpResDel = mtApi.DeleteUser(mtUrl, userId, adminLoginToken);

            httpResDel.Close();
        }
        private void TearDown(object sender, PostWebTestEventArgs e)
        {
            MTApiFunctionalities mtApi = new MTApiFunctionalities();

            if (string.IsNullOrEmpty(userId)) // When SetUp fails and didn't save userId
            {
                HttpWebResponse httpResLogin = mtApi.LoginUser(mtUrl, username, password);
                JObject         jsonResponse = mtApi.JsonParseHttpRes(httpResLogin);
                userId = jsonResponse["id"].ToString();
                httpResLogin.Close();
            }
            HttpWebResponse httpResAdminLogin      = mtApi.LoginUser(mtUrl, adminUsername, adminPassword); // Login as admin to get admin token to delete user
            JObject         jsonResponseAdminLogin = mtApi.JsonParseHttpRes(httpResAdminLogin);

            adminLoginToken = jsonResponseAdminLogin["token"].ToString();
            httpResAdminLogin.Close();
            HttpWebResponse httpResDel = mtApi.DeleteUser(mtUrl, userId, adminLoginToken);

            httpResDel.Close();
        }