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
TEST_PROJECT_NAME=some-project-name
TEST_HOST_URL=some-gitlab-url
TEST_PRIVATE_TOKEN=some-private-token
#
# To test GitLab4J-API, you must have access to a GitLab server.
#
# Copy this file to test-gitlab4j.properties and set the property
# 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;
import java.util.List;
public class ProtectedBranchesApi extends AbstractApi {
public ProtectedBranchesApi(GitLabApi gitLabApi) {
super(gitLabApi);
}
......
......@@ -15,7 +15,6 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.AccessLevel;
import org.gitlab4j.api.models.Branch;
import org.gitlab4j.api.models.CompareResults;
import org.gitlab4j.api.models.Tag;
......
......@@ -6,15 +6,16 @@ import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class BranchAccessLevelDetail {
private String accessLevel;
public class BranchAccessLevel {
private AccessLevel accessLevel;
private String accessLevelDescription;
public String getAccessLevel() {
public AccessLevel getAccessLevel() {
return this.accessLevel;
}
public void setAccessLevel(String accessLevel) {
public void setAccessLevel(AccessLevel accessLevel) {
this.accessLevel = accessLevel;
}
......
......@@ -9,9 +9,10 @@ import java.util.List;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ProtectedBranch {
private String name;
private List<BranchAccessLevelDetail> pushAccessLevels;
private List<BranchAccessLevelDetail> mergeAccessLevels;
private List<BranchAccessLevel> pushAccessLevels;
private List<BranchAccessLevel> mergeAccessLevels;
public String getName() {
return this.name;
......@@ -21,19 +22,19 @@ public class ProtectedBranch {
this.name = name;
}
public List<BranchAccessLevelDetail> getPushAccessLevels() {
public List<BranchAccessLevel> getPushAccessLevels() {
return this.pushAccessLevels;
}
public void setPushAccessLevels(List<BranchAccessLevelDetail> pushAccessLevels) {
public void setPushAccessLevels(List<BranchAccessLevel> pushAccessLevels) {
this.pushAccessLevels = pushAccessLevels;
}
public List<BranchAccessLevelDetail> getMergeAccessLevels() {
public List<BranchAccessLevel> getMergeAccessLevels() {
return this.mergeAccessLevels;
}
public void setMergeAccessLevels(List<BranchAccessLevelDetail> mergeAccessLevels) {
public void setMergeAccessLevels(List<BranchAccessLevel> mergeAccessLevels) {
this.mergeAccessLevels = mergeAccessLevels;
}
......
......@@ -77,6 +77,7 @@ public class TestGitLabApi {
@Test
public void testProxyConnection() throws GitLabApiException {
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
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;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.ProjectHook;
import org.gitlab4j.api.models.ProjectUser;
import org.gitlab4j.api.models.ProtectedBranch;
import org.gitlab4j.api.models.Session;
import org.gitlab4j.api.models.Snippet;
import org.gitlab4j.api.models.SshKey;
......@@ -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
public void testKey() {
......
......@@ -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
*
* TEST_HOST_URL
* TEST_USERNAME
* TEST_PASSWORD
* TEST_LOGIN_USERNAME
* TEST_LOGIN_PASSWORD
* TEST_PRIVATE_TOKEN
*
* If any of the above are NULL, all tests in this class will be skipped.
......@@ -24,13 +24,13 @@ import org.junit.Test;
public class TestGitLabLogin {
// The following needs to be set to your test repository
private static final String TEST_USERNAME;
private static final String TEST_PASSWORD;
private static final String TEST_LOGIN_USERNAME;
private static final String TEST_LOGIN_PASSWORD;
private static final String TEST_HOST_URL;
private static final String TEST_PRIVATE_TOKEN;
static {
TEST_USERNAME = TestUtils.getProperty("TEST_USERNAME");
TEST_PASSWORD = TestUtils.getProperty("TEST_PASSWORD");
TEST_LOGIN_USERNAME = TestUtils.getProperty("TEST_LOGIN_USERNAME");
TEST_LOGIN_PASSWORD = TestUtils.getProperty("TEST_LOGIN_PASSWORD");
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
}
......@@ -47,12 +47,12 @@ public class TestGitLabLogin {
problems = "";
if (TEST_USERNAME == null || TEST_USERNAME.trim().length() == 0) {
problems += "TEST_USERNAME cannot be empty\n";
if (TEST_LOGIN_USERNAME == null || TEST_LOGIN_USERNAME.trim().length() == 0) {
problems += "TEST_LOGIN_USERNAME cannot be empty\n";
}
if (TEST_PASSWORD == null || TEST_PASSWORD.trim().length() == 0) {
problems += "TEST_PASSWORD cannot be empty\n";
if (TEST_LOGIN_PASSWORD == null || TEST_LOGIN_PASSWORD.trim().length() == 0) {
problems += "TEST_LOGIN_PASSWORD cannot be empty\n";
}
if (TEST_HOST_URL == null || TEST_HOST_URL.trim().length() == 0) {
......@@ -93,7 +93,7 @@ public class TestGitLabLogin {
public void testSession() throws GitLabApiException {
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.getSession());
assertEquals(TEST_PRIVATE_TOKEN, gitLabApi.getSession().getPrivateToken());
......@@ -104,7 +104,7 @@ public class TestGitLabLogin {
public void testSessionV3() throws GitLabApiException {
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.getSession());
assertEquals(TEST_PRIVATE_TOKEN, gitLabApi.getSession().getPrivateToken());
......@@ -114,7 +114,7 @@ public class TestGitLabLogin {
public void testSessionFallover() throws GitLabApiException {
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);
Version version = gitLabApi.getVersion();
assertNotNull(version);
......@@ -123,7 +123,7 @@ public class TestGitLabLogin {
@Test
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);
Version version = gitLabApi.getVersion();
assertNotNull(version);
......
......@@ -83,10 +83,12 @@ public class TestGroupApi {
problems += "Problem fetching test group, error=" + gle.getMessage() + "\n";
}
try {
testUser = gitLabApi.getUserApi().getUser(TEST_GROUP_MEMBER_USERNAME);
} catch (GitLabApiException gle) {
problems += "Problem fetching test user, error=" + gle.getMessage() + "\n";
if (TEST_GROUP_MEMBER_USERNAME != null && TEST_GROUP_MEMBER_USERNAME.length() > 0) {
try {
testUser = gitLabApi.getUserApi().getUser(TEST_GROUP_MEMBER_USERNAME);
} catch (GitLabApiException gle) {
problems += "Problem fetching test user, error=" + gle.getMessage() + "\n";
}
}
}
......
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.Project;
import org.gitlab4j.api.models.ProtectedBranch;
......@@ -10,13 +17,6 @@ import org.junit.FixMethodOrder;
import org.junit.Test;
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
*
......@@ -154,6 +154,9 @@ public class TestProtectedBranchesApi {
assertNotNull(project);
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());
assertNotNull(branches);
assertTrue(branches.stream()
......
......@@ -86,13 +86,14 @@ public class TestRepositoryApi {
if (problems.isEmpty()) {
gitLabApi = new GitLabApi(ApiVersion.V3, TEST_HOST_URL, TEST_PRIVATE_TOKEN);
teardown();
} else {
System.err.print(problems);
}
}
@AfterClass
public static void teardown() throws GitLabApiException {
public static void teardown() {
if (gitLabApi != null) {
try {
......
......@@ -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
*
* TEST_HOOK_URL
*
* TEST_HOST_URL
* TEST_PRIVATE_TOKEN
*
*
* If any of the above are NULL, all tests in this class will be skipped.
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
......@@ -34,7 +33,7 @@ public class TestSystemHooksApi {
private static final String TEST_PRIVATE_TOKEN;
static {
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");
}
......
......@@ -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
*
*
* TEST_HOST_URL
* TEST_PRIVATE_TOKEN
* TEST_USERNAME
*
*
* If any of the above are NULL, all tests in this class will be skipped.
*
* TEST_SUDO_AS_USERNAME
......@@ -56,7 +56,11 @@ public class TestUserApi {
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
TEST_USERNAME = TestUtils.getProperty("TEST_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";
......@@ -149,7 +153,7 @@ public class TestUserApi {
@Test
public void testSudoAsUser() throws GitLabApiException {
assumeTrue(TEST_SUDO_AS_USERNAME != null);
assumeTrue(TEST_SUDO_AS_USERNAME != null && TEST_SUDO_AS_USERNAME.length() > 0);
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