Commit 7362bf6c authored by bitsofinfo's avatar bitsofinfo

support for user crud

parent 37391356
...@@ -37,6 +37,7 @@ public class GitlabAPI { ...@@ -37,6 +37,7 @@ public class GitlabAPI {
private final String _apiToken; private final String _apiToken;
private boolean _ignoreCertificateErrors = false; private boolean _ignoreCertificateErrors = false;
private static final String API_NAMESPACE = "/api/v3"; private static final String API_NAMESPACE = "/api/v3";
private static final String PARAM_SUDO = "sudo";
public static final ObjectMapper MAPPER = new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); public static final ObjectMapper MAPPER = new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
private GitlabAPI(String hostUrl, String apiToken) { private GitlabAPI(String hostUrl, String apiToken) {
...@@ -101,6 +102,122 @@ public class GitlabAPI { ...@@ -101,6 +102,122 @@ public class GitlabAPI {
return retrieve().to(tailUrl, GitlabUser.class); return retrieve().to(tailUrl, GitlabUser.class);
} }
public GitlabUser getUserViaSudo(String username) throws IOException {
String tailUrl = GitlabUser.USER_URL + "?" + PARAM_SUDO + "=" + username;
return retrieve().to(tailUrl, GitlabUser.class);
}
/**
* Create a new User
*
* @see http://doc.gitlab.com/ce/api/users.html
*
* @param email
* @param password
* @param username
* @param fullName
* @param skype
* @param linkedIn
* @param twitter
* @param website_url
* @param projects_limit
* @param extern_uid
* @param extern_provider_name
* @param bio
* @param isAdmin
* @param can_create_group
* @return
* @throws IOException
*/
public GitlabUser createUser(String email, String password, String username,
String fullName, String skypeId, String linkedIn,
String twitter, String website_url, Integer projects_limit,
String extern_uid, String extern_provider_name,
String bio, Boolean isAdmin, Boolean can_create_group) throws IOException {
Query query = new Query()
.append("email", email)
.appendIf("password", password)
.appendIf("username", username)
.appendIf("name", fullName)
.appendIf("skype", skypeId)
.appendIf("linkedIn", linkedIn)
.appendIf("twitter", twitter)
.appendIf("website_url", website_url)
.appendIf("projects_limit", (projects_limit != null ? projects_limit : null))
.appendIf("extern_uid", extern_uid)
.appendIf("extern_provider_name", extern_provider_name)
.appendIf("bio",bio)
.appendIf("isAdmin",bio)
.appendIf("can_create_group",can_create_group);
String tailUrl = GitlabUser.USERS_URL + query.toString();
return dispatch().to(tailUrl, GitlabUser.class);
}
/**
* Update a user
*
* @param targetUserId
* @param email
* @param password
* @param username
* @param fullName
* @param skypeId
* @param linkedIn
* @param twitter
* @param website_url
* @param projects_limit
* @param extern_uid
* @param extern_provider_name
* @param bio
* @param isAdmin
* @param can_create_group
* @return
* @throws IOException
*/
public GitlabUser updateUser(Integer targetUserId,
String email, String password, String username,
String fullName, String skypeId, String linkedIn,
String twitter, String website_url, Integer projects_limit,
String extern_uid, String extern_provider_name,
String bio, Boolean isAdmin, Boolean can_create_group) throws IOException {
Query query = new Query()
.append("email", email)
.appendIf("password", password)
.appendIf("username", username)
.appendIf("name", fullName)
.appendIf("skype", skypeId)
.appendIf("linkedIn", linkedIn)
.appendIf("twitter", twitter)
.appendIf("website_url", (website_url != null ?website_url.toString(): null))
.appendIf("projects_limit", (projects_limit != null ? projects_limit : null))
.appendIf("extern_uid", extern_uid)
.appendIf("extern_provider_name", extern_provider_name)
.appendIf("bio",bio)
.appendIf("isAdmin",bio)
.appendIf("can_create_group",can_create_group);
String tailUrl = GitlabUser.USERS_URL + "/"+targetUserId + query.toString();
return retrieve().method("PUT").to(tailUrl, GitlabUser.class);
}
/**
* Delete a user
*
* @param targetUserId
* @throws IOException
*/
public void deleteUser(Integer targetUserId) throws IOException {
String tailUrl = GitlabUser.USERS_URL + "/"+targetUserId;
retrieve().method("DELETE").to(tailUrl, Void.class);
}
public GitlabGroup getGroup(Integer groupId) throws IOException { public GitlabGroup getGroup(Integer groupId) throws IOException {
String tailUrl = GitlabGroup.URL + "/" + groupId; String tailUrl = GitlabGroup.URL + "/" + groupId;
return retrieve().to(tailUrl, GitlabGroup.class); return retrieve().to(tailUrl, GitlabGroup.class);
......
...@@ -5,6 +5,8 @@ import org.codehaus.jackson.annotate.JsonProperty; ...@@ -5,6 +5,8 @@ import org.codehaus.jackson.annotate.JsonProperty;
public class GitlabUser { public class GitlabUser {
public static String URL = "/users"; public static String URL = "/users";
public static String USERS_URL = "/users";
public static String USER_URL = "/user"; // for sudo based ops
private Integer _id; private Integer _id;
private String _username; private String _username;
...@@ -17,6 +19,12 @@ public class GitlabUser { ...@@ -17,6 +19,12 @@ public class GitlabUser {
private String _state; private String _state;
private boolean _blocked; private boolean _blocked;
@JsonProperty("provider")
private String _externProviderName;
@JsonProperty("website_url")
private String _websiteUrl;
@JsonProperty("created_at") @JsonProperty("created_at")
private Date _createdAt; private Date _createdAt;
...@@ -164,6 +172,22 @@ public class GitlabUser { ...@@ -164,6 +172,22 @@ public class GitlabUser {
_state = state; _state = state;
} }
public String getExternProviderName() {
return _state;
}
public void setExternProviderName(String externProviderName) {
_externProviderName = externProviderName;
}
public String getWebsiteUrl() {
return _websiteUrl;
}
public void setWebsiteUrl(String websiteUrl) {
_websiteUrl = websiteUrl;
}
public boolean isAdmin() { public boolean isAdmin() {
return _isAdmin; return _isAdmin;
} }
......
package org.gitlab.api; package org.gitlab.api;
import org.gitlab.api.models.GitlabUser;
import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.UUID;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
...@@ -12,9 +16,16 @@ public class GitlabAPITest { ...@@ -12,9 +16,16 @@ public class GitlabAPITest {
GitlabAPI _api; GitlabAPI _api;
private static final String TEST_URL = "http://10.216.201.236";
private static final String TEST_TOKEN = "ePgczFTuDuQupFhuG4HU";
String rand = UUID.randomUUID().toString().replace("-", "").substring(0, 8);
@Before @Before
public void setup() { public void setup() {
_api = GitlabAPI.connect("http://localhost", "token"); _api = GitlabAPI.connect(TEST_URL, TEST_TOKEN);
} }
@Test @Test
...@@ -24,13 +35,72 @@ public class GitlabAPITest { ...@@ -24,13 +35,72 @@ public class GitlabAPITest {
@Test @Test
public void testGetAPIUrl() throws IOException { public void testGetAPIUrl() throws IOException {
URL expected = new URL("http://localhost/api/v3/?private_token=token"); URL expected = new URL(TEST_URL+"/api/v3/?private_token="+TEST_TOKEN);
assertEquals(expected, _api.getAPIUrl("")); assertEquals(expected, _api.getAPIUrl(""));
} }
@Test @Test
public void testGetUrl() throws IOException { public void testGetUrl() throws IOException {
URL expected = new URL("http://localhost/"); URL expected = new URL(TEST_URL);
assertEquals(expected, _api.getUrl("")); assertEquals(expected +"/", _api.getUrl("").toString());
}
@Test
public void testCreateUpdateDeleteUser() throws IOException {
String password = randVal("$%password");
GitlabUser gitUser = _api.createUser(randVal("testEmail@gitlabapitest.com"),
password,
randVal("userName"),
randVal("fullName"),
randVal("skypeId"),
randVal("linledin"),
randVal("twitter"),
"http://"+randVal("url.com"),
10,
randVal("externuid"),
randVal("externprovidername"),
randVal("bio"),
false,
false);
Assert.assertNotNull(gitUser);
GitlabUser refetched = _api.getUserViaSudo(gitUser.getUsername());
Assert.assertNotNull(refetched);
Assert.assertEquals(refetched.getUsername(),gitUser.getUsername());
_api.updateUser(gitUser.getId(), gitUser.getEmail(), password , gitUser.getUsername(),
gitUser.getName(), "newSkypeId", gitUser.getLinkedin(), gitUser.getTwitter(), gitUser.getWebsiteUrl(),
10 /* project limit does not come back on GET */, gitUser.getExternUid(), gitUser.getExternProviderName(),
gitUser.getBio(), gitUser.isAdmin(), gitUser.isCanCreateGroup());
GitlabUser postUpdate = _api.getUserViaSudo(gitUser.getUsername());
Assert.assertNotNull(postUpdate);
Assert.assertEquals(postUpdate.getSkype(),"newSkypeId");
_api.deleteUser(postUpdate.getId());
// expect a 404, but we have no access to it
try {
GitlabUser shouldNotExist = _api.getUser(postUpdate.getId());
Assert.assertNull(shouldNotExist); // should never even get here
} catch(FileNotFoundException thisIsSoOddForAnRESTApiClient) {
Assert.assertTrue(true); // expected
}
}
private String randVal(String postfix) {
return rand + "-" + postfix;
} }
} }
Markdown is supported
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