From e6b8e729f5ea1adb229a1b6d30a1875289471733 Mon Sep 17 00:00:00 2001 From: boxdave Date: Sun, 22 Mar 2020 20:52:55 -0500 Subject: [PATCH 01/10] adds shared link downscoping and coverage --- .../java/com/box/sdk/BoxAPIConnection.java | 23 +++++---- .../com/box/sdk/BoxAPIConnectionTest.java | 49 +++++++++++++------ 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/box/sdk/BoxAPIConnection.java b/src/main/java/com/box/sdk/BoxAPIConnection.java index 382592e34..336df6127 100644 --- a/src/main/java/com/box/sdk/BoxAPIConnection.java +++ b/src/main/java/com/box/sdk/BoxAPIConnection.java @@ -677,7 +677,7 @@ public void setRequestInterceptor(RequestInterceptor interceptor) { * @param resource the resource for which the new token has to be obtained * @return scopedToken which has access token and other details */ - public ScopedToken getLowerScopedToken(List scopes, String resource) { + public ScopedToken getLowerScopedToken(List scopes, String resource, String sharedLink) { assert (scopes != null); assert (scopes.size() > 0); URL url = null; @@ -698,18 +698,21 @@ public ScopedToken getLowerScopedToken(List scopes, String resource) { String urlParameters = null; - if (resource != null) { - //this.getAccessToken() ensures we have a valid access token + if (resource == null && sharedLink == null) { urlParameters = String.format("grant_type=urn:ietf:params:oauth:grant-type:token-exchange" - + "&subject_token_type=urn:ietf:params:oauth:token-type:access_token&subject_token=%s" - + "&scope=%s&resource=%s", - this.getAccessToken(), spaceSeparatedScopes, resource); + + "&subject_token_type=urn:ietf:params:oauth:token-type:access_token&subject_token=%s" + + "&scope=%s", + this.getAccessToken(), spaceSeparatedScopes); + } else if (resource != null) { + urlParameters = String.format("grant_type=urn:ietf:params:oauth:grant-type:token-exchange" + + "&subject_token_type=urn:ietf:params:oauth:token-type:access_token&subject_token=%s" + + "&scope=%s&resource=%s", + this.getAccessToken(), spaceSeparatedScopes, resource); } else { - //this.getAccessToken() ensures we have a valid access token urlParameters = String.format("grant_type=urn:ietf:params:oauth:grant-type:token-exchange" - + "&subject_token_type=urn:ietf:params:oauth:token-type:access_token&subject_token=%s" - + "&scope=%s", - this.getAccessToken(), spaceSeparatedScopes); + + "&subject_token_type=urn:ietf:params:oauth:token-type:access_token&subject_token=%s" + + "&scope=%s&box_shared_link=%s", + this.getAccessToken(), spaceSeparatedScopes, sharedLink); } BoxAPIRequest request = new BoxAPIRequest(this, url, "POST"); diff --git a/src/test/java/com/box/sdk/BoxAPIConnectionTest.java b/src/test/java/com/box/sdk/BoxAPIConnectionTest.java index 0fcdfffee..aad85f103 100644 --- a/src/test/java/com/box/sdk/BoxAPIConnectionTest.java +++ b/src/test/java/com/box/sdk/BoxAPIConnectionTest.java @@ -569,11 +569,12 @@ public void getLowerScopedTokenRefreshesTheTokenIfNeededbyCallingGetAccessToken( List scopes = new ArrayList(); scopes.add("DummyScope"); String resource = ""; + String sharedLink = null; when(api.getTokenURL()).thenReturn("https://api.box.com/oauth2/token"); - when(api.getLowerScopedToken(scopes, resource)).thenCallRealMethod(); + when(api.getLowerScopedToken(scopes, resource, sharedLink)).thenCallRealMethod(); try { - api.getLowerScopedToken(scopes, resource); + api.getLowerScopedToken(scopes, resource, sharedLink); } catch (RuntimeException e) { //Ignore it } @@ -581,36 +582,52 @@ public void getLowerScopedTokenRefreshesTheTokenIfNeededbyCallingGetAccessToken( } @Test - @Category(UnitTest.class) - public void getLowerScopedTokenWithNullResource() { - BoxAPIConnection api = mock(BoxAPIConnection.class); + @Category(IntegrationTest.class) + public void getLowerScopedToken() { + final String originalAccessToken = TestConfig.getAccessToken(); + BoxAPIConnection api = new BoxAPIConnection(originalAccessToken); List scopes = new ArrayList(); - scopes.add("DummyScope"); + scopes.add("item_preview"); + scopes.add("item_content_upload"); String resource = null; + String sharedLink = null; - when(api.getTokenURL()).thenReturn("https://api.box.com/oauth2/token"); - when(api.getLowerScopedToken(scopes, resource)).thenCallRealMethod(); - try { - api.getLowerScopedToken(scopes, resource); - } catch (RuntimeException e) { - //Ignore it - } - verify(api).getAccessToken(); + ScopedToken token = api.getLowerScopedToken(scopes, resource, sharedLink); + assertThat(token, notNullValue()); + assertThat(token.getAccessToken(), notNullValue()); } @Test @Category(IntegrationTest.class) - public void getLowerScopedTokenWorks() { + public void getLowerScopedTokenForResource() { final String originalAccessToken = TestConfig.getAccessToken(); BoxAPIConnection api = new BoxAPIConnection(originalAccessToken); + List scopes = new ArrayList(); + scopes.add("item_preview"); + scopes.add("item_content_upload"); String resource = "https://api.box.com/2.0/files/135906984991"; + String sharedLink = null; + + ScopedToken token = api.getLowerScopedToken(scopes, resource, sharedLink); + assertThat(token, notNullValue()); + assertThat(token.getAccessToken(), notNullValue()); + } + + @Test + @Category(IntegrationTest.class) + public void getLowerScopedTokenForSharedLink() { + final String originalAccessToken = TestConfig.getAccessToken(); + BoxAPIConnection api = new BoxAPIConnection(originalAccessToken); + List scopes = new ArrayList(); scopes.add("item_preview"); scopes.add("item_content_upload"); + String resource = null; + String sharedLink = null; - ScopedToken token = api.getLowerScopedToken(scopes, resource); + ScopedToken token = api.getLowerScopedToken(scopes, resource, sharedLink); assertThat(token, notNullValue()); assertThat(token.getAccessToken(), notNullValue()); } From 54bdc1629bba3815a79af17970ad93bb8e5a2bf5 Mon Sep 17 00:00:00 2001 From: boxdave Date: Sun, 29 Mar 2020 16:21:08 -0500 Subject: [PATCH 02/10] adds helper for api vs. shared link url. tests failing --- .../java/com/box/sdk/BoxAPIConnection.java | 69 ++++++++++++++----- .../com/box/sdk/BoxAPIConnectionTest.java | 38 ++++++---- 2 files changed, 75 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/box/sdk/BoxAPIConnection.java b/src/main/java/com/box/sdk/BoxAPIConnection.java index 336df6127..cd6b5bb12 100644 --- a/src/main/java/com/box/sdk/BoxAPIConnection.java +++ b/src/main/java/com/box/sdk/BoxAPIConnection.java @@ -10,6 +10,7 @@ import java.util.Map; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.regex.Pattern; import com.eclipsesource.json.JsonObject; @@ -671,13 +672,42 @@ public void setRequestInterceptor(RequestInterceptor interceptor) { this.interceptor = interceptor; } + private String determineResourceLinkType(String resourceLink) { + + String resourceType = null; + + try { + URL validUrl = new URL(resourceLink); + String validURLStr = validUrl.toString(); + String APIEndpointPattern = "https://api.box.com/2.0/files/\\d+"; + boolean isAPIEndpointMatch = Pattern.matches(APIEndpointPattern, validURLStr); + if (isAPIEndpointMatch) { + System.out.println(validURLStr + " is valid API endpoint"); + resourceType = "api endpoint"; + } else { + String sharedLinkPattern = "(https://.*.box.com/s/.*|https://.*.app.box.com/notes/\\d+\\?s=.*)"; + boolean isSharedLinkMatch = Pattern.matches(sharedLinkPattern, validURLStr); + if (isSharedLinkMatch) { + System.out.println(validURLStr + " is valid shared link"); + resourceType = "shared link"; + }; + }; + + } catch (MalformedURLException e) { + System.out.println(resourceLink + " is not a valid URL"); + }; + + return resourceType; + } + /** * Get a lower-scoped token restricted to a resource for the list of scopes that are passed. * @param scopes the list of scopes to which the new token should be restricted for * @param resource the resource for which the new token has to be obtained * @return scopedToken which has access token and other details + * @throws IllegalArgumentException if resource is not a valid Box API endpoint or shared link */ - public ScopedToken getLowerScopedToken(List scopes, String resource, String sharedLink) { + public ScopedToken getLowerScopedToken(List scopes, String resource) { assert (scopes != null); assert (scopes.size() > 0); URL url = null; @@ -696,28 +726,29 @@ public ScopedToken getLowerScopedToken(List scopes, String resource, Str } } - String urlParameters = null; - - if (resource == null && sharedLink == null) { - urlParameters = String.format("grant_type=urn:ietf:params:oauth:grant-type:token-exchange" - + "&subject_token_type=urn:ietf:params:oauth:token-type:access_token&subject_token=%s" - + "&scope=%s", - this.getAccessToken(), spaceSeparatedScopes); - } else if (resource != null) { - urlParameters = String.format("grant_type=urn:ietf:params:oauth:grant-type:token-exchange" - + "&subject_token_type=urn:ietf:params:oauth:token-type:access_token&subject_token=%s" - + "&scope=%s&resource=%s", - this.getAccessToken(), spaceSeparatedScopes, resource); + String JSONBody = String.format( + "{grant_type=urn:ietf:params:oauth:grant-type:token-exchange," + + "subject_token_type=urn:ietf:params:oauth:token-type:access_token," + + "subject_token=%s,scope=%s", this.getAccessToken(), spaceSeparatedScopes); + + if (resource == null) { + JSONBody = JSONBody + "}"; } else { - urlParameters = String.format("grant_type=urn:ietf:params:oauth:grant-type:token-exchange" - + "&subject_token_type=urn:ietf:params:oauth:token-type:access_token&subject_token=%s" - + "&scope=%s&box_shared_link=%s", - this.getAccessToken(), spaceSeparatedScopes, sharedLink); - } + String resourceType = this.determineResourceLinkType(resource); + + if (resourceType.equals("api endpoint")) { + JSONBody = String.format(JSONBody + "resource=%s}", resource); + } else if (resourceType.equals("shared link")) { + JSONBody = String.format(JSONBody + "box_shared_link=%s}", resource); + } else { + String argExceptionMessage = resource + " is not a valid Box API endpoint or shared link"; + throw new BoxAPIException(argExceptionMessage); + }; + }; BoxAPIRequest request = new BoxAPIRequest(this, url, "POST"); request.shouldAuthenticate(false); - request.setBody(urlParameters); + request.setBody(JSONBody); String json; try { diff --git a/src/test/java/com/box/sdk/BoxAPIConnectionTest.java b/src/test/java/com/box/sdk/BoxAPIConnectionTest.java index aad85f103..d8bb8017d 100644 --- a/src/test/java/com/box/sdk/BoxAPIConnectionTest.java +++ b/src/test/java/com/box/sdk/BoxAPIConnectionTest.java @@ -569,12 +569,11 @@ public void getLowerScopedTokenRefreshesTheTokenIfNeededbyCallingGetAccessToken( List scopes = new ArrayList(); scopes.add("DummyScope"); String resource = ""; - String sharedLink = null; when(api.getTokenURL()).thenReturn("https://api.box.com/oauth2/token"); - when(api.getLowerScopedToken(scopes, resource, sharedLink)).thenCallRealMethod(); + when(api.getLowerScopedToken(scopes, resource)).thenCallRealMethod(); try { - api.getLowerScopedToken(scopes, resource, sharedLink); + api.getLowerScopedToken(scopes, resource); } catch (RuntimeException e) { //Ignore it } @@ -584,23 +583,22 @@ public void getLowerScopedTokenRefreshesTheTokenIfNeededbyCallingGetAccessToken( @Test @Category(IntegrationTest.class) public void getLowerScopedToken() { - final String originalAccessToken = TestConfig.getAccessToken(); + final String originalAccessToken = "a"; BoxAPIConnection api = new BoxAPIConnection(originalAccessToken); List scopes = new ArrayList(); scopes.add("item_preview"); scopes.add("item_content_upload"); String resource = null; - String sharedLink = null; - ScopedToken token = api.getLowerScopedToken(scopes, resource, sharedLink); + ScopedToken token = api.getLowerScopedToken(scopes, resource); assertThat(token, notNullValue()); assertThat(token.getAccessToken(), notNullValue()); } @Test @Category(IntegrationTest.class) - public void getLowerScopedTokenForResource() { + public void getLowerScopedTokenForAPIEndpointResource() { final String originalAccessToken = TestConfig.getAccessToken(); BoxAPIConnection api = new BoxAPIConnection(originalAccessToken); @@ -608,26 +606,40 @@ public void getLowerScopedTokenForResource() { scopes.add("item_preview"); scopes.add("item_content_upload"); String resource = "https://api.box.com/2.0/files/135906984991"; - String sharedLink = null; - ScopedToken token = api.getLowerScopedToken(scopes, resource, sharedLink); + ScopedToken token = api.getLowerScopedToken(scopes, resource); assertThat(token, notNullValue()); assertThat(token.getAccessToken(), notNullValue()); } @Test @Category(IntegrationTest.class) - public void getLowerScopedTokenForSharedLink() { + public void getLowerScopedTokenForSharedLinkResource() { final String originalAccessToken = TestConfig.getAccessToken(); BoxAPIConnection api = new BoxAPIConnection(originalAccessToken); List scopes = new ArrayList(); scopes.add("item_preview"); scopes.add("item_content_upload"); - String resource = null; - String sharedLink = null; + String resource = "https://rungaia.box.com/s/68c1cewvxas7orqmobakg17o61bfrkcu"; + + ScopedToken token = api.getLowerScopedToken(scopes, resource); + assertThat(token, notNullValue()); + assertThat(token.getAccessToken(), notNullValue()); + } + + @Test + @Category(IntegrationTest.class) + public void getLowerScopedTokenForBoxNoteSharedLinkResource() { + final String originalAccessToken = TestConfig.getAccessToken(); + BoxAPIConnection api = new BoxAPIConnection(originalAccessToken); + + List scopes = new ArrayList(); + scopes.add("item_preview"); + scopes.add("item_content_upload"); + String resource = "https://rungaia.app.box.com/notes/643001418459?s=68c1cewvxas7orqmobakg17o61bfrkcu"; - ScopedToken token = api.getLowerScopedToken(scopes, resource, sharedLink); + ScopedToken token = api.getLowerScopedToken(scopes, resource); assertThat(token, notNullValue()); assertThat(token.getAccessToken(), notNullValue()); } From 1429f7f624a20b438d2177edde7a0a817f94d942 Mon Sep 17 00:00:00 2001 From: boxdave Date: Fri, 3 Apr 2020 11:18:21 -0500 Subject: [PATCH 03/10] git ignore config.json --- .gitignore | 1 + src/main/java/com/box/sdk/BoxAPIConnection.java | 8 +++++--- src/test/config/{config.json => config.json.template} | 0 3 files changed, 6 insertions(+), 3 deletions(-) rename src/test/config/{config.json => config.json.template} (100%) diff --git a/.gitignore b/.gitignore index d68f543ec..b0ac4e822 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ gradle.properties build/ .DS_Store src/test/config/config.properties +src/test/config/config.json /target/ .settings/ .idea/ diff --git a/src/main/java/com/box/sdk/BoxAPIConnection.java b/src/main/java/com/box/sdk/BoxAPIConnection.java index cd6b5bb12..e24ff8a39 100644 --- a/src/main/java/com/box/sdk/BoxAPIConnection.java +++ b/src/main/java/com/box/sdk/BoxAPIConnection.java @@ -685,7 +685,7 @@ private String determineResourceLinkType(String resourceLink) { System.out.println(validURLStr + " is valid API endpoint"); resourceType = "api endpoint"; } else { - String sharedLinkPattern = "(https://.*.box.com/s/.*|https://.*.app.box.com/notes/\\d+\\?s=.*)"; + String sharedLinkPattern = "(.*box.com/s/.*|.*box.com.*s=.*)"; boolean isSharedLinkMatch = Pattern.matches(sharedLinkPattern, validURLStr); if (isSharedLinkMatch) { System.out.println(validURLStr + " is valid shared link"); @@ -705,7 +705,7 @@ private String determineResourceLinkType(String resourceLink) { * @param scopes the list of scopes to which the new token should be restricted for * @param resource the resource for which the new token has to be obtained * @return scopedToken which has access token and other details - * @throws IllegalArgumentException if resource is not a valid Box API endpoint or shared link + * @throws BoxAPIException if resource is not a valid Box API endpoint or shared link */ public ScopedToken getLowerScopedToken(List scopes, String resource) { assert (scopes != null); @@ -742,7 +742,9 @@ public ScopedToken getLowerScopedToken(List scopes, String resource) { JSONBody = String.format(JSONBody + "box_shared_link=%s}", resource); } else { String argExceptionMessage = resource + " is not a valid Box API endpoint or shared link"; - throw new BoxAPIException(argExceptionMessage); + BoxAPIException e = new BoxAPIException(argExceptionMessage); + this.notifyError(e); + throw e; }; }; diff --git a/src/test/config/config.json b/src/test/config/config.json.template similarity index 100% rename from src/test/config/config.json rename to src/test/config/config.json.template From 21540ee763e01751304997fa0c9597b026e349e6 Mon Sep 17 00:00:00 2001 From: boxdave Date: Fri, 3 Apr 2020 11:56:56 -0500 Subject: [PATCH 04/10] unit tests passing. integration failing --- .../java/com/box/sdk/BoxAPIConnection.java | 69 ++++++++++++++++- .../com/box/sdk/BoxAPIConnectionTest.java | 75 +++++++++++-------- 2 files changed, 110 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/box/sdk/BoxAPIConnection.java b/src/main/java/com/box/sdk/BoxAPIConnection.java index e24ff8a39..dd2c24766 100644 --- a/src/main/java/com/box/sdk/BoxAPIConnection.java +++ b/src/main/java/com/box/sdk/BoxAPIConnection.java @@ -700,6 +700,67 @@ private String determineResourceLinkType(String resourceLink) { return resourceType; } +// /** +// * Get a lower-scoped token restricted to a resource for the list of scopes that are passed. +// * @param scopes the list of scopes to which the new token should be restricted for +// * @param resource the resource for which the new token has to be obtained +// * @return scopedToken which has access token and other details +// */ +// public ScopedToken getLowerScopedToken(List scopes, String resource) { +// assert (scopes != null); +// assert (scopes.size() > 0); +// URL url = null; +// try { +// url = new URL(this.getTokenURL()); +// } catch (MalformedURLException e) { +// assert false : "An invalid refresh URL indicates a bug in the SDK."; +// throw new RuntimeException("An invalid refresh URL indicates a bug in the SDK.", e); +// } +// +// StringBuilder spaceSeparatedScopes = new StringBuilder(); +// for (int i = 0; i < scopes.size(); i++) { +// spaceSeparatedScopes.append(scopes.get(i)); +// if (i < scopes.size() - 1) { +// spaceSeparatedScopes.append(" "); +// } +// } +// +// String urlParameters = null; +// +// if (resource != null) { +// //this.getAccessToken() ensures we have a valid access token +// urlParameters = String.format("grant_type=urn:ietf:params:oauth:grant-type:token-exchange" +// + "&subject_token_type=urn:ietf:params:oauth:token-type:access_token&subject_token=%s" +// + "&scope=%s&resource=%s", +// this.getAccessToken(), spaceSeparatedScopes, resource); +// } else { +// //this.getAccessToken() ensures we have a valid access token +// urlParameters = String.format("grant_type=urn:ietf:params:oauth:grant-type:token-exchange" +// + "&subject_token_type=urn:ietf:params:oauth:token-type:access_token&subject_token=%s" +// + "&scope=%s", +// this.getAccessToken(), spaceSeparatedScopes); +// } +// +// BoxAPIRequest request = new BoxAPIRequest(this, url, "POST"); +// request.shouldAuthenticate(false); +// request.setBody(urlParameters); +// +// String json; +// try { +// BoxJSONResponse response = (BoxJSONResponse) request.send(); +// json = response.getJSON(); +// } catch (BoxAPIException e) { +// this.notifyError(e); +// throw e; +// } +// +// JsonObject jsonObject = JsonObject.readFrom(json); +// ScopedToken token = new ScopedToken(jsonObject); +// token.setObtainedAt(System.currentTimeMillis()); +// token.setExpiresIn(jsonObject.get("expires_in").asLong() * 1000); +// return token; +// } + /** * Get a lower-scoped token restricted to a resource for the list of scopes that are passed. * @param scopes the list of scopes to which the new token should be restricted for @@ -737,9 +798,9 @@ public ScopedToken getLowerScopedToken(List scopes, String resource) { String resourceType = this.determineResourceLinkType(resource); if (resourceType.equals("api endpoint")) { - JSONBody = String.format(JSONBody + "resource=%s}", resource); + JSONBody = String.format(JSONBody + ",resource=%s}", resource); } else if (resourceType.equals("shared link")) { - JSONBody = String.format(JSONBody + "box_shared_link=%s}", resource); + JSONBody = String.format(JSONBody + ",box_shared_link=%s}", resource); } else { String argExceptionMessage = resource + " is not a valid Box API endpoint or shared link"; BoxAPIException e = new BoxAPIException(argExceptionMessage); @@ -748,10 +809,14 @@ public ScopedToken getLowerScopedToken(List scopes, String resource) { }; }; + System.out.println("Constructed JSONBody " + JSONBody); + BoxAPIRequest request = new BoxAPIRequest(this, url, "POST"); request.shouldAuthenticate(false); request.setBody(JSONBody); + + String json; try { BoxJSONResponse response = (BoxJSONResponse) request.send(); diff --git a/src/test/java/com/box/sdk/BoxAPIConnectionTest.java b/src/test/java/com/box/sdk/BoxAPIConnectionTest.java index d8bb8017d..56739f7f6 100644 --- a/src/test/java/com/box/sdk/BoxAPIConnectionTest.java +++ b/src/test/java/com/box/sdk/BoxAPIConnectionTest.java @@ -563,12 +563,12 @@ public void appUsersManuallyPaginatesCorrectly() throws IOException { @Test @Category(UnitTest.class) - public void getLowerScopedTokenRefreshesTheTokenIfNeededbyCallingGetAccessToken() { + public void getLowerScopedTokenWithNullResource() { BoxAPIConnection api = mock(BoxAPIConnection.class); List scopes = new ArrayList(); - scopes.add("DummyScope"); - String resource = ""; + scopes.add("item_preview"); + String resource = null; when(api.getTokenURL()).thenReturn("https://api.box.com/oauth2/token"); when(api.getLowerScopedToken(scopes, resource)).thenCallRealMethod(); @@ -581,63 +581,74 @@ public void getLowerScopedTokenRefreshesTheTokenIfNeededbyCallingGetAccessToken( } @Test - @Category(IntegrationTest.class) - public void getLowerScopedToken() { - final String originalAccessToken = "a"; - BoxAPIConnection api = new BoxAPIConnection(originalAccessToken); + @Category(UnitTest.class) + public void getLowerScopedTokenForAPIEndpointResource() { + BoxAPIConnection api = mock(BoxAPIConnection.class); List scopes = new ArrayList(); scopes.add("item_preview"); - scopes.add("item_content_upload"); - String resource = null; + String APIEndpointResource = "https://api.box.com/2.0/files/135906984991"; - ScopedToken token = api.getLowerScopedToken(scopes, resource); - assertThat(token, notNullValue()); - assertThat(token.getAccessToken(), notNullValue()); + when(api.getTokenURL()).thenReturn("https://api.box.com/oauth2/token"); + when(api.getLowerScopedToken(scopes, APIEndpointResource)).thenCallRealMethod(); + try { + api.getLowerScopedToken(scopes, APIEndpointResource); + } catch (RuntimeException e) { + //Ignore it + } + verify(api).getAccessToken(); } @Test - @Category(IntegrationTest.class) - public void getLowerScopedTokenForAPIEndpointResource() { - final String originalAccessToken = TestConfig.getAccessToken(); - BoxAPIConnection api = new BoxAPIConnection(originalAccessToken); + @Category(UnitTest.class) + public void getLowerScopedTokenForSharedLinkResource() { + BoxAPIConnection api = mock(BoxAPIConnection.class); List scopes = new ArrayList(); scopes.add("item_preview"); - scopes.add("item_content_upload"); - String resource = "https://api.box.com/2.0/files/135906984991"; - ScopedToken token = api.getLowerScopedToken(scopes, resource); - assertThat(token, notNullValue()); - assertThat(token.getAccessToken(), notNullValue()); + String sharedLinkResource = "https://rungaia.box.com/s/68c1cewvxas7orqmobakg17o61bfrkcu"; + + when(api.getTokenURL()).thenReturn("https://api.box.com/oauth2/token"); + when(api.getLowerScopedToken(scopes, sharedLinkResource)).thenCallRealMethod(); + try { + api.getLowerScopedToken(scopes, sharedLinkResource); + } catch (RuntimeException e) { + //Ignore it + } + verify(api).getAccessToken(); } @Test - @Category(IntegrationTest.class) - public void getLowerScopedTokenForSharedLinkResource() { - final String originalAccessToken = TestConfig.getAccessToken(); - BoxAPIConnection api = new BoxAPIConnection(originalAccessToken); + @Category(UnitTest.class) + public void getLowerScopedTokenForNoteSharedLinkResource() { + BoxAPIConnection api = mock(BoxAPIConnection.class); List scopes = new ArrayList(); scopes.add("item_preview"); - scopes.add("item_content_upload"); - String resource = "https://rungaia.box.com/s/68c1cewvxas7orqmobakg17o61bfrkcu"; + String noteSharedLinkResource = "https://rungaia.app.box.com/notes/643001418459?s=68c1cewvxas7orqmobakg17o61bfrkcu\n"; - ScopedToken token = api.getLowerScopedToken(scopes, resource); - assertThat(token, notNullValue()); - assertThat(token.getAccessToken(), notNullValue()); + when(api.getTokenURL()).thenReturn("https://api.box.com/oauth2/token"); + when(api.getLowerScopedToken(scopes, noteSharedLinkResource)).thenCallRealMethod(); + try { + api.getLowerScopedToken(scopes, noteSharedLinkResource); + } catch (RuntimeException e) { + //Ignore it + } + verify(api).getAccessToken(); } @Test @Category(IntegrationTest.class) - public void getLowerScopedTokenForBoxNoteSharedLinkResource() { + public void getLowerScopedTokenWorks() { final String originalAccessToken = TestConfig.getAccessToken(); + System.out.println(originalAccessToken); BoxAPIConnection api = new BoxAPIConnection(originalAccessToken); + String resource = "https://api.box.com/2.0/files/135906984991"; List scopes = new ArrayList(); scopes.add("item_preview"); scopes.add("item_content_upload"); - String resource = "https://rungaia.app.box.com/notes/643001418459?s=68c1cewvxas7orqmobakg17o61bfrkcu"; ScopedToken token = api.getLowerScopedToken(scopes, resource); assertThat(token, notNullValue()); From d680ac093ca8341bced7c01771817b0242ecac2b Mon Sep 17 00:00:00 2001 From: boxdave Date: Fri, 3 Apr 2020 12:39:18 -0500 Subject: [PATCH 05/10] integration test BoxAPIConnectionTest.getLowerScopedTokenWorks failing --- .../com/box/sdk/BoxAPIConnectionTest.java | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/test/java/com/box/sdk/BoxAPIConnectionTest.java b/src/test/java/com/box/sdk/BoxAPIConnectionTest.java index 56739f7f6..d097ffadf 100644 --- a/src/test/java/com/box/sdk/BoxAPIConnectionTest.java +++ b/src/test/java/com/box/sdk/BoxAPIConnectionTest.java @@ -12,10 +12,7 @@ import java.util.StringTokenizer; import static org.hamcrest.Matchers.*; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.*; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -639,20 +636,25 @@ public void getLowerScopedTokenForNoteSharedLinkResource() { } @Test - @Category(IntegrationTest.class) - public void getLowerScopedTokenWorks() { - final String originalAccessToken = TestConfig.getAccessToken(); - System.out.println(originalAccessToken); - BoxAPIConnection api = new BoxAPIConnection(originalAccessToken); + @Category(IntegrationTestJWT.class) + public void getLowerScopedTokenWorks() throws IOException { + Reader reader = new FileReader("src/test/config/config.json"); + BoxConfig boxConfig = BoxConfig.readFrom(reader); + IAccessTokenCache accessTokenCache = new InMemoryLRUAccessTokenCache(100); + + BoxDeveloperEditionAPIConnection api = + BoxDeveloperEditionAPIConnection.getAppEnterpriseConnection(boxConfig, accessTokenCache); - String resource = "https://api.box.com/2.0/files/135906984991"; + String originalToken = api.getAccessToken(); + + String resource = null; List scopes = new ArrayList(); scopes.add("item_preview"); scopes.add("item_content_upload"); - ScopedToken token = api.getLowerScopedToken(scopes, resource); - assertThat(token, notNullValue()); - assertThat(token.getAccessToken(), notNullValue()); + ScopedToken newToken = api.getLowerScopedToken(scopes, resource); + assertThat(newToken, notNullValue()); + assertNotEquals(newToken.getAccessToken(), originalToken); } @Test From b93682e870ca87c9799c47a893184f656da5f5af Mon Sep 17 00:00:00 2001 From: boxdave Date: Tue, 7 Apr 2020 12:38:57 -0500 Subject: [PATCH 06/10] adds method for building scopes list. tests passing --- .../java/com/box/sdk/BoxAPIConnection.java | 110 +++++------------- .../com/box/sdk/BoxAPIConnectionTest.java | 2 +- 2 files changed, 27 insertions(+), 85 deletions(-) diff --git a/src/main/java/com/box/sdk/BoxAPIConnection.java b/src/main/java/com/box/sdk/BoxAPIConnection.java index dd2c24766..aab62865a 100644 --- a/src/main/java/com/box/sdk/BoxAPIConnection.java +++ b/src/main/java/com/box/sdk/BoxAPIConnection.java @@ -700,66 +700,17 @@ private String determineResourceLinkType(String resourceLink) { return resourceType; } -// /** -// * Get a lower-scoped token restricted to a resource for the list of scopes that are passed. -// * @param scopes the list of scopes to which the new token should be restricted for -// * @param resource the resource for which the new token has to be obtained -// * @return scopedToken which has access token and other details -// */ -// public ScopedToken getLowerScopedToken(List scopes, String resource) { -// assert (scopes != null); -// assert (scopes.size() > 0); -// URL url = null; -// try { -// url = new URL(this.getTokenURL()); -// } catch (MalformedURLException e) { -// assert false : "An invalid refresh URL indicates a bug in the SDK."; -// throw new RuntimeException("An invalid refresh URL indicates a bug in the SDK.", e); -// } -// -// StringBuilder spaceSeparatedScopes = new StringBuilder(); -// for (int i = 0; i < scopes.size(); i++) { -// spaceSeparatedScopes.append(scopes.get(i)); -// if (i < scopes.size() - 1) { -// spaceSeparatedScopes.append(" "); -// } -// } -// -// String urlParameters = null; -// -// if (resource != null) { -// //this.getAccessToken() ensures we have a valid access token -// urlParameters = String.format("grant_type=urn:ietf:params:oauth:grant-type:token-exchange" -// + "&subject_token_type=urn:ietf:params:oauth:token-type:access_token&subject_token=%s" -// + "&scope=%s&resource=%s", -// this.getAccessToken(), spaceSeparatedScopes, resource); -// } else { -// //this.getAccessToken() ensures we have a valid access token -// urlParameters = String.format("grant_type=urn:ietf:params:oauth:grant-type:token-exchange" -// + "&subject_token_type=urn:ietf:params:oauth:token-type:access_token&subject_token=%s" -// + "&scope=%s", -// this.getAccessToken(), spaceSeparatedScopes); -// } -// -// BoxAPIRequest request = new BoxAPIRequest(this, url, "POST"); -// request.shouldAuthenticate(false); -// request.setBody(urlParameters); -// -// String json; -// try { -// BoxJSONResponse response = (BoxJSONResponse) request.send(); -// json = response.getJSON(); -// } catch (BoxAPIException e) { -// this.notifyError(e); -// throw e; -// } -// -// JsonObject jsonObject = JsonObject.readFrom(json); -// ScopedToken token = new ScopedToken(jsonObject); -// token.setObtainedAt(System.currentTimeMillis()); -// token.setExpiresIn(jsonObject.get("expires_in").asLong() * 1000); -// return token; -// } + private StringBuilder buildScopesForTokenDownscoping(List scopes) { + StringBuilder spaceSeparatedScopes = new StringBuilder(); + for (int i = 0; i < scopes.size(); i++) { + spaceSeparatedScopes.append(scopes.get(i)); + if (i < scopes.size() - 1) { + spaceSeparatedScopes.append(" "); + } + } + + return spaceSeparatedScopes; + } /** * Get a lower-scoped token restricted to a resource for the list of scopes that are passed. @@ -776,31 +727,24 @@ public ScopedToken getLowerScopedToken(List scopes, String resource) { url = new URL(this.getTokenURL()); } catch (MalformedURLException e) { assert false : "An invalid refresh URL indicates a bug in the SDK."; - throw new RuntimeException("An invalid refresh URL indicates a bug in the SDK.", e); + throw new BoxAPIException("An invalid refresh URL indicates a bug in the SDK.", e); } - StringBuilder spaceSeparatedScopes = new StringBuilder(); - for (int i = 0; i < scopes.size(); i++) { - spaceSeparatedScopes.append(scopes.get(i)); - if (i < scopes.size() - 1) { - spaceSeparatedScopes.append(" "); - } - } + StringBuilder spaceSeparatedScopes = this.buildScopesForTokenDownscoping(scopes); - String JSONBody = String.format( - "{grant_type=urn:ietf:params:oauth:grant-type:token-exchange," - + "subject_token_type=urn:ietf:params:oauth:token-type:access_token," - + "subject_token=%s,scope=%s", this.getAccessToken(), spaceSeparatedScopes); + String urlParameters = String.format("grant_type=urn:ietf:params:oauth:grant-type:token-exchange" + + "&subject_token_type=urn:ietf:params:oauth:token-type:access_token&subject_token=%s" + + "&scope=%s", + this.getAccessToken(), spaceSeparatedScopes); + + if (resource != null) { - if (resource == null) { - JSONBody = JSONBody + "}"; - } else { String resourceType = this.determineResourceLinkType(resource); if (resourceType.equals("api endpoint")) { - JSONBody = String.format(JSONBody + ",resource=%s}", resource); + urlParameters = String.format(urlParameters + "&resource=%s", resource); } else if (resourceType.equals("shared link")) { - JSONBody = String.format(JSONBody + ",box_shared_link=%s}", resource); + urlParameters = String.format(urlParameters + "&box_shared_link=%s", resource); } else { String argExceptionMessage = resource + " is not a valid Box API endpoint or shared link"; BoxAPIException e = new BoxAPIException(argExceptionMessage); @@ -809,24 +753,22 @@ public ScopedToken getLowerScopedToken(List scopes, String resource) { }; }; - System.out.println("Constructed JSONBody " + JSONBody); + System.out.println("Constructed URL params " + urlParameters); BoxAPIRequest request = new BoxAPIRequest(this, url, "POST"); request.shouldAuthenticate(false); - request.setBody(JSONBody); - - + request.setBody(urlParameters); - String json; + String jsonResponse; try { BoxJSONResponse response = (BoxJSONResponse) request.send(); - json = response.getJSON(); + jsonResponse = response.getJSON(); } catch (BoxAPIException e) { this.notifyError(e); throw e; } - JsonObject jsonObject = JsonObject.readFrom(json); + JsonObject jsonObject = JsonObject.readFrom(jsonResponse); ScopedToken token = new ScopedToken(jsonObject); token.setObtainedAt(System.currentTimeMillis()); token.setExpiresIn(jsonObject.get("expires_in").asLong() * 1000); diff --git a/src/test/java/com/box/sdk/BoxAPIConnectionTest.java b/src/test/java/com/box/sdk/BoxAPIConnectionTest.java index d097ffadf..932e4b815 100644 --- a/src/test/java/com/box/sdk/BoxAPIConnectionTest.java +++ b/src/test/java/com/box/sdk/BoxAPIConnectionTest.java @@ -636,7 +636,7 @@ public void getLowerScopedTokenForNoteSharedLinkResource() { } @Test - @Category(IntegrationTestJWT.class) + @Category(IntegrationTest.class) public void getLowerScopedTokenWorks() throws IOException { Reader reader = new FileReader("src/test/config/config.json"); BoxConfig boxConfig = BoxConfig.readFrom(reader); From f7e06e08ee65b8e4249e6ea4868260d1f704907b Mon Sep 17 00:00:00 2001 From: Patrick Simon Date: Thu, 4 Jun 2020 16:00:24 -0700 Subject: [PATCH 07/10] Apply suggestions from code review --- src/main/java/com/box/sdk/BoxAPIConnection.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/box/sdk/BoxAPIConnection.java b/src/main/java/com/box/sdk/BoxAPIConnection.java index aab62865a..111cdf999 100644 --- a/src/main/java/com/box/sdk/BoxAPIConnection.java +++ b/src/main/java/com/box/sdk/BoxAPIConnection.java @@ -680,14 +680,12 @@ private String determineResourceLinkType(String resourceLink) { URL validUrl = new URL(resourceLink); String validURLStr = validUrl.toString(); String APIEndpointPattern = "https://api.box.com/2.0/files/\\d+"; - boolean isAPIEndpointMatch = Pattern.matches(APIEndpointPattern, validURLStr); - if (isAPIEndpointMatch) { + String sharedLinkPattern = "(.*box.com/s/.*|.*box.com.*s=.*)"; + if (Pattern.matches(APIEndpointPattern, validURLStr)) { System.out.println(validURLStr + " is valid API endpoint"); resourceType = "api endpoint"; } else { - String sharedLinkPattern = "(.*box.com/s/.*|.*box.com.*s=.*)"; - boolean isSharedLinkMatch = Pattern.matches(sharedLinkPattern, validURLStr); - if (isSharedLinkMatch) { + if (Pattern.matches(sharedLinkPattern, validURLStr)) { System.out.println(validURLStr + " is valid shared link"); resourceType = "shared link"; }; From b2506429a48ffd0384d673a43cc93307790d3609 Mon Sep 17 00:00:00 2001 From: Patrick Simon Date: Fri, 12 Jun 2020 21:45:23 -0700 Subject: [PATCH 08/10] Add enum type, update tests, refactoring --- .../java/com/box/sdk/BoxAPIConnection.java | 124 +++++++++++------- .../com/box/sdk/BoxAPIConnectionTest.java | 76 ++++------- 2 files changed, 100 insertions(+), 100 deletions(-) diff --git a/src/main/java/com/box/sdk/BoxAPIConnection.java b/src/main/java/com/box/sdk/BoxAPIConnection.java index 60f01214d..9077ff568 100644 --- a/src/main/java/com/box/sdk/BoxAPIConnection.java +++ b/src/main/java/com/box/sdk/BoxAPIConnection.java @@ -83,6 +83,28 @@ public class BoxAPIConnection { private RequestInterceptor interceptor; private Map customHeaders; + /** + * Used to categorize the types of resource links. + */ + protected enum ResourceLinkType { + /** + * Catch-all default for resource links that are unknown. + */ + Unknown, + + /** + * Resource URLs that point to an API endipoint such as https://api.box.com/2.0/files/:file_id. + */ + APIEndpoint, + + /** + * Resource URLs that point to a resource that has been shared + * such as https://example.box.com/s/qwertyuiop1234567890asdfghjk + * or https://example.app.box.com/notes/0987654321?s=zxcvbnm1234567890asdfghjk. + */ + SharedLink + } + /** * Constructs a new BoxAPIConnection that authenticates with a developer or access token. * @param accessToken a developer or access token to use for authenticating with the API. @@ -725,44 +747,6 @@ public void setRequestInterceptor(RequestInterceptor interceptor) { this.interceptor = interceptor; } - private String determineResourceLinkType(String resourceLink) { - - String resourceType = null; - - try { - URL validUrl = new URL(resourceLink); - String validURLStr = validUrl.toString(); - String APIEndpointPattern = "https://api.box.com/2.0/files/\\d+"; - String sharedLinkPattern = "(.*box.com/s/.*|.*box.com.*s=.*)"; - if (Pattern.matches(APIEndpointPattern, validURLStr)) { - System.out.println(validURLStr + " is valid API endpoint"); - resourceType = "api endpoint"; - } else { - if (Pattern.matches(sharedLinkPattern, validURLStr)) { - System.out.println(validURLStr + " is valid shared link"); - resourceType = "shared link"; - }; - }; - - } catch (MalformedURLException e) { - System.out.println(resourceLink + " is not a valid URL"); - }; - - return resourceType; - } - - private StringBuilder buildScopesForTokenDownscoping(List scopes) { - StringBuilder spaceSeparatedScopes = new StringBuilder(); - for (int i = 0; i < scopes.size(); i++) { - spaceSeparatedScopes.append(scopes.get(i)); - if (i < scopes.size() - 1) { - spaceSeparatedScopes.append(" "); - } - } - - return spaceSeparatedScopes; - } - /** * Get a lower-scoped token restricted to a resource for the list of scopes that are passed. * @param scopes the list of scopes to which the new token should be restricted for @@ -790,21 +774,24 @@ public ScopedToken getLowerScopedToken(List scopes, String resource) { if (resource != null) { - String resourceType = this.determineResourceLinkType(resource); + ResourceLinkType resourceType = this.determineResourceLinkType(resource); - if (resourceType.equals("api endpoint")) { + if (resourceType == ResourceLinkType.APIEndpoint) { urlParameters = String.format(urlParameters + "&resource=%s", resource); - } else if (resourceType.equals("shared link")) { + } else if (resourceType == ResourceLinkType.SharedLink) { urlParameters = String.format(urlParameters + "&box_shared_link=%s", resource); + } else if (resourceType == ResourceLinkType.Unknown) { + String argExceptionMessage = String.format("Unable to determine resource type: %s", resource); + BoxAPIException e = new BoxAPIException(argExceptionMessage); + this.notifyError(e); + throw e; } else { - String argExceptionMessage = resource + " is not a valid Box API endpoint or shared link"; + String argExceptionMessage = String.format("Unhandled resource type: %s", resource); BoxAPIException e = new BoxAPIException(argExceptionMessage); this.notifyError(e); throw e; - }; - }; - - System.out.println("Constructed URL params " + urlParameters); + } + } BoxAPIRequest request = new BoxAPIRequest(this, url, "POST"); request.shouldAuthenticate(false); @@ -826,6 +813,51 @@ public ScopedToken getLowerScopedToken(List scopes, String resource) { return token; } + /** + * Convert List to space-delimited String. + * Needed for versions prior to Java 8, which don't have String.join(delimiter, list) + * @param scopes the list of scopes to read from + * @return space-delimited String of scopes + */ + private StringBuilder buildScopesForTokenDownscoping(List scopes) { + StringBuilder spaceSeparatedScopes = new StringBuilder(); + for (int i = 0; i < scopes.size(); i++) { + spaceSeparatedScopes.append(scopes.get(i)); + if (i < scopes.size() - 1) { + spaceSeparatedScopes.append(" "); + } + } + + return spaceSeparatedScopes; + } + + /** + * Determines the type of resource, given a link to a Box resource. + * @param resourceLink the resource URL to check + * @return ResourceLinkType that categorizes the provided resourceLink + */ + protected ResourceLinkType determineResourceLinkType(String resourceLink) { + + ResourceLinkType resourceType = ResourceLinkType.Unknown; + + try { + URL validUrl = new URL(resourceLink); + String validURLStr = validUrl.toString(); + final String apiEndpointPattern = "https://api.box.com/2.0/files/\\d+"; + final String sharedLinkPattern = "(.*box.com/s/.*|.*box.com.*s=.*)"; + + if (Pattern.matches(apiEndpointPattern, validURLStr)) { + resourceType = ResourceLinkType.APIEndpoint; + } else if (Pattern.matches(sharedLinkPattern, validURLStr)) { + resourceType = ResourceLinkType.SharedLink; + } + } catch (MalformedURLException e) { + //Swallow exception and return default ResourceLinkType set at top of function + } + + return resourceType; + } + /** * Revokes the tokens associated with this API connection. This results in the connection no * longer being able to make API calls until a fresh authorization is made by calling authenticate() diff --git a/src/test/java/com/box/sdk/BoxAPIConnectionTest.java b/src/test/java/com/box/sdk/BoxAPIConnectionTest.java index 76fdc7519..1e902778b 100644 --- a/src/test/java/com/box/sdk/BoxAPIConnectionTest.java +++ b/src/test/java/com/box/sdk/BoxAPIConnectionTest.java @@ -1,5 +1,6 @@ package com.box.sdk; +import com.box.sdk.BoxAPIConnection.ResourceLinkType; import java.io.FileReader; import java.io.IOException; import java.io.Reader; @@ -33,7 +34,6 @@ import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; public class BoxAPIConnectionTest { - /** * Wiremock */ @@ -578,7 +578,7 @@ public void appUsersManuallyPaginatesCorrectly() throws IOException { @Test @Category(UnitTest.class) - public void getLowerScopedTokenWithNullResource() { + public void getLowerScopedTokenRefreshesTheTokenIfNeededbyCallingGetAccessToken() { BoxAPIConnection api = mock(BoxAPIConnection.class); List scopes = new ArrayList(); @@ -597,60 +597,29 @@ public void getLowerScopedTokenWithNullResource() { @Test @Category(UnitTest.class) - public void getLowerScopedTokenForAPIEndpointResource() { + public void checkAllResourceLinkTypes() { + this.getResourceLinkTypeFromURLString( + "https://api.box.com/2.0/files/1234567890", ResourceLinkType.APIEndpoint); + this.getResourceLinkTypeFromURLString( + "https://example.box.com/s/qwertyuiop1234567890asdfghjkl", ResourceLinkType.SharedLink); + this.getResourceLinkTypeFromURLString( + "https://example.app.box.com/notes/09876321?s=zxcvm123458asdf", ResourceLinkType.SharedLink); + this.getResourceLinkTypeFromURLString( + null, ResourceLinkType.Unknown); + this.getResourceLinkTypeFromURLString( + "", ResourceLinkType.Unknown); + this.getResourceLinkTypeFromURLString( + "qwertyuiopasdfghjklzxcvbnm1234567890", ResourceLinkType.Unknown); + } + + private void getResourceLinkTypeFromURLString(String resource, ResourceLinkType resourceLinkType) { BoxAPIConnection api = mock(BoxAPIConnection.class); + when(api.determineResourceLinkType(resource)) + .thenReturn(resourceLinkType); - List scopes = new ArrayList(); - scopes.add("item_preview"); - String APIEndpointResource = "https://api.box.com/2.0/files/135906984991"; + ResourceLinkType actualResourceLinkType = api.determineResourceLinkType(resource); - when(api.getTokenURL()).thenReturn("https://api.box.com/oauth2/token"); - when(api.getLowerScopedToken(scopes, APIEndpointResource)).thenCallRealMethod(); - try { - api.getLowerScopedToken(scopes, APIEndpointResource); - } catch (RuntimeException e) { - //Ignore it - } - verify(api).getAccessToken(); - } - - @Test - @Category(UnitTest.class) - public void getLowerScopedTokenForSharedLinkResource() { - BoxAPIConnection api = mock(BoxAPIConnection.class); - - List scopes = new ArrayList(); - scopes.add("item_preview"); - - String sharedLinkResource = "https://rungaia.box.com/s/68c1cewvxas7orqmobakg17o61bfrkcu"; - - when(api.getTokenURL()).thenReturn("https://api.box.com/oauth2/token"); - when(api.getLowerScopedToken(scopes, sharedLinkResource)).thenCallRealMethod(); - try { - api.getLowerScopedToken(scopes, sharedLinkResource); - } catch (RuntimeException e) { - //Ignore it - } - verify(api).getAccessToken(); - } - - @Test - @Category(UnitTest.class) - public void getLowerScopedTokenForNoteSharedLinkResource() { - BoxAPIConnection api = mock(BoxAPIConnection.class); - - List scopes = new ArrayList(); - scopes.add("item_preview"); - String noteSharedLinkResource = "https://rungaia.app.box.com/notes/643001418459?s=68c1cewvxas7orqmobakg17o61bfrkcu\n"; - - when(api.getTokenURL()).thenReturn("https://api.box.com/oauth2/token"); - when(api.getLowerScopedToken(scopes, noteSharedLinkResource)).thenCallRealMethod(); - try { - api.getLowerScopedToken(scopes, noteSharedLinkResource); - } catch (RuntimeException e) { - //Ignore it - } - verify(api).getAccessToken(); + Assert.assertEquals(actualResourceLinkType, resourceLinkType); } @Test @@ -664,7 +633,6 @@ public void getLowerScopedTokenWorks() throws IOException { BoxDeveloperEditionAPIConnection.getAppEnterpriseConnection(boxConfig, accessTokenCache); String originalToken = api.getAccessToken(); - String resource = null; List scopes = new ArrayList(); scopes.add("item_preview"); From 2a2f0d4f6fa3cf60e1eda9ca92cbc716e04c795e Mon Sep 17 00:00:00 2001 From: Patrick Simon Date: Mon, 15 Jun 2020 11:13:20 -0700 Subject: [PATCH 09/10] Fix test --- src/test/java/com/box/sdk/BoxAPIConnectionTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/box/sdk/BoxAPIConnectionTest.java b/src/test/java/com/box/sdk/BoxAPIConnectionTest.java index 1e902778b..a88fa4ac9 100644 --- a/src/test/java/com/box/sdk/BoxAPIConnectionTest.java +++ b/src/test/java/com/box/sdk/BoxAPIConnectionTest.java @@ -614,8 +614,10 @@ public void checkAllResourceLinkTypes() { private void getResourceLinkTypeFromURLString(String resource, ResourceLinkType resourceLinkType) { BoxAPIConnection api = mock(BoxAPIConnection.class); +// when(api.determineResourceLinkType(resource)) +// .thenReturn(resourceLinkType); when(api.determineResourceLinkType(resource)) - .thenReturn(resourceLinkType); + .thenCallRealMethod(); ResourceLinkType actualResourceLinkType = api.determineResourceLinkType(resource); From dbbca4bf4cb1cb9b89e3a2379e15234754a3c28b Mon Sep 17 00:00:00 2001 From: Patrick Simon Date: Mon, 15 Jun 2020 11:15:02 -0700 Subject: [PATCH 10/10] Fix test --- src/test/java/com/box/sdk/BoxAPIConnectionTest.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/test/java/com/box/sdk/BoxAPIConnectionTest.java b/src/test/java/com/box/sdk/BoxAPIConnectionTest.java index a88fa4ac9..3ea27e04d 100644 --- a/src/test/java/com/box/sdk/BoxAPIConnectionTest.java +++ b/src/test/java/com/box/sdk/BoxAPIConnectionTest.java @@ -614,13 +614,9 @@ public void checkAllResourceLinkTypes() { private void getResourceLinkTypeFromURLString(String resource, ResourceLinkType resourceLinkType) { BoxAPIConnection api = mock(BoxAPIConnection.class); -// when(api.determineResourceLinkType(resource)) -// .thenReturn(resourceLinkType); when(api.determineResourceLinkType(resource)) .thenCallRealMethod(); - ResourceLinkType actualResourceLinkType = api.determineResourceLinkType(resource); - Assert.assertEquals(actualResourceLinkType, resourceLinkType); }