Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Sawicki2011-11-10 19:34:32 +0000
committerKevin Sawicki2011-11-10 19:34:32 +0000
commit91449d29fb0c23f688991e7d05596755df0ad15f (patch)
tree84635d7e238c884d7c873a3d26969afd20f91bee
parent0c1e1b71f49a3020b40385e2224ff97244d30574 (diff)
downloadegit-github-91449d29fb0c23f688991e7d05596755df0ad15f.tar.gz
egit-github-91449d29fb0c23f688991e7d05596755df0ad15f.tar.xz
egit-github-91449d29fb0c23f688991e7d05596755df0ad15f.zip
Add service support for OAuth authorization API
Change-Id: I18dd6a66d3d6191bfc68b914fec746832d695110 Signed-off-by: Kevin Sawicki <kevin@github.com>
-rw-r--r--org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/AllHeadlessTests.java5
-rw-r--r--org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/ApplicationTest.java43
-rw-r--r--org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/AuthorizationTest.java61
-rw-r--r--org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/OAuthServiceTest.java166
-rw-r--r--org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/Application.java58
-rw-r--r--org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/Authorization.java152
-rw-r--r--org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/IGitHubConstants.java2
-rw-r--r--org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/service/OAuthService.java157
8 files changed, 643 insertions, 1 deletions
diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/AllHeadlessTests.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/AllHeadlessTests.java
index 7e0642b6..fcb0ea5c 100644
--- a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/AllHeadlessTests.java
+++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/AllHeadlessTests.java
@@ -20,7 +20,9 @@ import org.junit.runners.Suite.SuiteClasses;
* All headless unit tests of GitHub Java API
*/
@RunWith(Suite.class)
-@SuiteClasses({ BlobTest.class, //
+@SuiteClasses({ ApplicationTest.class, //
+ AuthorizationTest.class, //
+ BlobTest.class, //
CollaboratorServiceTest.class, //
CommentTest.class, //
CommitCommentTest.class, //
@@ -59,6 +61,7 @@ import org.junit.runners.Suite.SuiteClasses;
MilestoneComparatorTest.class, //
MilestoneServiceTest.class, //
MilestoneTest.class, //
+ OAuthServiceTest.class, //
OrganizationServiceTest.class, //
PagedRequestTest.class, //
PullRequestMarkerTest.class, //
diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/ApplicationTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/ApplicationTest.java
new file mode 100644
index 00000000..a8e62ec6
--- /dev/null
+++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/ApplicationTest.java
@@ -0,0 +1,43 @@
+/******************************************************************************
+ * Copyright (c) 2011 GitHub Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Kevin Sawicki (GitHub Inc.) - initial API and implementation
+ *****************************************************************************/
+package org.eclipse.egit.github.core.tests;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import org.eclipse.egit.github.core.Application;
+import org.junit.Test;
+
+/**
+ * Unit tests of {@link Application}
+ */
+public class ApplicationTest {
+
+ /**
+ * Test default state of application
+ */
+ @Test
+ public void defaultState() {
+ Application app = new Application();
+ assertNull(app.getName());
+ assertNull(app.getUrl());
+ }
+
+ /**
+ * Test updating application fields
+ */
+ @Test
+ public void updateFields() {
+ Application app = new Application();
+ assertEquals("name", app.setName("name").getName());
+ assertEquals("url", app.setUrl("url").getUrl());
+ }
+}
diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/AuthorizationTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/AuthorizationTest.java
new file mode 100644
index 00000000..b1a52c55
--- /dev/null
+++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/AuthorizationTest.java
@@ -0,0 +1,61 @@
+/******************************************************************************
+ * Copyright (c) 2011 GitHub Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Kevin Sawicki (GitHub Inc.) - initial API and implementation
+ *****************************************************************************/
+package org.eclipse.egit.github.core.tests;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.util.Collections;
+import java.util.Date;
+
+import org.eclipse.egit.github.core.Application;
+import org.eclipse.egit.github.core.Authorization;
+import org.junit.Test;
+
+/**
+ * Unit tests of {@link Authorization}
+ */
+public class AuthorizationTest {
+
+ /**
+ * Test default state of authorization
+ */
+ @Test
+ public void defaultState() {
+ Authorization auth = new Authorization();
+ assertNull(auth.getApp());
+ assertNull(auth.getCreatedAt());
+ assertEquals(0, auth.getId());
+ assertNull(auth.getScopes());
+ assertNull(auth.getToken());
+ assertNull(auth.getUpdatedAt());
+ assertNull(auth.getUrl());
+ }
+
+ /**
+ * Test updating application fields
+ */
+ @Test
+ public void updateFields() {
+ Authorization auth = new Authorization();
+ Application app = new Application();
+ assertEquals(app, auth.setApp(app).getApp());
+ assertEquals(new Date(2500), auth.setCreatedAt(new Date(2500))
+ .getCreatedAt());
+ assertEquals(123, auth.setId(123).getId());
+ assertEquals(Collections.singletonList("repo"),
+ auth.setScopes(Collections.singletonList("repo")).getScopes());
+ assertEquals("token", auth.setToken("token").getToken());
+ assertEquals(new Date(8000), auth.setUpdatedAt(new Date(8000))
+ .getUpdatedAt());
+ assertEquals("url", auth.setUrl("url").getUrl());
+ }
+}
diff --git a/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/OAuthServiceTest.java b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/OAuthServiceTest.java
new file mode 100644
index 00000000..3008ff7d
--- /dev/null
+++ b/org.eclipse.egit.github.core.tests/src/org/eclipse/egit/github/core/tests/OAuthServiceTest.java
@@ -0,0 +1,166 @@
+/******************************************************************************
+ * Copyright (c) 2011 GitHub Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Kevin Sawicki (GitHub Inc.) - initial API and implementation
+ *****************************************************************************/
+package org.eclipse.egit.github.core.tests;
+
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.verify;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+
+import org.eclipse.egit.github.core.Authorization;
+import org.eclipse.egit.github.core.client.GitHubClient;
+import org.eclipse.egit.github.core.client.GitHubRequest;
+import org.eclipse.egit.github.core.client.GitHubResponse;
+import org.eclipse.egit.github.core.service.OAuthService;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+/**
+ * Unit tests of {@link OAuthService}
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class OAuthServiceTest {
+
+ @Mock
+ private GitHubClient client;
+
+ @Mock
+ private GitHubResponse response;
+
+ private OAuthService service;
+
+ /**
+ * Test case set up
+ *
+ * @throws IOException
+ */
+ @Before
+ public void before() throws IOException {
+ doReturn(response).when(client).get(any(GitHubRequest.class));
+ service = new OAuthService(client);
+ }
+
+ /**
+ * Create service with null client
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void constructorNullArgument() {
+ new OAuthService(null);
+ }
+
+ /**
+ * Create service using default constructor
+ */
+ @Test
+ public void defaultConstructor() {
+ assertNotNull(new OAuthService().getClient());
+ }
+
+ /**
+ * Get authorizations
+ *
+ * @throws IOException
+ */
+ @Test
+ public void getAuthorizations() throws IOException {
+ service.getAuthorizations();
+ GitHubRequest request = new GitHubRequest();
+ request.setUri(Utils.page("/authorizations"));
+ verify(client).get(request);
+ }
+
+ /**
+ * Get authorization
+ *
+ * @throws IOException
+ */
+ @Test
+ public void getAuthorization() throws IOException {
+ service.getAuthorization(400);
+ GitHubRequest request = new GitHubRequest();
+ request.setUri("/authorizations/400");
+ verify(client).get(request);
+ }
+
+ /**
+ * Delete authorization
+ *
+ * @throws IOException
+ */
+ @Test
+ public void deleteAuthorization() throws IOException {
+ service.deleteAuthorization(678);
+ verify(client).delete("/authorizations/678");
+ }
+
+ /**
+ * Create authorization
+ *
+ * @throws IOException
+ */
+ @Test
+ public void createAuthorization() throws IOException {
+ Authorization auth = new Authorization();
+ service.createAuthorization(auth);
+ verify(client).post("/authorizations", auth, Authorization.class);
+ }
+
+ /**
+ * Test adding scopes to authorization
+ *
+ * @throws IOException
+ */
+ @Test
+ public void addScopes() throws IOException {
+ Collection<String> scopes = Arrays.asList("repo");
+ service.addScopes(300, scopes);
+ verify(client).post("/authorizations/300",
+ Collections.singletonMap("add_scopes", scopes),
+ Authorization.class);
+ }
+
+ /**
+ * Test removing scopes to authorization
+ *
+ * @throws IOException
+ */
+ @Test
+ public void removeScopes() throws IOException {
+ Collection<String> scopes = Arrays.asList("user");
+ service.removeScopes(400, scopes);
+ verify(client).post("/authorizations/400",
+ Collections.singletonMap("remove_scopes", scopes),
+ Authorization.class);
+ }
+
+ /**
+ * Test setting scopes to authorization
+ *
+ * @throws IOException
+ */
+ @Test
+ public void setScopes() throws IOException {
+ Collection<String> scopes = Arrays.asList("gist");
+ service.setScopes(500, scopes);
+ verify(client)
+ .post("/authorizations/500",
+ Collections.singletonMap("scopes", scopes),
+ Authorization.class);
+ }
+}
diff --git a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/Application.java b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/Application.java
new file mode 100644
index 00000000..be71feb9
--- /dev/null
+++ b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/Application.java
@@ -0,0 +1,58 @@
+/******************************************************************************
+ * Copyright (c) 2011 GitHub Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Kevin Sawicki (GitHub Inc.) - initial API and implementation
+ *****************************************************************************/
+package org.eclipse.egit.github.core;
+
+import java.io.Serializable;
+
+/**
+ * Authorized application model class
+ */
+public class Application implements Serializable {
+
+ /** serialVersionUID */
+ private static final long serialVersionUID = 5675660442127228497L;
+
+ private String name;
+
+ private String url;
+
+ /**
+ * @return name
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * @param name
+ * @return this application
+ */
+ public Application setName(String name) {
+ this.name = name;
+ return this;
+ }
+
+ /**
+ * @return url
+ */
+ public String getUrl() {
+ return url;
+ }
+
+ /**
+ * @param url
+ * @return this application
+ */
+ public Application setUrl(String url) {
+ this.url = url;
+ return this;
+ }
+}
diff --git a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/Authorization.java b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/Authorization.java
new file mode 100644
index 00000000..a0279645
--- /dev/null
+++ b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/Authorization.java
@@ -0,0 +1,152 @@
+/******************************************************************************
+ * Copyright (c) 2011 GitHub Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Kevin Sawicki (GitHub Inc.) - initial API and implementation
+ *****************************************************************************/
+package org.eclipse.egit.github.core;
+
+import java.io.Serializable;
+import java.util.Date;
+import java.util.List;
+
+import org.eclipse.egit.github.core.util.DateUtils;
+
+/**
+ * Authorization model class
+ */
+public class Authorization implements Serializable {
+
+ /** serialVersionUID */
+ private static final long serialVersionUID = -5564926246696914047L;
+
+ private Application app;
+
+ private Date createdAt;
+
+ private Date updatedAt;
+
+ private int id;
+
+ private List<String> scopes;
+
+ private String token;
+
+ private String url;
+
+ /**
+ * @return app
+ */
+ public Application getApp() {
+ return app;
+ }
+
+ /**
+ * @param app
+ * @return this authorization
+ */
+ public Authorization setApp(Application app) {
+ this.app = app;
+ return this;
+ }
+
+ /**
+ * @return createdAt
+ */
+ public Date getCreatedAt() {
+ return DateUtils.clone(createdAt);
+ }
+
+ /**
+ * @param createdAt
+ * @return this authorization
+ */
+ public Authorization setCreatedAt(Date createdAt) {
+ this.createdAt = DateUtils.clone(createdAt);
+ return this;
+ }
+
+ /**
+ * @return updatedAt
+ */
+ public Date getUpdatedAt() {
+ return DateUtils.clone(updatedAt);
+ }
+
+ /**
+ * @param updatedAt
+ * @return this authorization
+ */
+ public Authorization setUpdatedAt(Date updatedAt) {
+ this.updatedAt = DateUtils.clone(updatedAt);
+ return this;
+ }
+
+ /**
+ * @return id
+ */
+ public int getId() {
+ return id;
+ }
+
+ /**
+ * @param id
+ * @return this authorization
+ */
+ public Authorization setId(int id) {
+ this.id = id;
+ return this;
+ }
+
+ /**
+ * @return scopes
+ */
+ public List<String> getScopes() {
+ return scopes;
+ }
+
+ /**
+ * @param scopes
+ * @return this authorization
+ */
+ public Authorization setScopes(List<String> scopes) {
+ this.scopes = scopes;
+ return this;
+ }
+
+ /**
+ * @return token
+ */
+ public String getToken() {
+ return token;
+ }
+
+ /**
+ * @param token
+ * @return this authorization
+ */
+ public Authorization setToken(String token) {
+ this.token = token;
+ return this;
+ }
+
+ /**
+ * @return url
+ */
+ public String getUrl() {
+ return url;
+ }
+
+ /**
+ * @param url
+ * @return this authorization
+ */
+ public Authorization setUrl(String url) {
+ this.url = url;
+ return this;
+ }
+}
diff --git a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/IGitHubConstants.java b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/IGitHubConstants.java
index 88132728..1f313748 100644
--- a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/IGitHubConstants.java
+++ b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/IGitHubConstants.java
@@ -73,6 +73,8 @@ public interface IGitHubConstants {
String SCHEME_OAUTH2 = "oauth2"; //$NON-NLS-1$
/** */
+ String SEGMENT_AUTHORIZATIONS = "/authorizations"; //$NON-NLS-1$
+ /** */
String SEGMENT_BLOBS = "/blobs"; //$NON-NLS-1$
/** */
String SEGMENT_BRANCHES = "/branches"; //$NON-NLS-1$
diff --git a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/service/OAuthService.java b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/service/OAuthService.java
new file mode 100644
index 00000000..4133f8df
--- /dev/null
+++ b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/service/OAuthService.java
@@ -0,0 +1,157 @@
+/******************************************************************************
+ * Copyright (c) 2011 GitHub Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Kevin Sawicki (GitHub Inc.) - initial API and implementation
+ *****************************************************************************/
+package org.eclipse.egit.github.core.service;
+
+import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_AUTHORIZATIONS;
+
+import com.google.gson.reflect.TypeToken;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import org.eclipse.egit.github.core.Authorization;
+import org.eclipse.egit.github.core.client.GitHubClient;
+import org.eclipse.egit.github.core.client.GitHubRequest;
+import org.eclipse.egit.github.core.client.PagedRequest;
+
+/**
+ * Service for interacting with a user's OAUth authorizations
+ *
+ * @see <a href="http://developer.github.com/v3/oauth/">GitHub OAuth API
+ * documentation</a>
+ */
+public class OAuthService extends GitHubService {
+
+ /**
+ * Create OAuth service
+ */
+ public OAuthService() {
+ super();
+ }
+
+ /**
+ * Create OAuth service
+ *
+ * @param client
+ */
+ public OAuthService(GitHubClient client) {
+ super(client);
+ }
+
+ /**
+ * Get all authorizations for currently authenticated user
+ *
+ * @return list of authorizations
+ * @throws IOException
+ */
+ public List<Authorization> getAuthorizations() throws IOException {
+ PagedRequest<Authorization> request = createPagedRequest();
+ request.setUri(SEGMENT_AUTHORIZATIONS);
+ request.setType(new TypeToken<List<Authorization>>() {
+ }.getType());
+ return getAll(request);
+ }
+
+ /**
+ * Get authorization with given id
+ *
+ * @param id
+ * @return authorization
+ * @throws IOException
+ */
+ public Authorization getAuthorization(int id) throws IOException {
+ GitHubRequest request = createRequest();
+ StringBuilder uri = new StringBuilder(SEGMENT_AUTHORIZATIONS);
+ uri.append('/').append(id);
+ request.setUri(uri);
+ request.setType(Authorization.class);
+ return (Authorization) client.get(request).getBody();
+ }
+
+ /**
+ * Delete authorization with given id
+ *
+ * @param id
+ * @throws IOException
+ */
+ public void deleteAuthorization(int id) throws IOException {
+ StringBuilder uri = new StringBuilder(SEGMENT_AUTHORIZATIONS);
+ uri.append('/').append(id);
+ client.delete(uri.toString());
+ }
+
+ /**
+ * Create authorization
+ *
+ * @param authorization
+ * @return authorization
+ * @throws IOException
+ */
+ public Authorization createAuthorization(Authorization authorization)
+ throws IOException {
+ return client.post(SEGMENT_AUTHORIZATIONS, authorization,
+ Authorization.class);
+ }
+
+ /**
+ * Add scopes to authorization
+ *
+ * @param id
+ * @param scopes
+ * @return authorization
+ * @throws IOException
+ */
+ public Authorization addScopes(int id, Collection<String> scopes)
+ throws IOException {
+ StringBuilder uri = new StringBuilder(SEGMENT_AUTHORIZATIONS);
+ uri.append('/').append(id);
+ Map<String, Collection<String>> params = Collections.singletonMap(
+ "add_scopes", scopes); //$NON-NLS-1$
+ return client.post(uri.toString(), params, Authorization.class);
+ }
+
+ /**
+ * Remove scopes from authorization
+ *
+ * @param id
+ * @param scopes
+ * @return authorization
+ * @throws IOException
+ */
+ public Authorization removeScopes(int id, Collection<String> scopes)
+ throws IOException {
+ StringBuilder uri = new StringBuilder(SEGMENT_AUTHORIZATIONS);
+ uri.append('/').append(id);
+ Map<String, Collection<String>> params = Collections.singletonMap(
+ "remove_scopes", scopes); //$NON-NLS-1$
+ return client.post(uri.toString(), params, Authorization.class);
+ }
+
+ /**
+ * Set scopes for authorization
+ *
+ * @param id
+ * @param scopes
+ * @return authorization
+ * @throws IOException
+ */
+ public Authorization setScopes(int id, Collection<String> scopes)
+ throws IOException {
+ StringBuilder uri = new StringBuilder(SEGMENT_AUTHORIZATIONS);
+ uri.append('/').append(id);
+ Map<String, Collection<String>> params = Collections.singletonMap(
+ "scopes", scopes); //$NON-NLS-1$
+ return client.post(uri.toString(), params, Authorization.class);
+ }
+}

Back to the top