diff --git a/google-oauth-client/src/test/java/com/google/api/client/auth/oauth/OAuthParametersTest.java b/google-oauth-client/src/test/java/com/google/api/client/auth/oauth/OAuthParametersTest.java index 72f22801c..f31afb000 100644 --- a/google-oauth-client/src/test/java/com/google/api/client/auth/oauth/OAuthParametersTest.java +++ b/google-oauth-client/src/test/java/com/google/api/client/auth/oauth/OAuthParametersTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Google Inc. + * Copyright (c) 2010-2017 Google Inc. * * 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 @@ -20,11 +20,11 @@ * Tests {@link OAuthParameters}. * * @author Yaniv Inbar + * @author Michael Hausegger, hausegger.michael@googlemail.com */ public class OAuthParametersTest extends TestCase { - public OAuthParametersTest() { - } + public OAuthParametersTest() {} public OAuthParametersTest(String name) { super(name); @@ -49,10 +49,41 @@ public void testGetAuthorizationHeader() { parameters.timestamp = "1274732403"; parameters.token = "4/1mZ3ZPynTry3szE49h3XyXk24p_I"; parameters.signature = "OTfTeiNjKsNeqBtYhUPIiJO9pC4="; - assertEquals("OAuth oauth_consumer_key=\"anonymous\", oauth_nonce=\"b51df3249df9dfd\", " - + "oauth_signature=\"OTfTeiNjKsNeqBtYhUPIiJO9pC4%3D\", " - + "oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1274732403\", " - + "oauth_token=\"4%2F1mZ3ZPynTry3szE49h3XyXk24p_I\", " - + "oauth_verifier=\"gZ1BFee1qSijpqbxfnX%2Bo8rQ\"", parameters.getAuthorizationHeader()); + assertEquals( + "OAuth oauth_consumer_key=\"anonymous\", oauth_nonce=\"b51df3249df9dfd\", " + + "oauth_signature=\"OTfTeiNjKsNeqBtYhUPIiJO9pC4%3D\", " + + "oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1274732403\", " + + "oauth_token=\"4%2F1mZ3ZPynTry3szE49h3XyXk24p_I\", " + + "oauth_verifier=\"gZ1BFee1qSijpqbxfnX%2Bo8rQ\"", + parameters.getAuthorizationHeader()); + } + + public void testComputeNonceActuallyChangesNonce() { + + OAuthParameters oAuthParameters = new OAuthParameters(); + + assertNull(oAuthParameters.nonce); + + oAuthParameters.computeNonce(); + + assertNotNull(oAuthParameters.nonce); + + String oldNonceOne = oAuthParameters.nonce; + + oAuthParameters.computeNonce(); + + assertNotNull(oAuthParameters.nonce); + + assertFalse(oldNonceOne.equals(oAuthParameters.nonce)); + + String oldNonceTwo = oAuthParameters.nonce; + + oAuthParameters.computeNonce(); + + assertNotNull(oAuthParameters.nonce); + + assertFalse(oldNonceOne.equals(oAuthParameters.nonce)); + + assertFalse(oldNonceTwo.equals(oAuthParameters.nonce)); } } diff --git a/google-oauth-client/src/test/java/com/google/api/client/auth/oauth2/StoredCredentialTest.java b/google-oauth-client/src/test/java/com/google/api/client/auth/oauth2/StoredCredentialTest.java new file mode 100644 index 000000000..767c3227c --- /dev/null +++ b/google-oauth-client/src/test/java/com/google/api/client/auth/oauth2/StoredCredentialTest.java @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2017 Google Inc. + * + * 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.api.client.auth.oauth2; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Unit tests for class {@link StoredCredential}. + * + * @author Michael Hausegger, hausegger.michael@googlemail.com + */ +public class StoredCredentialTest { + + @Test + public void testGetRefreshTokenReturningNonEmptyString() throws Exception { + + StoredCredential storedCredential = new StoredCredential(); + StoredCredential storedCredentialTwo = storedCredential.setRefreshToken("HEAD"); + + assertEquals("HEAD", storedCredentialTwo.getRefreshToken()); + } + + @Test + public void testSetGetExpirationTimeMillisecondsAndSetExpirationTimeMilliseconds() + throws Exception { + + StoredCredential storedCredential = new StoredCredential(); + Long longValue = new Long(1000000L); + StoredCredential storedCredentialTwo = + storedCredential.setExpirationTimeMilliseconds(longValue); + + assertEquals(1000000L, (long) storedCredential.getExpirationTimeMilliseconds()); + } + + @Test + public void testGetAccessTokenReturningNonEmptyString() throws Exception { + + StoredCredential storedCredential = new StoredCredential(); + StoredCredential storedCredentialTwo = storedCredential.setAccessToken("a"); + + assertEquals("a", storedCredentialTwo.getAccessToken()); + } + + @Test(expected = NullPointerException.class) + public void testGetDefaultDataStoreThrowsNullPointerException() throws Exception { + + StoredCredential.getDefaultDataStore(null); + } + + @Test(expected = NullPointerException.class) + public void testFailsToCreateStoredCredentialTakingCredentialThrowsNullPointerException() + throws Exception { + + new StoredCredential(null); + } + + @Test + public void testEqualsReturningTrue() throws Exception { + + StoredCredential storedCredential = new StoredCredential(); + StoredCredential storedCredentialTwo = new StoredCredential(); + + assertTrue(storedCredential.equals(storedCredentialTwo)); + assertTrue(storedCredentialTwo.equals(storedCredential)); + + assertTrue(storedCredential.equals(storedCredential)); + assertTrue(storedCredentialTwo.equals(storedCredentialTwo)); + } + + @Test + public void testEqualsReturningFalse() throws Exception { + + StoredCredential storedCredential = new StoredCredential(); + StoredCredential storedCredentialTwo = new StoredCredential(); + + storedCredentialTwo.setAccessToken("a"); + + assertFalse(storedCredential.equals(storedCredentialTwo)); + assertFalse(storedCredentialTwo.equals(storedCredential)); + + assertFalse(storedCredential.equals(null)); + assertFalse(storedCredentialTwo.equals(null)); + } + + @Test + public void testToString() throws Exception { + + StoredCredential storedCredential = new StoredCredential(); + + assertEquals( + "Class{accessToken=null, refreshToken=null, expirationTimeMilliseconds=null}", + storedCredential.toString()); + } +} diff --git a/google-oauth-client/src/test/java/com/google/api/client/auth/openidconnect/IdTokenResponseTest.java b/google-oauth-client/src/test/java/com/google/api/client/auth/openidconnect/IdTokenResponseTest.java new file mode 100644 index 000000000..827dbb9b1 --- /dev/null +++ b/google-oauth-client/src/test/java/com/google/api/client/auth/openidconnect/IdTokenResponseTest.java @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2017 Google Inc. + * + * 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.api.client.auth.openidconnect; + +import com.google.api.client.json.jackson.JacksonFactory; +import org.junit.Test; + +import static junit.framework.Assert.assertNotNull; +import static org.junit.Assert.*; + +/** + * Unit tests for class {@link IdTokenResponse}. + * + * @author Michael Hausegger, hausegger.michael@googlemail.com + */ +public class IdTokenResponseTest { + + @Test(expected = NullPointerException.class) + public void testSetTokenTypeThrowsNullPointerException() throws Exception { + + IdTokenResponse idTokenResponse = new IdTokenResponse(); + + idTokenResponse.setTokenType(null); + } + + @Test(expected = NullPointerException.class) + public void testSetIdTokenThrowsNullPointerException() throws Exception { + + IdTokenResponse idTokenResponse = new IdTokenResponse(); + + idTokenResponse.setIdToken(null); + } + + @Test(expected = NullPointerException.class) + public void testSetAccessTokenThrowsNullPointerException() throws Exception { + + IdTokenResponse idTokenResponse = new IdTokenResponse(); + + idTokenResponse.setAccessToken(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testParseIdTokenThrowsIllegalArgumentException() throws Exception { + + IdTokenResponse idTokenResponse = new IdTokenResponse(); + JacksonFactory jacksonFactory = new JacksonFactory(); + idTokenResponse.setFactory(jacksonFactory); + IdTokenResponse idTokenResponseTwo = idTokenResponse.setIdToken(""); + + idTokenResponseTwo.parseIdToken(); + } + + @Test(expected = NullPointerException.class) + public void testExecuteThrowsNullPointerExceptionAndExecuteWithNull() throws Exception { + + IdTokenResponse.execute(null); + } + + @Test + public void testSetGetIdToken() throws Exception { + + IdTokenResponse idTokenResponseOne = new IdTokenResponse(); + IdTokenResponse idTokenResponseTwo = idTokenResponseOne.setIdToken("a"); + + assertNull(idTokenResponseOne.getScope()); + assertEquals("a", idTokenResponseOne.getIdToken()); + + assertNull(idTokenResponseOne.getTokenType()); + assertNull(idTokenResponseOne.getAccessToken()); + + assertNull(idTokenResponseOne.getRefreshToken()); + assertEquals("a", idTokenResponseTwo.getIdToken()); + + assertNull(idTokenResponseTwo.getAccessToken()); + assertNull(idTokenResponseTwo.getRefreshToken()); + + assertNull(idTokenResponseTwo.getScope()); + assertNull(idTokenResponseTwo.getTokenType()); + + assertSame(idTokenResponseOne, idTokenResponseTwo); + assertSame(idTokenResponseTwo, idTokenResponseOne); + + String idTokenString = idTokenResponseOne.getIdToken(); + + assertEquals("a", idTokenString); + assertNull(idTokenResponseOne.getScope()); + + assertEquals("a", idTokenResponseOne.getIdToken()); + assertNull(idTokenResponseOne.getTokenType()); + + assertNull(idTokenResponseOne.getAccessToken()); + assertNull(idTokenResponseOne.getRefreshToken()); + + assertNotNull(idTokenString); + assertSame(idTokenResponseOne, idTokenResponseTwo); + } +}