Merge "Support to retrieve an account via REST"

This commit is contained in:
Shawn Pearce
2013-03-21 04:57:05 +00:00
committed by Gerrit Code Review
8 changed files with 173 additions and 7 deletions

View File

@@ -8,6 +8,33 @@ link:rest-api.html[REST API].
Endpoints
---------
[[get-account]]
Get Account
~~~~~~~~~~~
[verse]
'GET /accounts/link:#account-id[\{account-id\}]'
Returns an account as an link:#account-info[AccountInfo] entity.
.Request
----
GET /accounts/self HTTP/1.0
----
.Response
----
HTTP/1.1 200 OK
Content-Disposition: attachment
Content-Type: application/json;charset=UTF-8
)]}'
{
"_account_id": 1000096,
"name": "John Doe",
"email": "john.doe@example.com"
}
----
[[list-account-capabilities]]
List Account Capabilities
~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@@ -0,0 +1,29 @@
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.acceptance.rest.account;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import com.google.gerrit.acceptance.TestAccount;
public class AccountAssert {
public static void assertAccountInfo(TestAccount a, AccountInfo ai) {
assertTrue(a.id.get() == ai._account_id);
assertEquals(a.fullName, ai.name);
assertEquals(a.email, ai.email);
}
}

View File

@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.acceptance.rest.group;
package com.google.gerrit.acceptance.rest.account;
public class AccountInfo {
public Integer _account_id;

View File

@@ -0,0 +1,88 @@
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.acceptance.rest.account;
import static com.google.gerrit.acceptance.rest.account.AccountAssert.assertAccountInfo;
import static org.junit.Assert.assertEquals;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.AccountCreator;
import com.google.gerrit.acceptance.RestResponse;
import com.google.gerrit.acceptance.RestSession;
import com.google.gerrit.acceptance.TestAccount;
import com.google.gerrit.extensions.restapi.Url;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.inject.Inject;
import org.apache.http.HttpStatus;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
public class GetAccountIT extends AbstractDaemonTest {
@Inject
private AccountCreator accounts;
private TestAccount admin;
private RestSession session;
@Before
public void setUp() throws Exception {
admin = accounts.create("admin", "admin@example.com", "Administrator",
"Administrators");
session = new RestSession(admin);
}
@Test
public void getNonExistingAccount_NotFound() throws IOException {
assertEquals(HttpStatus.SC_NOT_FOUND, session.get("/accounts/non-existing")
.getStatusCode());
}
@Test
public void getAccount() throws IOException {
// by formatted string
testGetAccount("/accounts/"
+ Url.encode(admin.fullName + " <" + admin.email + ">"), admin);
// by email
testGetAccount("/accounts/" + admin.email, admin);
// by full name
testGetAccount("/accounts/" + admin.fullName, admin);
// by account ID
testGetAccount("/accounts/" + admin.id.get(), admin);
// by user name
testGetAccount("/accounts/" + admin.username, admin);
// by 'self'
testGetAccount("/accounts/self", admin);
}
private void testGetAccount(String url, TestAccount expectedAccount)
throws IOException {
RestResponse r = session.get(url);
assertEquals(HttpStatus.SC_OK, r.getStatusCode());
AccountInfo account =
(new Gson()).fromJson(r.getReader(),
new TypeToken<AccountInfo>() {}.getType());
assertAccountInfo(expectedAccount, account);
}
}

View File

@@ -14,6 +14,7 @@
package com.google.gerrit.acceptance.rest.group;
import static com.google.gerrit.acceptance.rest.account.AccountAssert.assertAccountInfo;
import static com.google.gerrit.acceptance.rest.group.GroupAssert.assertGroupInfo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -28,6 +29,7 @@ import com.google.gerrit.acceptance.AccountCreator;
import com.google.gerrit.acceptance.RestResponse;
import com.google.gerrit.acceptance.RestSession;
import com.google.gerrit.acceptance.TestAccount;
import com.google.gerrit.acceptance.rest.account.AccountInfo;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.reviewdb.client.AccountGroupIncludeByUuid;
@@ -188,12 +190,6 @@ public class AddRemoveGroupMembersIT extends AbstractDaemonTest {
session.put("/groups/" + name, in).consume();
}
private void assertAccountInfo(TestAccount a, AccountInfo ai) {
assertTrue(a.id.get() == ai._account_id);
assertEquals(a.fullName, ai.name);
assertEquals(a.email, ai.email);
}
private void assertMembers(String group, TestAccount... members)
throws OrmException {
AccountGroup g = groupCache.get(new AccountGroup.NameKey(group));

View File

@@ -24,6 +24,7 @@ import com.google.gerrit.acceptance.AccountCreator;
import com.google.gerrit.acceptance.RestResponse;
import com.google.gerrit.acceptance.RestSession;
import com.google.gerrit.acceptance.TestAccount;
import com.google.gerrit.acceptance.rest.account.AccountInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.inject.Inject;

View File

@@ -0,0 +1,24 @@
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.account;
import com.google.gerrit.extensions.restapi.RestReadView;
public class GetAccount implements RestReadView<AccountResource> {
@Override
public AccountInfo apply(AccountResource rsrc) {
return AccountInfo.parse(rsrc.getUser().getAccount(), true);
}
}

View File

@@ -29,6 +29,7 @@ public class Module extends RestApiModule {
DynamicMap.mapOf(binder(), ACCOUNT_KIND);
DynamicMap.mapOf(binder(), CAPABILITY_KIND);
get(ACCOUNT_KIND).to(GetAccount.class);
get(ACCOUNT_KIND, "avatar").to(GetAvatar.class);
child(ACCOUNT_KIND, "capabilities").to(Capabilities.class);
get(ACCOUNT_KIND, "groups").to(GetGroups.class);