blob: e18dc48ac172eb3b554c3377752ca9e21f7d178f [file] [log] [blame]
package org.eclipse.userstorage.oauth;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.client.fluent.Request;
import org.eclipse.userstorage.internal.Session;
import org.eclipse.userstorage.spi.Credentials;
import org.junit.Test;
public class EclipseOAuthCredentialsProviderTest {
@Test
public void testClientDetails() throws URISyntaxException {
EclipseOAuthCredentialsProvider provider = new EclipseOAuthCredentialsProvider("clientId", "clientSecret",
new String[] { "scope1", "scope2" }, new URI("http://localhost"));
assertEquals("clientId", provider.getClientId());
assertEquals("clientSecret", provider.getClientSecret());
assertArrayEquals(new String[] { "scope1", "scope2" }, provider.getScopes());
assertEquals(new URI("http://localhost"), provider.getRegisteredCallback());
}
@Test
public void testIsValid_InvalidCredentials() throws URISyntaxException {
EclipseOAuthCredentialsProvider provider = new EclipseOAuthCredentialsProvider("clientId", "clientSecret",
new String[] { "scope1", "scope2" }, new URI("http://localhost"));
assertFalse(provider.isValid(new Credentials("", "blah")));
assertFalse(provider.isValid(new Credentials("username", "")));
assertFalse(provider.isValid(new Credentials("username", "blah")));
}
@Test
public void testIsValid_ExpiredCredentials() throws URISyntaxException {
EclipseOAuthCredentialsProvider provider = new EclipseOAuthCredentialsProvider("clientId", "clientSecret",
new String[] { "scope1", "scope2" }, new URI("http://localhost"));
// expired token
assertFalse(provider.isValid(new Credentials("username", "{\"access_token\":\"a8f8a49232520e7d621b1ae1235a35665e27344d\","
+ "\"token_type\":\"Bearer\"," + "\"scope\":\"scope1\","
+ "\"_expires_\":\"Mon, 07 Nov 2016 22:19:17 GMT\"}")));
}
@Test
public void testIsValid_InsufficientScope() throws URISyntaxException {
EclipseOAuthCredentialsProvider provider = new EclipseOAuthCredentialsProvider("clientId", "clientSecret",
new String[] { "scope1", "scope2" }, new URI("http://localhost"));
// expired token
assertFalse(provider.isValid(new Credentials("username", "{\"access_token\":\"a8f8a49232520e7d621b1ae1235a35665e27344d\","
+ "\"token_type\":\"Bearer\"," + "\"scope\":\"scope1\"}")));
}
@Test
public void testConfigureRequest() throws URISyntaxException {
String serializedForm = "{\"access_token\":\"a8f8a49232520e7d621b1ae1235a35665e27344d\","
+ "\"token_type\":\"Bearer\"," + "\"scope\":\"scope1 scope2\"}";
Credentials credentials = new Credentials("username", serializedForm);
EclipseOAuthCredentialsProvider provider = new EclipseOAuthCredentialsProvider("clientId", "clientSecret",
new String[] {"scope1"}, new URI("http://localhost"));
assertTrue(provider.isValid(credentials));
Request request = provider.configureRequest(Request.Get("http://localhost"), new URI("http://nonsensical"),
credentials);
assertNotNull(request);
assertThat(Session.formatRequest(request),
containsString("Authorization: Bearer a8f8a49232520e7d621b1ae1235a35665e27344d"));
}
}