Commit fc7515b2 authored by Greg Messner's avatar Greg Messner
Browse files

GitLabApi class now tracks Session if session login was used.

parent 70d987fb
...@@ -2,6 +2,8 @@ package org.gitlab4j.api; ...@@ -2,6 +2,8 @@ package org.gitlab4j.api;
import java.util.Map; import java.util.Map;
import org.gitlab4j.api.models.Session;
/** /**
* This class is provides a simplified interface to a GitLab API server, and divides the API up into * This class is provides a simplified interface to a GitLab API server, and divides the API up into
* a separate API class for each concern. * a separate API class for each concern.
...@@ -20,6 +22,8 @@ public class GitLabApi { ...@@ -20,6 +22,8 @@ public class GitLabApi {
private SessionApi sessoinApi; private SessionApi sessoinApi;
private UserApi userApi; private UserApi userApi;
private Session session;
/** /**
* Logs into GitLab using provided {@code username} and {@code password}, and creates a new {@code GitLabApi} instance using returned private token * Logs into GitLab using provided {@code username} and {@code password}, and creates a new {@code GitLabApi} instance using returned private token
* *
...@@ -30,8 +34,19 @@ public class GitLabApi { ...@@ -30,8 +34,19 @@ public class GitLabApi {
* @throws GitLabApiException GitLabApiException if any exception occurs during execution * @throws GitLabApiException GitLabApiException if any exception occurs during execution
*/ */
public static GitLabApi create(String url, String username, String password) throws GitLabApiException { public static GitLabApi create(String url, String username, String password) throws GitLabApiException {
String token = new SessionApi(new GitLabApi(url, null)).login(username, null, password).getPrivateToken(); SessionApi sessionApi = new SessionApi(new GitLabApi(url, (String)null));
return new GitLabApi(url, token); Session session = sessionApi.login(username, null, password);
return (new GitLabApi(url, session));
}
/**
* If this instance was created with {@link #create(String, String, String)} this method will
* return the Session instance returned by the GitLab API on login, otherwise returns null.
*
* @return the Session instance
*/
public Session getSession() {
return session;
} }
/** /**
...@@ -45,6 +60,18 @@ public class GitLabApi { ...@@ -45,6 +60,18 @@ public class GitLabApi {
this(hostUrl, privateToken, null); this(hostUrl, privateToken, null);
} }
/**
* Constructs a GitLabApi instance set up to interact with the GitLab server
* specified by hostUrl.
*
* @param hostUrl the URL of the GitLab server
* @param session the Session instance obtained by logining into the GitLab server
*/
public GitLabApi(String hostUrl, Session session) {
this(hostUrl, session.getPrivateToken(), null);
this.session = session;
}
/** /**
* Constructs a GitLabApi instance set up to interact with the GitLab server * Constructs a GitLabApi instance set up to interact with the GitLab server
* specified by hostUrl. * specified by hostUrl.
......
package org.gitlab4j.api; package org.gitlab4j.api;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assume.assumeTrue; import static org.junit.Assume.assumeTrue;
...@@ -13,11 +14,13 @@ import org.junit.Test; ...@@ -13,11 +14,13 @@ import org.junit.Test;
* TEST_HOST_URL * TEST_HOST_URL
* TEST_USERNAME * TEST_USERNAME
* TEST_PASSWORD * TEST_PASSWORD
* TEST_PRIVATE_TOKEN
* *
* If any of the above are NULL, all tests in this class will be skipped. If running from mvn simply * If any of the above are NULL, all tests in this class will be skipped. If running from mvn simply
* use a command line similar to: * use a command line similar to:
* *
* mvn test -DTEST_HOST_URL=https://gitlab.com -DTTEST_USERNAME=your_username -DTEST_PASSWORD=your_strong_password * mvn test -DTEST_HOST_URL=https://gitlab.com -DTTEST_USERNAME=your_username \
* -DTEST_PASSWORD=your_strong_password -DTEST_PRIVATE_TOKEN=your_private_token
*/ */
public class TestGitLabSession { public class TestGitLabSession {
...@@ -25,10 +28,12 @@ public class TestGitLabSession { ...@@ -25,10 +28,12 @@ public class TestGitLabSession {
private static final String TEST_USERNAME; private static final String TEST_USERNAME;
private static final String TEST_PASSWORD; private static final String TEST_PASSWORD;
private static final String TEST_HOST_URL; private static final String TEST_HOST_URL;
private static final String TEST_PRIVATE_TOKEN;
static { static {
TEST_USERNAME = System.getProperty("TEST_USERNAME"); TEST_USERNAME = System.getProperty("TEST_USERNAME");
TEST_PASSWORD = System.getProperty("TEST_PASSWORD"); TEST_PASSWORD = System.getProperty("TEST_PASSWORD");
TEST_HOST_URL = System.getProperty("TEST_HOST_URL"); TEST_HOST_URL = System.getProperty("TEST_HOST_URL");
TEST_PRIVATE_TOKEN = System.getProperty("TEST_PRIVATE_TOKEN");
} }
private static String problems = ""; private static String problems = "";
...@@ -54,6 +59,10 @@ public class TestGitLabSession { ...@@ -54,6 +59,10 @@ public class TestGitLabSession {
problems += "TEST_HOST_URL cannot be empty\n"; problems += "TEST_HOST_URL cannot be empty\n";
} }
if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN.trim().length() == 0) {
problems += "TEST_PRIVATE_TOKEN cannot be empty\n";
}
if (!problems.isEmpty()) { if (!problems.isEmpty()) {
System.err.print(problems); System.err.print(problems);
} }
...@@ -66,7 +75,10 @@ public class TestGitLabSession { ...@@ -66,7 +75,10 @@ public class TestGitLabSession {
@Test @Test
public void testSession() throws GitLabApiException { public void testSession() throws GitLabApiException {
GitLabApi gitlabApi = GitLabApi.create(TEST_HOST_URL, TEST_USERNAME, TEST_PASSWORD);
assertNotNull(gitlabApi); GitLabApi gitLabApi = GitLabApi.create(TEST_HOST_URL, TEST_USERNAME, TEST_PASSWORD);
assertNotNull(gitLabApi);
assertNotNull(gitLabApi.getSession());
assertEquals(TEST_PRIVATE_TOKEN, gitLabApi.getSession().getPrivateToken());
} }
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment