Search in sources :

Example 26 with RemoteAPI

use of ca.corefacility.bioinformatics.irida.model.RemoteAPI in project irida by phac-nml.

the class SampleRemoteRepositoryImpl method getSampleMetadata.

/**
 * {@inheritDoc}
 */
public Map<String, MetadataEntry> getSampleMetadata(Sample sample) {
    logger.trace("Requesting sample metadata for sample " + sample.getSelfHref());
    RemoteAPI remoteAPI = sample.getRemoteStatus().getApi();
    OAuthTokenRestTemplate restTemplate = new OAuthTokenRestTemplate(tokenService, remoteAPI);
    // get the metadata link
    Link metadataLink = sample.getLink(METADATA_REL);
    // request metadata response
    ResponseEntity<ResourceWrapper<SampleMetadataWrapper>> exchange = restTemplate.exchange(metadataLink.getHref(), HttpMethod.GET, HttpEntity.EMPTY, metadataTypeReference);
    // pull metadata response from request
    Map<String, MetadataEntry> resource = exchange.getBody().getResource().getMetadata();
    return resource;
}
Also used : RemoteAPI(ca.corefacility.bioinformatics.irida.model.RemoteAPI) ResourceWrapper(ca.corefacility.bioinformatics.irida.model.remote.resource.ResourceWrapper) ListResourceWrapper(ca.corefacility.bioinformatics.irida.model.remote.resource.ListResourceWrapper) MetadataEntry(ca.corefacility.bioinformatics.irida.model.sample.metadata.MetadataEntry) Link(org.springframework.hateoas.Link) OAuthTokenRestTemplate(ca.corefacility.bioinformatics.irida.repositories.remote.resttemplate.OAuthTokenRestTemplate)

Example 27 with RemoteAPI

use of ca.corefacility.bioinformatics.irida.model.RemoteAPI in project irida by phac-nml.

the class OltuAuthorizationControllerTest method testAuthenticate.

@Test
public void testAuthenticate() throws OAuthSystemException, UnsupportedEncodingException {
    RemoteAPI remoteAPI = new RemoteAPI("name", "http://uri", "a description", "id", "secret");
    remoteAPI.setId(1L);
    String redirect = "http://base";
    String authenticate = controller.authenticate(remoteAPI, redirect);
    // need to decode the escaped characters
    String decoded = URLDecoder.decode(authenticate, "UTF-8");
    assertTrue(decoded.startsWith("redirect:"));
    assertTrue(decoded.contains(redirect));
    assertTrue(decoded.contains(serverBase));
}
Also used : RemoteAPI(ca.corefacility.bioinformatics.irida.model.RemoteAPI) Test(org.junit.Test)

Example 28 with RemoteAPI

use of ca.corefacility.bioinformatics.irida.model.RemoteAPI in project irida by phac-nml.

the class OltuAuthorizationControllerTest method testGetTokenFromAuthCode.

@Test
public void testGetTokenFromAuthCode() throws IOException, OAuthSystemException, OAuthProblemException, URISyntaxException {
    Long apiId = 1L;
    RemoteAPI remoteAPI = new RemoteAPI("name", "http://remoteLocation", "a description", "id", "secret");
    remoteAPI.setId(apiId);
    String code = "code";
    String redirect = "http://originalPage";
    when(apiService.read(apiId)).thenReturn(remoteAPI);
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    Map<String, String[]> requestParams = new HashMap<>();
    requestParams.put("code", new String[] { code });
    when(request.getParameterMap()).thenReturn(requestParams);
    controller.getTokenFromAuthCode(request, response, apiId, redirect);
    verify(apiService).read(apiId);
    ArgumentCaptor<String> redirectArg = ArgumentCaptor.forClass(String.class);
    verify(tokenService).createTokenFromAuthCode(eq(code), eq(remoteAPI), redirectArg.capture());
    String capturedRedirect = redirectArg.getValue();
    assertTrue(capturedRedirect.contains(redirect));
    assertTrue(capturedRedirect.contains(serverBase));
}
Also used : RemoteAPI(ca.corefacility.bioinformatics.irida.model.RemoteAPI) HttpServletRequest(javax.servlet.http.HttpServletRequest) HashMap(java.util.HashMap) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Example 29 with RemoteAPI

use of ca.corefacility.bioinformatics.irida.model.RemoteAPI in project irida by phac-nml.

the class RemoteAPIControllerTest method testConnectToAPI.

@Test(expected = IridaOAuthException.class)
public void testConnectToAPI() {
    Long apiId = 1L;
    ExtendedModelMap model = new ExtendedModelMap();
    RemoteAPI client = new RemoteAPI("name", "http://uri", "a description", "id", "secret");
    when(remoteAPIService.read(apiId)).thenReturn(client);
    when(projectRemoteService.getServiceStatus(client)).thenThrow(new IridaOAuthException("invalid token", client));
    remoteAPIController.connectToAPI(apiId, model);
}
Also used : RemoteAPI(ca.corefacility.bioinformatics.irida.model.RemoteAPI) IridaOAuthException(ca.corefacility.bioinformatics.irida.exceptions.IridaOAuthException) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) Test(org.junit.Test)

Example 30 with RemoteAPI

use of ca.corefacility.bioinformatics.irida.model.RemoteAPI in project irida by phac-nml.

the class RemoteAPIControllerTest method testHandleOAuthException.

@Test
public void testHandleOAuthException() throws MalformedURLException, OAuthSystemException {
    HttpServletRequest request = mock(HttpServletRequest.class);
    String redirect = "http://request";
    when(request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).thenReturn(redirect);
    RemoteAPI client = new RemoteAPI("name", "http://uri", "a description", "id", "secret");
    IridaOAuthException ex = new IridaOAuthException("msg", client);
    remoteAPIController.handleOAuthException(request, ex);
    verify(authController).authenticate(client, redirect);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RemoteAPI(ca.corefacility.bioinformatics.irida.model.RemoteAPI) IridaOAuthException(ca.corefacility.bioinformatics.irida.exceptions.IridaOAuthException) Test(org.junit.Test)

Aggregations

RemoteAPI (ca.corefacility.bioinformatics.irida.model.RemoteAPI)44 Test (org.junit.Test)30 RemoteAPIToken (ca.corefacility.bioinformatics.irida.model.RemoteAPIToken)6 ExtendedModelMap (org.springframework.ui.ExtendedModelMap)6 URI (java.net.URI)5 Date (java.util.Date)5 Link (org.springframework.hateoas.Link)5 IridaOAuthException (ca.corefacility.bioinformatics.irida.exceptions.IridaOAuthException)4 Project (ca.corefacility.bioinformatics.irida.model.project.Project)4 EntityNotFoundException (ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException)3 RemoteStatus (ca.corefacility.bioinformatics.irida.model.remote.RemoteStatus)3 Before (org.junit.Before)3 Sample (ca.corefacility.bioinformatics.irida.model.sample.Sample)2 SequenceFile (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile)2 User (ca.corefacility.bioinformatics.irida.model.user.User)2 OAuthTokenRestTemplate (ca.corefacility.bioinformatics.irida.repositories.remote.resttemplate.OAuthTokenRestTemplate)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)2 WithMockUser (org.springframework.security.test.context.support.WithMockUser)2 MockRestServiceServer (org.springframework.test.web.client.MockRestServiceServer)2