forked from gitlab4j/gitlab4j-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestDeployTokensApi.java
137 lines (110 loc) · 4.79 KB
/
TestDeployTokensApi.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package org.gitlab4j.api;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import org.gitlab4j.api.Constants.DeployTokenScope;
import org.gitlab4j.api.models.DeployToken;
import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.Project;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
/**
* 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.
*
*/
@Tag("integration")
@ExtendWith(SetupIntegrationTestExtension.class)
@org.junit.jupiter.api.Disabled("Integration tests are disabled, see https://github.com/gitlab4j/gitlab4j-api/issues/1165")
public class TestDeployTokensApi extends AbstractIntegrationTest {
// The following needs to be set to your test repository
private static final String TEST_USERNAME = HelperUtils.getProperty(USERNAME_KEY);
private static GitLabApi gitLabApi;
private static Group testGroup;
private static Project testProject;
public TestDeployTokensApi() {
super();
}
@BeforeAll
public static void setup() {
// Must setup the connection to the GitLab test server
gitLabApi = baseTestSetup();
testProject = getTestProject();
String problems = "";
if (gitLabApi != null) {
Optional<Group> group = gitLabApi.getGroupApi().getOptionalGroup(TEST_GROUP);
if (group.isPresent()) {
testGroup = group.get();
} else {
problems += "Problem fetching test group\n";
}
}
if (!problems.isEmpty()) {
System.err.print(problems);
}
if (TEST_USERNAME == null || TEST_USERNAME.trim().isEmpty()) {
System.err.println("TEST_USER_NAME cannot be empty");
}
}
@BeforeEach
public void beforeMethod() {
assumeTrue(gitLabApi != null);
assumeTrue(testGroup != null);
assumeTrue(testProject != null);
}
@Test
public void testCreateProject() throws GitLabApiException {
assertNotNull(testProject);
String name = "token-test-" + HelperUtils.getRandomInt(1000);
int initialSize = gitLabApi.getDeployTokensApi().getProjectDeployTokens(testProject).size();
DeployToken test = gitLabApi.getDeployTokensApi().addProjectDeployToken(
testProject,
name,
Date.from(Instant.now().plus(1, ChronoUnit.DAYS)),
"test-user-name", // Currently ignored by the API but correction is on the way
// See: https://gitlab.com/gitlab-org/gitlab/-/issues/211963
Collections.singletonList(DeployTokenScope.READ_REGISTRY));
assertNotNull(test);
assertEquals(test.getName(), name);
assertNotNull(test.getToken());
assertNotEquals(test.getToken(), "");
gitLabApi.getDeployTokensApi().deleteProjectDeployToken(testProject, test.getId());
assertEquals(initialSize, gitLabApi.getDeployTokensApi().getProjectDeployTokens(testProject).size());
}
@Test
public void testCreateGroup() throws GitLabApiException {
assertNotNull(testGroup);
String name = "token-test-" + HelperUtils.getRandomInt(1000);
int initialSize = gitLabApi.getDeployTokensApi().getGroupDeployTokens(testGroup).size();
DeployToken test = gitLabApi.getDeployTokensApi().addGroupDeployToken(
testGroup,
name,
Date.from(Instant.now().plus(1, ChronoUnit.DAYS)),
"test-user-name", // Currently ignored by the API but correction is on the way
// See: https://gitlab.com/gitlab-org/gitlab/-/issues/211963
Arrays.asList(DeployTokenScope.READ_REPOSITORY, DeployTokenScope.READ_REGISTRY));
assertNotNull(test);
assertEquals(test.getName(), name);
assertNotNull(test.getToken());
assertNotEquals(test.getToken(), "");
gitLabApi.getDeployTokensApi().deleteGroupDeployToken(testGroup, test.getId());
List<DeployToken> deployTokens = gitLabApi.getDeployTokensApi().getGroupDeployTokens(testGroup);
assertEquals(initialSize, deployTokens.size());
}
}