| margaretha | ec247dd | 2018-06-12 21:55:46 +0200 | [diff] [blame^] | 1 | package de.ids_mannheim.korap.web.controller; |
| 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertTrue; |
| 5 | |
| 6 | import java.net.URI; |
| 7 | |
| 8 | import javax.ws.rs.core.MultivaluedMap; |
| 9 | |
| 10 | import org.apache.http.entity.ContentType; |
| 11 | import org.junit.Test; |
| 12 | import org.springframework.beans.factory.annotation.Autowired; |
| 13 | |
| 14 | import com.google.common.net.HttpHeaders; |
| 15 | import com.sun.jersey.api.client.ClientHandlerException; |
| 16 | import com.sun.jersey.api.client.ClientResponse; |
| 17 | import com.sun.jersey.api.client.UniformInterfaceException; |
| 18 | import com.sun.jersey.core.util.MultivaluedMapImpl; |
| 19 | |
| 20 | import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler; |
| 21 | import de.ids_mannheim.korap.config.Attributes; |
| 22 | import de.ids_mannheim.korap.config.SpringJerseyTest; |
| 23 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 24 | |
| 25 | public class OAuth2OpenIdControllerTest extends SpringJerseyTest { |
| 26 | |
| 27 | @Autowired |
| 28 | private HttpAuthorizationHandler handler; |
| 29 | |
| 30 | @Test |
| 31 | public void testAuthorize () throws UniformInterfaceException, |
| 32 | ClientHandlerException, KustvaktException { |
| 33 | |
| 34 | String redirectUri = |
| 35 | "https://korap.ids-mannheim.de/confidential/redirect"; |
| 36 | MultivaluedMap<String, String> form = new MultivaluedMapImpl(); |
| 37 | form.add("response_type", "code"); |
| 38 | form.add("scope", "openid"); |
| 39 | form.add("redirect_uri", redirectUri); |
| 40 | form.add("client_id", "fCBbQkAyYzI4NzUxMg"); |
| 41 | |
| 42 | ClientResponse response = resource().path("oauth2").path("openid") |
| 43 | .path("authorize") |
| 44 | .header(Attributes.AUTHORIZATION, |
| 45 | handler.createBasicAuthorizationHeaderValue("dory", |
| 46 | "password")) |
| 47 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 48 | .header(HttpHeaders.CONTENT_TYPE, |
| 49 | ContentType.APPLICATION_FORM_URLENCODED) |
| 50 | .entity(form).post(ClientResponse.class); |
| 51 | |
| 52 | URI location = response.getLocation(); |
| 53 | |
| 54 | assertEquals(redirectUri, location.getScheme() + "://" |
| 55 | + location.getHost() + location.getPath()); |
| 56 | assertTrue(location.getQuery().startsWith("code")); |
| 57 | } |
| 58 | |
| 59 | } |