Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
佳 邓
Gitlab4j Api
Commits
75a24c23
Commit
75a24c23
authored
May 01, 2019
by
Greg Messner
Browse files
Initial commit (#336).
parent
ab045070
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/gitlab4j/api/utils/AccessTokenUtils.java
0 → 100644
View file @
75a24c23
This diff is collapsed.
Click to expand it.
src/test/java/org/gitlab4j/api/TestAccessTokenUtils.java
0 → 100644
View file @
75a24c23
package
org.gitlab4j.api
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
fail
;
import
static
org
.
junit
.
Assume
.
assumeTrue
;
import
java.util.Arrays
;
import
org.gitlab4j.api.utils.AccessTokenUtils
;
import
org.junit.Before
;
import
org.junit.BeforeClass
;
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_LOGIN_USERNAME
* TEST_LOGIN_PASSWORD
*
* If any of the above are NULL, all tests in this class will be skipped.
*/
public
class
TestAccessTokenUtils
{
// The following needs to be set to your test repository
private
static
final
String
TEST_LOGIN_USERNAME
=
HelperUtils
.
getProperty
(
"TEST_LOGIN_USERNAME"
);
private
static
final
String
TEST_LOGIN_PASSWORD
=
HelperUtils
.
getProperty
(
"TEST_LOGIN_PASSWORD"
);
private
static
final
String
TEST_HOST_URL
=
HelperUtils
.
getProperty
(
"TEST_HOST_URL"
);
private
static
String
problems
=
""
;
public
TestAccessTokenUtils
()
{
super
();
}
@BeforeClass
public
static
void
setup
()
{
problems
=
""
;
if
(
TEST_LOGIN_USERNAME
==
null
||
TEST_LOGIN_USERNAME
.
trim
().
isEmpty
())
{
problems
+=
"TEST_LOGIN_USERNAME cannot be empty\n"
;
}
if
(
TEST_LOGIN_PASSWORD
==
null
||
TEST_LOGIN_PASSWORD
.
trim
().
isEmpty
())
{
problems
+=
"TEST_LOGIN_PASSWORD cannot be empty\n"
;
}
if
(
TEST_HOST_URL
==
null
||
TEST_HOST_URL
.
trim
().
isEmpty
())
{
problems
+=
"TEST_HOST_URL cannot be empty\n"
;
}
if
(!
problems
.
isEmpty
())
{
System
.
err
.
print
(
problems
);
}
}
@Before
public
void
beforeMethod
()
{
assumeTrue
(
problems
.
isEmpty
());
}
@Test
public
void
testCreatePersonalAccessToken
()
throws
GitLabApiException
{
final
String
tokenName
=
"Testing Token Creation-"
+
HelperUtils
.
getRandomInt
(
1000
);
String
accessToken
=
AccessTokenUtils
.
createPersonalAccessToken
(
TEST_HOST_URL
,
TEST_LOGIN_USERNAME
,
TEST_LOGIN_PASSWORD
,
tokenName
,
Arrays
.
asList
(
"api"
,
"sudo"
));
System
.
out
.
println
(
"Created personal access token: "
+
accessToken
);
assertNotNull
(
accessToken
);
assertFalse
(
accessToken
.
trim
().
isEmpty
());
// Go ahead and revoke (delete) the just created access token
try
{
AccessTokenUtils
.
revokePersonalAccessToken
(
TEST_HOST_URL
,
TEST_LOGIN_USERNAME
,
TEST_LOGIN_PASSWORD
,
tokenName
,
Arrays
.
asList
(
"api"
,
"sudo"
));
System
.
out
.
println
(
"Revoked personal access token: "
+
accessToken
);
}
catch
(
Exception
ignore
)
{}
}
@Test
public
void
testCreatePersonalAccessTokenFailedLogin
()
{
try
{
AccessTokenUtils
.
createPersonalAccessToken
(
TEST_HOST_URL
,
TEST_LOGIN_USERNAME
,
"INVALID PASSWORD"
,
"Testing Token Creation"
,
Arrays
.
asList
(
"api"
,
"sudo"
));
fail
(
"Expected a failure, but personal access token was created."
);
}
catch
(
GitLabApiException
glae
)
{
assertEquals
(
401
,
glae
.
getHttpStatus
());
}
}
@Test
public
void
testRevokePersonalAccessToken
()
throws
GitLabApiException
{
final
String
tokenName
=
"Testing Token Revoke-"
+
HelperUtils
.
getRandomInt
(
1000
);
String
accessToken
=
AccessTokenUtils
.
createPersonalAccessToken
(
TEST_HOST_URL
,
TEST_LOGIN_USERNAME
,
TEST_LOGIN_PASSWORD
,
tokenName
,
Arrays
.
asList
(
"api"
,
"sudo"
));
System
.
out
.
println
(
"Created personal access token: "
+
accessToken
);
assertNotNull
(
accessToken
);
assertFalse
(
accessToken
.
trim
().
isEmpty
());
AccessTokenUtils
.
revokePersonalAccessToken
(
TEST_HOST_URL
,
TEST_LOGIN_USERNAME
,
TEST_LOGIN_PASSWORD
,
tokenName
,
Arrays
.
asList
(
"api"
,
"sudo"
));
System
.
out
.
println
(
"Revoked personal access token: "
+
accessToken
);
}
@Test
public
void
testGetFeedToken
()
throws
GitLabApiException
{
String
feedToken
=
AccessTokenUtils
.
getFeedToken
(
TEST_HOST_URL
,
TEST_LOGIN_USERNAME
,
TEST_LOGIN_PASSWORD
);
System
.
out
.
println
(
"Fetched Feed token: "
+
feedToken
);
assertNotNull
(
feedToken
);
assertFalse
(
feedToken
.
trim
().
isEmpty
());
}
@Test
public
void
testGetHealthCheckAccessToken
()
throws
GitLabApiException
{
String
accessToken
=
AccessTokenUtils
.
getHealthCheckAccessToken
(
TEST_HOST_URL
,
TEST_LOGIN_USERNAME
,
TEST_LOGIN_PASSWORD
);
System
.
out
.
println
(
"Fetched health check access token: "
+
accessToken
);
assertNotNull
(
accessToken
);
assertFalse
(
accessToken
.
trim
().
isEmpty
());
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment