Search in sources :

Example 6 with AccessToken

use of io.pivotal.cla.data.AccessToken in project pivotal-cla by pivotalsoftware.

the class GitHubHooksControllerTests method setupAccessToken.

@Before
public void setupAccessToken() {
    accessToken = new AccessToken(AccessToken.CLA_ACCESS_TOKEN_ID, "GitHubHooksControllerTests_access_token");
    when(mockTokenRepo.findOne(AccessToken.CLA_ACCESS_TOKEN_ID)).thenReturn(accessToken);
}
Also used : AccessToken(io.pivotal.cla.data.AccessToken) Before(org.junit.Before)

Example 7 with AccessToken

use of io.pivotal.cla.data.AccessToken in project pivotal-cla by pivotalsoftware.

the class GitHubHooksControllerTests method skipOwnRequests.

@Test
public void skipOwnRequests() throws Exception {
    User user = WithSigningUserFactory.create();
    when(mockUserRepo.findOne(anyString())).thenReturn(user);
    when(mockTokenRepo.findOne("rwinch/176_test")).thenReturn(new AccessToken("rwinch/176_test", "mock_access_token_value"));
    when(gitHubApiMock.getGitHubClaUserLogin()).thenReturn("robwinch");
    when(mockIndividualSignatureRepository.findSignaturesFor(any(), any(), anyString())).thenReturn(Arrays.asList(individualSignature));
    mockMvc.perform(hookRequest().header("X-GitHub-Event", GithubEvents.PULL_REQUEST_REVIEW_COMMENT).content(getPayload("pull_request_review_comment.json"))).andExpect(status().isOk());
    verify(gitHubApiMock).getGitHubClaUserLogin();
    verifyNoMoreInteractions(gitHubApiMock);
}
Also used : User(io.pivotal.cla.data.User) AccessToken(io.pivotal.cla.data.AccessToken) Test(org.junit.Test)

Example 8 with AccessToken

use of io.pivotal.cla.data.AccessToken in project pivotal-cla by pivotalsoftware.

the class GitHubHooksControllerTests method userNeverAuthenticated.

@Test
public void userNeverAuthenticated() throws Exception {
    when(mockTokenRepo.findOne("rwinch/176_test")).thenReturn(new AccessToken("rwinch/176_test", "mock_access_token_value"));
    mockMvc.perform(hookRequest().header("X-GitHub-Event", GithubEvents.PULL_REQUEST).content(getPayload("pull_request.json"))).andExpect(status().isOk());
    ArgumentCaptor<PullRequestStatus> statusCaptor = ArgumentCaptor.forClass(PullRequestStatus.class);
    verify(mockGitHub).save(statusCaptor.capture());
    PullRequestStatus status = statusCaptor.getValue();
    assertThat(status.getRepoId()).isEqualTo("rwinch/176_test");
    assertThat(status.getAccessToken()).isEqualTo("mock_access_token_value");
    assertThat(status.getPullRequestId()).isEqualTo(2);
    assertThat(status.getSha()).isEqualTo("a6befb598a35c1c206e1bf7bbb3018f4403b9610");
    assertThat(status.getUrl()).isEqualTo("http://localhost/sign/pivotal?repositoryId=rwinch/176_test&pullRequestId=2");
    assertThat(status.getFaqUrl()).endsWith("/about");
    assertThat(status.getSyncUrl()).contains("/sync/pivotal?repositoryId=rwinch/176_test&pullRequestId=2");
    assertThat(status.isSuccess()).isFalse();
}
Also used : AccessToken(io.pivotal.cla.data.AccessToken) PullRequestStatus(io.pivotal.cla.service.github.PullRequestStatus) Test(org.junit.Test)

Example 9 with AccessToken

use of io.pivotal.cla.data.AccessToken in project pivotal-cla by pivotalsoftware.

the class GitHubHooksControllerTests method markCommitStatusSuccessCorporate.

@Test
@SuppressWarnings("unchecked")
public void markCommitStatusSuccessCorporate() throws Exception {
    User user = WithSigningUserFactory.create();
    when(mockUserRepo.findOne(anyString())).thenReturn(user);
    when(mockTokenRepo.findOne("rwinch/176_test")).thenReturn(new AccessToken("rwinch/176_test", "mock_access_token_value"));
    when(mockGitHub.getOrganizations(anyString())).thenReturn(Arrays.asList("organization"));
    when(mockCorporateSignatureRepository.findSignature(anyString(), anySet(), anyCollectionOf(String.class))).thenReturn(corporateSignature);
    mockMvc.perform(hookRequest().header("X-GitHub-Event", GithubEvents.PULL_REQUEST).content(getPayload("pull_request.json"))).andExpect(status().isOk());
    ArgumentCaptor<PullRequestStatus> statusCaptor = ArgumentCaptor.forClass(PullRequestStatus.class);
    verify(mockGitHub).save(statusCaptor.capture());
    PullRequestStatus status = statusCaptor.getValue();
    assertThat(status.getRepoId()).isEqualTo("rwinch/176_test");
    assertThat(status.getAccessToken()).isEqualTo("mock_access_token_value");
    assertThat(status.getPullRequestId()).isEqualTo(2);
    assertThat(status.getSha()).isEqualTo("a6befb598a35c1c206e1bf7bbb3018f4403b9610");
    assertThat(status.getUrl()).isEqualTo("http://localhost/sign/pivotal?repositoryId=rwinch/176_test&pullRequestId=2");
    assertThat(status.isSuccess()).isTrue();
    assertThat(status.getGitHubUsername()).isEqualTo(user.getGitHubLogin());
}
Also used : User(io.pivotal.cla.data.User) AccessToken(io.pivotal.cla.data.AccessToken) Matchers.anyString(org.mockito.Matchers.anyString) PullRequestStatus(io.pivotal.cla.service.github.PullRequestStatus) Test(org.junit.Test)

Example 10 with AccessToken

use of io.pivotal.cla.data.AccessToken in project pivotal-cla by pivotalsoftware.

the class AdminLinkClaController method linkCla.

@RequestMapping(value = "/admin/cla/link", method = RequestMethod.POST)
public String linkCla(@AuthenticationPrincipal User user, HttpServletRequest request, Map<String, Object> model, @Valid LinkClaForm linkClaForm, BindingResult result, RedirectAttributes attrs) throws Exception {
    if (result.hasErrors()) {
        model.put("licenses", findPrimaryClas());
        model.put("accessTokensUrl", ACCESS_TOKENS_URL);
        model.put("repositories", gitHub.findRepositoryNamesWithAdminPermission(user.getAccessToken()));
        return "admin/cla/link";
    }
    AccessToken accessToken = tokenRepo.findOne(AccessToken.CLA_ACCESS_TOKEN_ID);
    UrlBuilder pullRequestUrlBldr = UrlBuilder.fromRequest(request);
    UrlBuilder signClaUrlBldr = UrlBuilder.fromRequest(request);
    String urlEncodedClaName = URLEncoder.encode(linkClaForm.getClaName(), "UTF-8");
    String pullRequestHookUrl = pullRequestUrlBldr.path(UrlBuilder.pullRequestHookCallbackPath(linkClaForm.getClaName())).build();
    String signClaUrl = signClaUrlBldr.path("/sign/" + urlEncodedClaName).build();
    CreatePullRequestHookRequest createPullRequest = new CreatePullRequestHookRequest();
    createPullRequest.setAccessToken(user.getAccessToken());
    List<String> repositoryIds = linkClaForm.getRepositories();
    createPullRequest.setRepositoryIds(repositoryIds);
    createPullRequest.setGitHubEventUrl(pullRequestHookUrl);
    createPullRequest.setSecret(accessToken.getToken());
    ContributingUrlsResponse contributingUrls = gitHub.getContributingUrls(repositoryIds);
    List<String> hookUrls = gitHub.createPullRequestHooks(createPullRequest);
    for (String repositoryId : repositoryIds) {
        AccessToken token = new AccessToken();
        token.setId(repositoryId);
        token.setToken(user.getAccessToken());
        tokenRepo.save(token);
    }
    UpdatePullRequestStatusesForm updatePullRequestStatusesForm = new UpdatePullRequestStatusesForm();
    updatePullRequestStatusesForm.setClaName(linkClaForm.getClaName());
    updatePullRequestStatusesForm.setRepositories(repositoryIds);
    attrs.addFlashAttribute("signClaUrl", signClaUrl);
    attrs.addFlashAttribute("hookUrls", hookUrls);
    attrs.addFlashAttribute("success", true);
    attrs.addFlashAttribute("editContributingAdocUrls", contributingUrls.getAsciidoc());
    attrs.addFlashAttribute("editContributingMdUrls", contributingUrls.getMarkdown());
    attrs.addFlashAttribute("updatePullRequestStatusesForm", updatePullRequestStatusesForm);
    return "redirect:/admin/cla/link";
}
Also used : CreatePullRequestHookRequest(io.pivotal.cla.service.github.CreatePullRequestHookRequest) AccessToken(io.pivotal.cla.data.AccessToken) ContributingUrlsResponse(io.pivotal.cla.service.github.ContributingUrlsResponse) UrlBuilder(io.pivotal.cla.mvc.util.UrlBuilder) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

AccessToken (io.pivotal.cla.data.AccessToken)19 Test (org.junit.Test)13 PullRequestStatus (io.pivotal.cla.service.github.PullRequestStatus)10 User (io.pivotal.cla.data.User)9 WithSigningUser (io.pivotal.cla.security.WithSigningUser)2 ContributingUrlsResponse (io.pivotal.cla.service.github.ContributingUrlsResponse)2 CreatePullRequestHookRequest (io.pivotal.cla.service.github.CreatePullRequestHookRequest)2 Before (org.junit.Before)2 Matchers.anyString (org.mockito.Matchers.anyString)2 PullRequestId (io.pivotal.cla.egit.github.core.PullRequestId)1 UrlBuilder (io.pivotal.cla.mvc.util.UrlBuilder)1 WithAdminUser (io.pivotal.cla.security.WithAdminUser)1 SignCclaPage (io.pivotal.cla.webdriver.pages.SignCclaPage)1 SignClaPage (io.pivotal.cla.webdriver.pages.SignClaPage)1 SignIclaPage (io.pivotal.cla.webdriver.pages.SignIclaPage)1 AdminLinkClaPage (io.pivotal.cla.webdriver.pages.admin.AdminLinkClaPage)1 SneakyThrows (lombok.SneakyThrows)1 WithAnonymousUser (org.springframework.security.test.context.support.WithAnonymousUser)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1