1 package it.com.atlassian.rest.scope;
2
3 import com.atlassian.fugue.Either;
4 import com.atlassian.rest.jersey.client.WebResourceFactory;
5 import com.sun.jersey.api.client.UniformInterfaceException;
6 import com.sun.jersey.api.client.WebResource;
7 import org.junit.Test;
8
9 import javax.ws.rs.core.Cookie;
10 import java.util.Optional;
11
12 import static com.atlassian.rest.jersey.client.WebResourceFactory.REST_VERSION;
13 import static java.util.Optional.of;
14 import static javax.ws.rs.core.Response.Status.PRECONDITION_FAILED;
15 import static org.junit.Assert.assertEquals;
16
17 public class ScopeTest {
18
19 final Cookie dummyScope = new Cookie("atlassian.scope.dummy", "true");
20 final Cookie refappScope = new Cookie("atlassian.scope.refapp", "true");
21
22 @Test
23 public void testExplicitScope() {
24 assertEquals(Integer.valueOf(PRECONDITION_FAILED.getStatusCode()), doRequest("explicit-scope", Optional.empty()).left().get());
25
26 assertEquals(Integer.valueOf(PRECONDITION_FAILED.getStatusCode()), doRequest("explicit-scope", of(dummyScope)).left().get());
27
28 assertEquals("exp", doRequest("explicit-scope", of(refappScope)).right().get());
29 }
30
31 @Test
32 public void testImplicitScope() {
33 assertEquals(Integer.valueOf(PRECONDITION_FAILED.getStatusCode()), doRequest("implicit-scope", Optional.empty()).left().get());
34
35 assertEquals(Integer.valueOf(PRECONDITION_FAILED.getStatusCode()), doRequest("implicit-scope", of(dummyScope)).left().get());
36
37 assertEquals("imp", doRequest("implicit-scope", of(refappScope)).right().get());
38 }
39
40 @Test
41 public void testNoScoped() {
42 assertEquals("non", doRequest("non-scoped", Optional.empty()).right().get());
43
44 assertEquals("non", doRequest("non-scoped", of(dummyScope)).right().get());
45
46 assertEquals("non", doRequest("non-scoped", of(refappScope)).right().get());
47 }
48
49 private Either<Integer, String> doRequest(String path, Optional<Cookie> scope) {
50 try {
51 final WebResource ws = WebResourceFactory.anonymous(
52 WebResourceFactory.getUriBuilder()
53 .path("rest").path(path).path(REST_VERSION).path("get").build());
54 return scope.isPresent()
55 ? Either.right(ws.cookie(scope.get()).get(String.class))
56 : Either.right(ws.get(String.class));
57 } catch (UniformInterfaceException e) {
58 return Either.left(e.getResponse().getStatusInfo().getStatusCode());
59 }
60 }
61 }