示例#1
0
 @Issue("JENKINS-18641")
 @Test
 public void display() throws Exception {
   User.get("bob");
   JenkinsRule.WebClient wc = j.createWebClient();
   HtmlPage page;
   try {
     page = wc.goTo("asynchPeople");
   } catch (FailingHttpStatusCodeException x) {
     System.err.println(x.getResponse().getResponseHeaders());
     System.err.println(x.getResponse().getContentAsString());
     throw x;
   }
   assertEquals(0, wc.waitForBackgroundJavaScript(120000));
   boolean found = false;
   for (DomElement table : page.getElementsByTagName("table")) {
     if (table.getAttribute("class").contains("progress-bar")) {
       found = true;
       assertEquals("display: none;", table.getAttribute("style"));
       break;
     }
   }
   assertTrue(found);
   /* TODO this still fails occasionally, for reasons TBD (I think because User.getAll sometimes is empty):
   assertNotNull(page.getElementById("person-bob"));
   */
 }
 public String getParsedPage() {
   List<String> alertHandler = new LinkedList<String>();
   ;
   WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24); // CHROME);
   webClient.setAjaxController(new MyNicelyResynchronizingAjaxController());
   webClient.getOptions().setJavaScriptEnabled(true);
   webClient.getOptions().setTimeout(3500);
   webClient.getOptions().setThrowExceptionOnScriptError(true);
   webClient.getOptions().setCssEnabled(true);
   webClient.getOptions().isRedirectEnabled();
   webClient.setAlertHandler(
       new CollectingAlertHandler(alertHandler)); // 将JavaScript中alert标签产生的数据保存在一个链表中
   // webClient.getOptions().setThrowExceptionOnScriptError(false);
   HtmlPage page = null;
   JavaScriptEngine engine = new JavaScriptEngine(webClient);
   webClient.setJavaScriptEngine(engine);
   try {
     page = webClient.getPage(data);
   } catch (FailingHttpStatusCodeException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   } catch (MalformedURLException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   }
   if (page != null) {
     return page.asXml();
   }
   return null;
 }
  @Test
  public void testConfigurationAuthentication() throws Exception {
    prepareSecurity();

    FreeStyleProject p = j.createFreeStyleProject();

    WebClient wc = j.createWebClient();
    wc.login("test1");

    // Reauthentication is not required if No need for re-authentication is checked
    p.addProperty(
        new AuthorizeProjectProperty(new SpecificUsersAuthorizationStrategy("admin", true)));
    j.submit(wc.getPage(p, "configure").getFormByName("config"));

    // Reauthentication is required if No need for re-authentication is checked
    p.removeProperty(AuthorizeProjectProperty.class);
    p.addProperty(
        new AuthorizeProjectProperty(new SpecificUsersAuthorizationStrategy("admin", false)));
    try {
      j.submit(wc.getPage(p, "configure").getFormByName("config"));
      fail();
    } catch (FailingHttpStatusCodeException e) {
      assertEquals(400, e.getStatusCode());
    }

    // No authentication is required if oneself.
    {
      HtmlPage page = wc.getPage(p, "configure");
      HtmlTextInput userid =
          page.<HtmlTextInput>getFirstByXPath(
              "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'userid') and @type='text']");
      userid.setValueAttribute("test1");
      j.submit(page.getFormByName("config"));

      assertEquals(
          "test1",
          ((SpecificUsersAuthorizationStrategy)
                  p.getProperty(AuthorizeProjectProperty.class).getStrategy())
              .getUserid());
    }

    // Reauthentication is required to change userid even if No need for re-authentication is
    // checked
    p.addProperty(
        new AuthorizeProjectProperty(new SpecificUsersAuthorizationStrategy("admin", true)));
    {
      HtmlPage page = wc.getPage(p, "configure");
      HtmlTextInput userid =
          page.<HtmlTextInput>getFirstByXPath(
              "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'userid') and @type='text']");
      userid.setValueAttribute("test2");
      try {
        j.submit(page.getFormByName("config"));
        fail();
      } catch (FailingHttpStatusCodeException e) {
        assertEquals(400, e.getStatusCode());
      }
    }
  }
 /** Click Exception link. */
 public void testException() {
   setScriptingEnabled(false);
   beginAt(servletName);
   try {
     clickLinkWithText("Exception");
   } catch (FailingHttpStatusCodeException e) {
     assertEquals(500, e.getStatusCode());
   }
   assertTextPresent("MelatiBugMelatiException");
 }
示例#5
0
    @Issue("JENKINS-16608")
    @Test public void notAllowedName() throws Exception {
        HtmlForm form = j.createWebClient().goTo("newView").getFormByName("createItem");
        form.getInputByName("name").setValueAttribute("..");
        form.getRadioButtonsByName("mode").get(0).setChecked(true);

        try {
            j.submit(form);
            fail("\"..\" should not be allowed.");
        } catch (FailingHttpStatusCodeException e) {
            assertEquals(400, e.getStatusCode());
        }
    }
示例#6
0
 public static HtmlPage loadIndex() {
   System.out.println("Loading Index...");
   try {
     return wc.getPage("http://torn.com/index.php");
   } catch (FailingHttpStatusCodeException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   } catch (MalformedURLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
   return null;
 }
示例#7
0
    /**
     * Creating two views with the same name.
     */
    @Email("http://d.hatena.ne.jp/ssogabe/20090101/1230744150")
    @Test public void conflictingName() throws Exception {
        assertNull(j.jenkins.getView("foo"));

        HtmlForm form = j.createWebClient().goTo("newView").getFormByName("createItem");
        form.getInputByName("name").setValueAttribute("foo");
        form.getRadioButtonsByName("mode").get(0).setChecked(true);
        j.submit(form);
        assertNotNull(j.jenkins.getView("foo"));

        // do it again and verify an error
        try {
            j.submit(form);
            fail("shouldn't be allowed to create two views of the same name.");
        } catch (FailingHttpStatusCodeException e) {
            assertEquals(400, e.getStatusCode());
        }
    }
  private void ProcessTest(WebTest test) {
    HtmlPage page = null;

    // Orville.LOG().info(String.format("Executing test %s\n", event));

    WebClient webClient = new WebClient();
    webClient.setRefreshHandler(new ThreadedRefreshHandler());
    webClient.getOptions().setJavaScriptEnabled(false);

    try {
      if (test.getMethod().toLowerCase().equals("post")) {
        WebRequest request =
            new WebRequest(UrlUtils.toUrlUnsafe(test.getUrlString()), HttpMethod.POST);

        request.setRequestParameters(new ArrayList<NameValuePair>());

        for (Map.Entry<String, String> entry : test.getPostData().entrySet()) {
          request.getRequestParameters().add(new NameValuePair(entry.getKey(), entry.getValue()));
        }

        page = webClient.getPage(request);
      } else {
        page = webClient.getPage(test.getUrlString());
      }

      ProcessResult(test, page.getWebResponse());

    } catch (FailingHttpStatusCodeException fsc) {
      Orville.LOG()
          .warning(
              String.format(
                  "Failing HTTP Status code caught executing the test command: %s\n",
                  fsc.getMessage()));
    } catch (IOException ioe) {
      Orville.LOG()
          .warning(
              String.format(
                  "IO Exception caught executing the test command: %s\n", ioe.getMessage()));
    }

    webClient.closeAllWindows();
  }
  @Test
  public void testConfigureApitoken() throws Exception {
    prepareSecurity();

    FreeStyleProject p = j.createFreeStyleProject();
    p.addProperty(
        new AuthorizeProjectProperty(new SpecificUsersAuthorizationStrategy("test2", false)));

    WebClient wc = j.createWebClient();
    wc.login("test1");

    String apitokenForTest2 = User.get("test2").getProperty(ApiTokenProperty.class).getApiToken();
    assertNotNull(apitokenForTest2);
    assertNotEquals("", apitokenForTest2);

    // authentication fails without apitoken
    {
      HtmlPage page = wc.getPage(p, "configure");
      HtmlCheckBoxInput useApitoken =
          page.<HtmlCheckBoxInput>getFirstByXPath(
              "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'useApitoken') and @type='checkbox']");
      useApitoken.setChecked(true);
      try {
        j.submit(page.getFormByName("config"));
        fail();
      } catch (FailingHttpStatusCodeException e) {
        assertEquals(400, e.getStatusCode());
      }
    }

    // authentication succeeds with the good apitoken
    {
      HtmlPage page = wc.getPage(p, "configure");
      HtmlCheckBoxInput useApitoken =
          page.<HtmlCheckBoxInput>getFirstByXPath(
              "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'useApitoken') and @type='checkbox']");
      useApitoken.setChecked(true);
      HtmlTextInput apitoken =
          page.<HtmlTextInput>getFirstByXPath(
              "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'apitoken') and @type='text']");
      apitoken.setValueAttribute(apitokenForTest2);
      j.submit(page.getFormByName("config"));

      assertEquals(
          "test2",
          ((SpecificUsersAuthorizationStrategy)
                  p.getProperty(AuthorizeProjectProperty.class).getStrategy())
              .getUserid());
    }

    // authentication fails with a bad apitoken
    {
      HtmlPage page = wc.getPage(p, "configure");
      HtmlCheckBoxInput useApitoken =
          page.<HtmlCheckBoxInput>getFirstByXPath(
              "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'useApitoken') and @type='checkbox']");
      useApitoken.setChecked(true);
      HtmlTextInput apitoken =
          page.<HtmlTextInput>getFirstByXPath(
              "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'apitoken') and @type='text']");
      apitoken.setValueAttribute(apitokenForTest2 + "xxx");
      try {
        j.submit(page.getFormByName("config"));
        fail();
      } catch (FailingHttpStatusCodeException e) {
        assertEquals(400, e.getStatusCode());
      }
    }

    // authentication fails if the apitoken is used for password
    {
      HtmlPage page = wc.getPage(p, "configure");
      HtmlCheckBoxInput useApitoken =
          page.<HtmlCheckBoxInput>getFirstByXPath(
              "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'useApitoken') and @type='checkbox']");
      useApitoken.setChecked(false);
      HtmlPasswordInput password =
          page.<HtmlPasswordInput>getFirstByXPath(
              "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'password') and @type='password']");
      password.setValueAttribute(apitokenForTest2);
      HtmlTextInput apitoken =
          page.<HtmlTextInput>getFirstByXPath(
              "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'apitoken') and @type='text']");
      apitoken.setValueAttribute(apitokenForTest2);
      try {
        j.submit(page.getFormByName("config"));
        fail();
      } catch (FailingHttpStatusCodeException e) {
        assertEquals(400, e.getStatusCode());
      }
    }
  }