Unverified Commit 22679222 authored by Greg Messner's avatar Greg Messner Committed by GitHub
Browse files

Feature/protected branch and example test properties (#156)

* Minor mods to protected branch functionality.
* Cleaned up example-test-gitlab4j.properties and related tests.
parent e5350665
# Rename this file to test-gitlab4j.properties with proper property values. #
TEST_NAMESPACE=some-namespace # To test GitLab4J-API, you must have access to a GitLab server.
TEST_PROJECT_NAME=some-project-name #
TEST_HOST_URL=some-gitlab-url # Copy this file to test-gitlab4j.properties and set the property
TEST_PRIVATE_TOKEN=some-private-token # values to match the GitLab server you are testing against.
#
# REQUIRED: The following values must be set or the tests will fail.
# The TEST_PRIVATE_TOKEN and TEST_USERNAME must belong to an admin user.
TEST_PRIVATE_TOKEN=
TEST_ACCESS_TOKEN=
TEST_USERNAME=
TEST_NAMESPACE=
TEST_PROJECT_NAME=
TEST_HOST_URL=
# OPTIONAL: To test the GroupApi, set these to an existing group name and group-project name,
# and the username of a group member
TEST_GROUP=
TEST_GROUP_PROJECT=
TEST_GROUP_MEMBER_USERNAME=
# OPTIONAL: To test oauth2Login, set these properties
TEST_LOGIN_USERNAME=
TEST_LOGIN_PASSWORD=
# OPTIONAL: To test sudo capability provide a username to sudo as
TEST_SUDO_AS_USERNAME=
# OPTIONAL: To test using GitLab4J-API with a proxy, set the following properties
TEST_PROXY_URI=
TEST_PROXY_USERNAME=
TEST_PROXY_PASSWORD=
...@@ -9,6 +9,7 @@ import javax.ws.rs.core.Response; ...@@ -9,6 +9,7 @@ import javax.ws.rs.core.Response;
import java.util.List; import java.util.List;
public class ProtectedBranchesApi extends AbstractApi { public class ProtectedBranchesApi extends AbstractApi {
public ProtectedBranchesApi(GitLabApi gitLabApi) { public ProtectedBranchesApi(GitLabApi gitLabApi) {
super(gitLabApi); super(gitLabApi);
} }
......
...@@ -15,7 +15,6 @@ import javax.ws.rs.core.MediaType; ...@@ -15,7 +15,6 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import org.gitlab4j.api.GitLabApi.ApiVersion; import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.AccessLevel;
import org.gitlab4j.api.models.Branch; import org.gitlab4j.api.models.Branch;
import org.gitlab4j.api.models.CompareResults; import org.gitlab4j.api.models.CompareResults;
import org.gitlab4j.api.models.Tag; import org.gitlab4j.api.models.Tag;
......
...@@ -6,15 +6,16 @@ import javax.xml.bind.annotation.XmlRootElement; ...@@ -6,15 +6,16 @@ import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement @XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD) @XmlAccessorType(XmlAccessType.FIELD)
public class BranchAccessLevelDetail { public class BranchAccessLevel {
private String accessLevel;
private AccessLevel accessLevel;
private String accessLevelDescription; private String accessLevelDescription;
public String getAccessLevel() { public AccessLevel getAccessLevel() {
return this.accessLevel; return this.accessLevel;
} }
public void setAccessLevel(String accessLevel) { public void setAccessLevel(AccessLevel accessLevel) {
this.accessLevel = accessLevel; this.accessLevel = accessLevel;
} }
......
...@@ -9,9 +9,10 @@ import java.util.List; ...@@ -9,9 +9,10 @@ import java.util.List;
@XmlRootElement @XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD) @XmlAccessorType(XmlAccessType.FIELD)
public class ProtectedBranch { public class ProtectedBranch {
private String name; private String name;
private List<BranchAccessLevelDetail> pushAccessLevels; private List<BranchAccessLevel> pushAccessLevels;
private List<BranchAccessLevelDetail> mergeAccessLevels; private List<BranchAccessLevel> mergeAccessLevels;
public String getName() { public String getName() {
return this.name; return this.name;
...@@ -21,19 +22,19 @@ public class ProtectedBranch { ...@@ -21,19 +22,19 @@ public class ProtectedBranch {
this.name = name; this.name = name;
} }
public List<BranchAccessLevelDetail> getPushAccessLevels() { public List<BranchAccessLevel> getPushAccessLevels() {
return this.pushAccessLevels; return this.pushAccessLevels;
} }
public void setPushAccessLevels(List<BranchAccessLevelDetail> pushAccessLevels) { public void setPushAccessLevels(List<BranchAccessLevel> pushAccessLevels) {
this.pushAccessLevels = pushAccessLevels; this.pushAccessLevels = pushAccessLevels;
} }
public List<BranchAccessLevelDetail> getMergeAccessLevels() { public List<BranchAccessLevel> getMergeAccessLevels() {
return this.mergeAccessLevels; return this.mergeAccessLevels;
} }
public void setMergeAccessLevels(List<BranchAccessLevelDetail> mergeAccessLevels) { public void setMergeAccessLevels(List<BranchAccessLevel> mergeAccessLevels) {
this.mergeAccessLevels = mergeAccessLevels; this.mergeAccessLevels = mergeAccessLevels;
} }
......
...@@ -77,6 +77,7 @@ public class TestGitLabApi { ...@@ -77,6 +77,7 @@ public class TestGitLabApi {
@Test @Test
public void testProxyConnection() throws GitLabApiException { public void testProxyConnection() throws GitLabApiException {
assumeTrue(TEST_PROXY_URI != null && TEST_PROXY_USERNAME != null && TEST_PROXY_PASSWORD != null); assumeTrue(TEST_PROXY_URI != null && TEST_PROXY_USERNAME != null && TEST_PROXY_PASSWORD != null);
assumeTrue(TEST_PROXY_URI.length() > 0 && TEST_PROXY_USERNAME.length() > 0 && TEST_PROXY_PASSWORD.length() > 0);
// Setup a GitLabApi instance to use a proxy // Setup a GitLabApi instance to use a proxy
Map<String, Object> clientConfig = ProxyClientConfig.createProxyClientConfig(TEST_PROXY_URI, TEST_PROXY_USERNAME, TEST_PROXY_PASSWORD); Map<String, Object> clientConfig = ProxyClientConfig.createProxyClientConfig(TEST_PROXY_URI, TEST_PROXY_USERNAME, TEST_PROXY_PASSWORD);
......
...@@ -54,6 +54,7 @@ import org.gitlab4j.api.models.Pipeline; ...@@ -54,6 +54,7 @@ import org.gitlab4j.api.models.Pipeline;
import org.gitlab4j.api.models.Project; import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.ProjectHook; import org.gitlab4j.api.models.ProjectHook;
import org.gitlab4j.api.models.ProjectUser; import org.gitlab4j.api.models.ProjectUser;
import org.gitlab4j.api.models.ProtectedBranch;
import org.gitlab4j.api.models.Session; import org.gitlab4j.api.models.Session;
import org.gitlab4j.api.models.Snippet; import org.gitlab4j.api.models.Snippet;
import org.gitlab4j.api.models.SshKey; import org.gitlab4j.api.models.SshKey;
...@@ -266,6 +267,17 @@ public class TestGitLabApiBeans { ...@@ -266,6 +267,17 @@ public class TestGitLabApiBeans {
} }
} }
@Test
public void testProtectedBranch() {
try {
ProtectedBranch protectedBranch = makeFakeApiCall(ProtectedBranch.class, "protected-branch");
assertTrue(compareJson(protectedBranch, "protected-branch"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test @Test
public void testKey() { public void testKey() {
......
...@@ -15,8 +15,8 @@ import org.junit.Test; ...@@ -15,8 +15,8 @@ import org.junit.Test;
* In order for these tests to run you must set the following properties in test-gitlab4j.properties * In order for these tests to run you must set the following properties in test-gitlab4j.properties
* *
* TEST_HOST_URL * TEST_HOST_URL
* TEST_USERNAME * TEST_LOGIN_USERNAME
* TEST_PASSWORD * TEST_LOGIN_PASSWORD
* TEST_PRIVATE_TOKEN * TEST_PRIVATE_TOKEN
* *
* If any of the above are NULL, all tests in this class will be skipped. * If any of the above are NULL, all tests in this class will be skipped.
...@@ -24,13 +24,13 @@ import org.junit.Test; ...@@ -24,13 +24,13 @@ import org.junit.Test;
public class TestGitLabLogin { public class TestGitLabLogin {
// The following needs to be set to your test repository // The following needs to be set to your test repository
private static final String TEST_USERNAME; private static final String TEST_LOGIN_USERNAME;
private static final String TEST_PASSWORD; private static final String TEST_LOGIN_PASSWORD;
private static final String TEST_HOST_URL; private static final String TEST_HOST_URL;
private static final String TEST_PRIVATE_TOKEN; private static final String TEST_PRIVATE_TOKEN;
static { static {
TEST_USERNAME = TestUtils.getProperty("TEST_USERNAME"); TEST_LOGIN_USERNAME = TestUtils.getProperty("TEST_LOGIN_USERNAME");
TEST_PASSWORD = TestUtils.getProperty("TEST_PASSWORD"); TEST_LOGIN_PASSWORD = TestUtils.getProperty("TEST_LOGIN_PASSWORD");
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL"); TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN"); TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
} }
...@@ -47,12 +47,12 @@ public class TestGitLabLogin { ...@@ -47,12 +47,12 @@ public class TestGitLabLogin {
problems = ""; problems = "";
if (TEST_USERNAME == null || TEST_USERNAME.trim().length() == 0) { if (TEST_LOGIN_USERNAME == null || TEST_LOGIN_USERNAME.trim().length() == 0) {
problems += "TEST_USERNAME cannot be empty\n"; problems += "TEST_LOGIN_USERNAME cannot be empty\n";
} }
if (TEST_PASSWORD == null || TEST_PASSWORD.trim().length() == 0) { if (TEST_LOGIN_PASSWORD == null || TEST_LOGIN_PASSWORD.trim().length() == 0) {
problems += "TEST_PASSWORD cannot be empty\n"; problems += "TEST_LOGIN_PASSWORD cannot be empty\n";
} }
if (TEST_HOST_URL == null || TEST_HOST_URL.trim().length() == 0) { if (TEST_HOST_URL == null || TEST_HOST_URL.trim().length() == 0) {
...@@ -93,7 +93,7 @@ public class TestGitLabLogin { ...@@ -93,7 +93,7 @@ public class TestGitLabLogin {
public void testSession() throws GitLabApiException { public void testSession() throws GitLabApiException {
assumeTrue(hasSession); assumeTrue(hasSession);
GitLabApi gitLabApi = GitLabApi.login(ApiVersion.V4, TEST_HOST_URL, TEST_USERNAME, TEST_PASSWORD); GitLabApi gitLabApi = GitLabApi.login(ApiVersion.V4, TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD);
assertNotNull(gitLabApi); assertNotNull(gitLabApi);
assertNotNull(gitLabApi.getSession()); assertNotNull(gitLabApi.getSession());
assertEquals(TEST_PRIVATE_TOKEN, gitLabApi.getSession().getPrivateToken()); assertEquals(TEST_PRIVATE_TOKEN, gitLabApi.getSession().getPrivateToken());
...@@ -104,7 +104,7 @@ public class TestGitLabLogin { ...@@ -104,7 +104,7 @@ public class TestGitLabLogin {
public void testSessionV3() throws GitLabApiException { public void testSessionV3() throws GitLabApiException {
assumeTrue(hasSession); assumeTrue(hasSession);
GitLabApi gitLabApi = GitLabApi.login(ApiVersion.V3, TEST_HOST_URL, TEST_USERNAME, TEST_PASSWORD); GitLabApi gitLabApi = GitLabApi.login(ApiVersion.V3, TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD);
assertNotNull(gitLabApi); assertNotNull(gitLabApi);
assertNotNull(gitLabApi.getSession()); assertNotNull(gitLabApi.getSession());
assertEquals(TEST_PRIVATE_TOKEN, gitLabApi.getSession().getPrivateToken()); assertEquals(TEST_PRIVATE_TOKEN, gitLabApi.getSession().getPrivateToken());
...@@ -114,7 +114,7 @@ public class TestGitLabLogin { ...@@ -114,7 +114,7 @@ public class TestGitLabLogin {
public void testSessionFallover() throws GitLabApiException { public void testSessionFallover() throws GitLabApiException {
assumeFalse(hasSession); assumeFalse(hasSession);
GitLabApi gitLabApi = GitLabApi.login(ApiVersion.V4, TEST_HOST_URL, TEST_USERNAME, TEST_PASSWORD); GitLabApi gitLabApi = GitLabApi.login(ApiVersion.V4, TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD);
assertNotNull(gitLabApi); assertNotNull(gitLabApi);
Version version = gitLabApi.getVersion(); Version version = gitLabApi.getVersion();
assertNotNull(version); assertNotNull(version);
...@@ -123,7 +123,7 @@ public class TestGitLabLogin { ...@@ -123,7 +123,7 @@ public class TestGitLabLogin {
@Test @Test
public void testOauth2Login() throws GitLabApiException { public void testOauth2Login() throws GitLabApiException {
GitLabApi gitLabApi = GitLabApi.oauth2Login(TEST_HOST_URL, TEST_USERNAME, TEST_PASSWORD, null, null, true); GitLabApi gitLabApi = GitLabApi.oauth2Login(TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD, null, null, true);
assertNotNull(gitLabApi); assertNotNull(gitLabApi);
Version version = gitLabApi.getVersion(); Version version = gitLabApi.getVersion();
assertNotNull(version); assertNotNull(version);
......
...@@ -83,10 +83,12 @@ public class TestGroupApi { ...@@ -83,10 +83,12 @@ public class TestGroupApi {
problems += "Problem fetching test group, error=" + gle.getMessage() + "\n"; problems += "Problem fetching test group, error=" + gle.getMessage() + "\n";
} }
try { if (TEST_GROUP_MEMBER_USERNAME != null && TEST_GROUP_MEMBER_USERNAME.length() > 0) {
testUser = gitLabApi.getUserApi().getUser(TEST_GROUP_MEMBER_USERNAME); try {
} catch (GitLabApiException gle) { testUser = gitLabApi.getUserApi().getUser(TEST_GROUP_MEMBER_USERNAME);
problems += "Problem fetching test user, error=" + gle.getMessage() + "\n"; } catch (GitLabApiException gle) {
problems += "Problem fetching test user, error=" + gle.getMessage() + "\n";
}
} }
} }
......
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.assertTrue;
import static org.junit.Assume.assumeTrue;
import java.util.List;
import org.gitlab4j.api.models.Branch; import org.gitlab4j.api.models.Branch;
import org.gitlab4j.api.models.Project; import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.ProtectedBranch; import org.gitlab4j.api.models.ProtectedBranch;
...@@ -10,13 +17,6 @@ import org.junit.FixMethodOrder; ...@@ -10,13 +17,6 @@ import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
import java.util.List;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
/** /**
* In order for these tests to run you must set the following properties in test-gitlab4j.properties * In order for these tests to run you must set the following properties in test-gitlab4j.properties
* *
...@@ -154,6 +154,9 @@ public class TestProtectedBranchesApi { ...@@ -154,6 +154,9 @@ public class TestProtectedBranchesApi {
assertNotNull(project); assertNotNull(project);
ProtectedBranch branch = gitLabApi.getProtectedBranchesApi().protectBranch(project.getId(), TEST_BRANCH_NAME); ProtectedBranch branch = gitLabApi.getProtectedBranchesApi().protectBranch(project.getId(), TEST_BRANCH_NAME);
assertNotNull(branch);
assertEquals(TEST_BRANCH_NAME, branch.getName());
List<ProtectedBranch> branches = gitLabApi.getProtectedBranchesApi().getProtectedBranches(project.getId()); List<ProtectedBranch> branches = gitLabApi.getProtectedBranchesApi().getProtectedBranches(project.getId());
assertNotNull(branches); assertNotNull(branches);
assertTrue(branches.stream() assertTrue(branches.stream()
......
...@@ -86,13 +86,14 @@ public class TestRepositoryApi { ...@@ -86,13 +86,14 @@ public class TestRepositoryApi {
if (problems.isEmpty()) { if (problems.isEmpty()) {
gitLabApi = new GitLabApi(ApiVersion.V3, TEST_HOST_URL, TEST_PRIVATE_TOKEN); gitLabApi = new GitLabApi(ApiVersion.V3, TEST_HOST_URL, TEST_PRIVATE_TOKEN);
teardown();
} else { } else {
System.err.print(problems); System.err.print(problems);
} }
} }
@AfterClass @AfterClass
public static void teardown() throws GitLabApiException { public static void teardown() {
if (gitLabApi != null) { if (gitLabApi != null) {
try { try {
......
...@@ -18,11 +18,10 @@ import org.junit.runners.MethodSorters; ...@@ -18,11 +18,10 @@ import org.junit.runners.MethodSorters;
/** /**
* In order for these tests to run you must set the following properties in test-gitlab4j.properties * In order for these tests to run you must set the following properties in test-gitlab4j.properties
* *
* TEST_HOOK_URL
* TEST_HOST_URL * TEST_HOST_URL
* TEST_PRIVATE_TOKEN * TEST_PRIVATE_TOKEN
* *
* If any of the above are NULL, all tests in this class will be skipped. * If any of the above are NULL, all tests in this class will be skipped.
*/ */
@FixMethodOrder(MethodSorters.NAME_ASCENDING) @FixMethodOrder(MethodSorters.NAME_ASCENDING)
...@@ -34,7 +33,7 @@ public class TestSystemHooksApi { ...@@ -34,7 +33,7 @@ public class TestSystemHooksApi {
private static final String TEST_PRIVATE_TOKEN; private static final String TEST_PRIVATE_TOKEN;
static { static {
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL"); TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
TEST_HOOK_URL = TestUtils.getProperty("TEST_HOOK_URL"); TEST_HOOK_URL = "http://hook.example.com/hook/callback";
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN"); TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
} }
......
...@@ -27,11 +27,11 @@ import org.junit.Test; ...@@ -27,11 +27,11 @@ import org.junit.Test;
/** /**
* In order for these tests to run you must set the following properties in test-gitlab4j.properties * In order for these tests to run you must set the following properties in test-gitlab4j.properties
* *
* TEST_HOST_URL * TEST_HOST_URL
* TEST_PRIVATE_TOKEN * TEST_PRIVATE_TOKEN
* TEST_USERNAME * TEST_USERNAME
* *
* If any of the above are NULL, all tests in this class will be skipped. * If any of the above are NULL, all tests in this class will be skipped.
* *
* TEST_SUDO_AS_USERNAME * TEST_SUDO_AS_USERNAME
...@@ -56,7 +56,11 @@ public class TestUserApi { ...@@ -56,7 +56,11 @@ public class TestUserApi {
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN"); TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
TEST_USERNAME = TestUtils.getProperty("TEST_USERNAME"); TEST_USERNAME = TestUtils.getProperty("TEST_USERNAME");
TEST_SUDO_AS_USERNAME = TestUtils.getProperty("TEST_SUDO_AS_USERNAME"); TEST_SUDO_AS_USERNAME = TestUtils.getProperty("TEST_SUDO_AS_USERNAME");
TEST_SSH_KEY = TestUtils.getProperty("TEST_SSH_KEY"); TEST_SSH_KEY = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCvbkmGRaANy2nmLrfYa9LkjMqjs9twYZXQKUPK18j" +
"BWmNgnAm818IikxjfFit3Gqnnh9zdNzlzUYs2osmfdHwRLeFY3hKVR6WckGYVroQuV5ArUA4+oME+IIQ2soCv/" +
"vNWfEmp2N1mpBTwi2mIYKurCKv6UpIpGK9D+ezNk5H0waVTK8EvZ/ey69Nu7C7RsbTYeyi5WY/jaUG5JbsEeKY" +
"IW/2DIlUts7gcB2hzXtt7r7+6DLx82Vb+S2jPZu2JQaB4zfgS7LQgzHUy1aAAgUUpuAbvWzuGHKO0p551Ru4qi" +
"tyXN2+OUVXcYAsuIIdGGB0wLvTDgiOOSZWnSE+sg6XX user@example.com";
} }
private static final String TEST_IMPERSONATION_TOKEN_NAME = "token1"; private static final String TEST_IMPERSONATION_TOKEN_NAME = "token1";
...@@ -149,7 +153,7 @@ public class TestUserApi { ...@@ -149,7 +153,7 @@ public class TestUserApi {
@Test @Test
public void testSudoAsUser() throws GitLabApiException { public void testSudoAsUser() throws GitLabApiException {
assumeTrue(TEST_SUDO_AS_USERNAME != null); assumeTrue(TEST_SUDO_AS_USERNAME != null && TEST_SUDO_AS_USERNAME.length() > 0);
try { try {
......
{
"name": "develop",
"push_access_levels": [
{
"access_level": 40,
"access_level_description": "Masters"
},
{
"access_level": 30,
"access_level_description": "Developer access"
}
],
"merge_access_levels": [
{
"access_level": 40,
"access_level_description": "Masters"
},
{
"access_level": 30,
"access_level_description": "Developer access"
}
]
}
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