Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UserApi Development Initialized #189

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/com/cdancy/bitbucket/rest/BitbucketApi.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.cdancy.bitbucket.rest.features.SystemApi;
import com.cdancy.bitbucket.rest.features.TagApi;
import com.cdancy.bitbucket.rest.features.TasksApi;
import com.cdancy.bitbucket.rest.features.UserApi;
import com.cdancy.bitbucket.rest.features.WebHookApi;


Expand Down Expand Up @@ -88,4 +89,8 @@ public interface BitbucketApi extends Closeable {

@Delegate
TasksApi tasksApi();

@Delegate
UserApi userApi();

}
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ public static User create(final List<Error> errors, final String name,
final String emailAddress, final int id,
final String displayName,final boolean active,
final String slug, final String type,
final String directoryName, final boolean deletable,
final long lastAuthenticationTimestamp, final boolean mutableDetails,
final boolean mutableGroups) {
@Nullable final String directoryName, @Nullable final boolean deletable,
@Nullable final long lastAuthenticationTimestamp, @Nullable final boolean mutableDetails,
@Nullable final boolean mutableGroups) {

return new AutoValue_User(BitbucketUtils.nullToEmpty(errors), name, emailAddress, id, displayName,
active, slug, type, directoryName, deletable, lastAuthenticationTimestamp, mutableDetails, mutableGroups);
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/com/cdancy/bitbucket/rest/features/UserApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.cdancy.bitbucket.rest.features;

import com.cdancy.bitbucket.rest.annotations.Documentation;
import com.cdancy.bitbucket.rest.domain.admin.UserPage;
import com.cdancy.bitbucket.rest.fallbacks.BitbucketFallbacks;
import com.cdancy.bitbucket.rest.filters.BitbucketAuthenticationFilter;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.rest.annotations.Fallback;
import org.jclouds.rest.annotations.RequestFilters;

import javax.inject.Named;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

@Produces(MediaType.APPLICATION_JSON)
@RequestFilters(BitbucketAuthenticationFilter.class)
@Path("/rest/api/{jclouds.api-version}/user")
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public interface UserApi {

@Named("user:get-user")
@Documentation({"https://docs.atlassian.com/bitbucket-server/rest/6.0.0/bitbucket-rest.html#idp362"})
@Consumes(MediaType.APPLICATION_JSON)
@Path("/groups/more-members")
@Fallback(BitbucketFallbacks.UserPageOnError.class)
@GET
UserPage users(@Nullable @QueryParam("filter") String filter,
@Nullable @QueryParam("group") String group,
@Nullable @QueryParam("permission") String globalRole,
@Nullable @QueryParam("permission.1") String projectRole,
@Nullable @QueryParam("permission.1.projectKey") String projProjectKey,
@Nullable @QueryParam("permission.1.projectId") String projProjectId,
@Nullable @QueryParam("permission.2") String repoRole,
@Nullable @QueryParam("permission.2.projectKey") String repoProjectKey,
@Nullable @QueryParam("permission.3.projectId") String repoPrrojectId,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we could have N number of these which is going to make this a bit unruly and also limit folks if we only specify 3. We'll probably have to create an optional Binder which will take in a list of of these "permission objects" which we will then have to inject the values as query-params. Take a look HERE and poke around the code base to see how we do this type of thing elsewhere. Let me know if you have any questions. Looks good so far.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry I misunderstood the documentation I thought permission was for global 1 was for project permissions and 2 for repo etc.. I didn't recognize they were the same thing but different filters.

I looked into what you told me, isn't that a way to bind some data to the payload? which is as much as I know should not be used with GET requests as their body is not processed? Please correct me if I misunderstood anything.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore the class name ... you can alter the request in general and append new query params/etc. Take a look at the below code where we do something like this:

https://github.com/cdancy/jenkins-rest/blob/master/src/main/java/com/cdancy/jenkins/rest/filters/ScrubNullFolderParam.java

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sorry I have to postpone it to the next Friday, as am having my midterm exams this week and I got a lot of studying to do.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ajammil of course and take your time.

@Nullable @QueryParam("start") Integer start,
@Nullable @QueryParam("limit") Integer limit);


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.cdancy.bitbucket.rest.features;

import com.cdancy.bitbucket.rest.BaseBitbucketApiLiveTest;
import com.cdancy.bitbucket.rest.TestUtilities;
import com.cdancy.bitbucket.rest.domain.admin.UserPage;
import com.cdancy.bitbucket.rest.domain.pullrequest.User;

import org.testng.annotations.Test;

import static org.assertj.core.api.Assertions.assertThat;

@Test(groups = "live", testName = "UserApiLiveTest", singleThreaded = true)
public class UserApiLiveTest extends BaseBitbucketApiLiveTest {

@Test
public void testListUsers() {
final User user = TestUtilities.getDefaultUser(this.bitbucketAuthentication, this.api);
final UserPage userPage = api().users(user.slug(), defaultBitbucketGroup, null, null,
null, null, null, null, null, null, null);//Further testing required
assertThat(userPage).isNotNull();
assertThat(userPage.size() > 0).isTrue();
}

public void testListUsersOnError() {
final UserPage userPage = api().users(TestUtilities.randomString(), TestUtilities.randomString(), null,
null, null, null, null, null, null, null, null);
assertThat(userPage).isNotNull();
assertThat(userPage.size() == 0).isTrue();
}

private UserApi api() {
return api.userApi();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.cdancy.bitbucket.rest.features;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Map;

import org.testng.annotations.Test;

import com.cdancy.bitbucket.rest.BaseBitbucketMockTest;
import com.cdancy.bitbucket.rest.BitbucketApi;
//import com.cdancy.bitbucket.rest.BitbucketApiMetadata;
ajammil marked this conversation as resolved.
Show resolved Hide resolved
import com.cdancy.bitbucket.rest.domain.admin.UserPage;
import com.google.common.collect.ImmutableMap;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;

@Test(groups = "unit", testName = "UserApiMockTest")
public class UserApiMockTest extends BaseBitbucketMockTest {

private final String localGroup = "test";
private final String limitKeyword = "limit";
private final String startKeyword = "start";
private final String restApiPath = "/rest/api/";
private final String getMethod = "GET";


public void testUserListUsers() throws Exception {


final MockWebServer server = mockWebServer();

server.enqueue(new MockResponse()
.setBody(payloadFromResource("/user-list-users.json"))
.setResponseCode(200));
try (final BitbucketApi baseApi = api(server.getUrl("/"))) {

final UserPage up = baseApi.userApi().users("jcitizen", "stash-users", "ADMIN", null,
null, null, null, null, null, 0, 2);
assertThat(up).isNotNull();
assertThat(up.errors()).isEmpty();
assertThat(up.size() == 1).isTrue();
assertThat(up.values().get(0).slug().equals("jcitizen")).isTrue();

final Map<String, ?> queryParams = ImmutableMap.of("filter", "jcitizen",
"group","stash-users" , "permission", "ADMIN","start",0,"limit",2);
//assertSent(server, getMethod, restApiPath + BitbucketApiMetadata.API_VERSION
// + "/users", queryParams);
ajammil marked this conversation as resolved.
Show resolved Hide resolved
} finally {
server.shutdown();
}
}

public void testUserListUsersOnError() throws Exception {
final MockWebServer server = mockWebServer();

server.enqueue(new MockResponse().setBody(payloadFromResource("/user-list-users-error.json")).setResponseCode(401));
final BitbucketApi baseApi = api(server.getUrl("/"));
try {
final UserPage up = baseApi.userApi().users("blah blah", "stash-users", "ADMIN", null,
null, null, null, null, null, 0, 2);
assertThat(up).isNotNull();
assertThat(up.errors()).isNotEmpty();

final Map<String, ?> queryParams = ImmutableMap.of("filter", "blah%20blah",
"group","stash-users" , "permission", "ADMIN","start",0,"limit",2);
//assertSent(server, getMethod, restApiPath + BitbucketApiMetadata.API_VERSION
// + "/users", queryParams);
} finally {
baseApi.close();
server.shutdown();
}
}




}
9 changes: 9 additions & 0 deletions src/test/resources/user-list-users-error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"errors": [
{
"context": null,
"message": "Authentication failed. Please check your credentials and try again.",
"exceptionName": "com.atlassian.bitbucket.auth.IncorrectPasswordAuthenticationException"
}
]
}
26 changes: 26 additions & 0 deletions src/test/resources/user-list-users.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

{
"size": 1,
"limit": 25,
"isLastPage": true,
"values": [
{
"name": "jcitizen",
"emailAddress": "[email protected]",
"id": 101,
"displayName": "Jane Citizen",
"active": true,
"slug": "jcitizen",
"type": "NORMAL",
"links": {
"self": [
{
"href": "https://bitbucket.acme.com/users/jcitizen"
}
]
}
}

],
"start": 0
}