/** * Tests that the builder correctly identifies the file path. */ @Test public void testReadPropertiesFromFile_refreshTokenBadWithFilePath() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientId", "clientId"); config.setProperty("api.admanager.clientSecret", "clientSecret"); when(configurationHelper.fromFile("/home/user/path")).thenReturn(config); ForApiBuilder builder = new OfflineCredentials.ForApiBuilder( configurationHelper, OfflineCredentials.Api.AD_MANAGER, oAuth2Helper); thrown.expect(ValidationException.class); thrown.expectMessage( "A refresh token must be set as api.admanager.refreshToken in /home/user/path." + "\nIt is required for offline credentials. If you need to create one, " + "see the GetRefreshToken example."); builder.fromFile("/home/user/path").build(); }
/** * Tests that the builder correctly identifies the file path. */ @Test public void testReadPropertiesFromFile_clientSecretTokenBadWithFilePath() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientId", "clientId"); config.setProperty("api.admanager.refreshToken", "refreshToken"); when(configurationHelper.fromFile("/home/user/path")).thenReturn(config); ForApiBuilder builder = new OfflineCredentials.ForApiBuilder( configurationHelper, OfflineCredentials.Api.AD_MANAGER, oAuth2Helper); thrown.expect(ValidationException.class); thrown.expectMessage( "Client secret must be set as api.admanager.clientSecret in /home/user/path." + "\nIf you do not have a client ID or secret, please create one in the API " + "console: https://console.developers.google.com/project"); builder.fromFile("/home/user/path").build(); }
/** * Tests that the builder correctly identifies the file path. */ @Test public void testReadPropertiesFromFile_clientIdTokenBadWithFilePath() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientSecret", "clientSecret"); config.setProperty("api.admanager.refreshToken", "refreshToken"); when(configurationHelper.fromFile("/home/user/path")).thenReturn(config); ForApiBuilder builder = new OfflineCredentials.ForApiBuilder( configurationHelper, OfflineCredentials.Api.AD_MANAGER, oAuth2Helper); thrown.expect(ValidationException.class); thrown.expectMessage( "Client ID must be set as api.admanager.clientId in /home/user/path." + "\nIf you do not have a client ID or secret, please create one in the API " + "console: https://console.developers.google.com/project"); builder.fromFile("/home/user/path").build(); }
/** * Tests that the builder correctly reads properties from a configuration. */ @Test public void testReadPropertiesFromConfiguration_adwords() throws ValidationException { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.adwords.clientId", "clientId"); config.setProperty("api.adwords.clientSecret", "clientSecret"); config.setProperty("api.adwords.refreshToken", "refreshToken"); OfflineCredentials offlineCredentials = new OfflineCredentials.Builder() .forApi(OfflineCredentials.Api.ADWORDS) .from(config) .build(); assertEquals("clientId", offlineCredentials.getClientId()); assertEquals("clientSecret", offlineCredentials.getClientSecret()); assertEquals("refreshToken", offlineCredentials.getRefreshToken()); }
/** * Tests that the builder correctly reads properties from a configuration. */ @Test public void testReadPropertiesFromConfiguration_dfp() throws ValidationException { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientId", "clientId"); config.setProperty("api.admanager.clientSecret", "clientSecret"); config.setProperty("api.admanager.refreshToken", "refreshToken"); OfflineCredentials offlineCredentials = new OfflineCredentials.Builder() .forApi(OfflineCredentials.Api.AD_MANAGER) .from(config) .build(); assertEquals("clientId", offlineCredentials.getClientId()); assertEquals("clientSecret", offlineCredentials.getClientSecret()); assertEquals("refreshToken", offlineCredentials.getRefreshToken()); }
/** * Tests that the builder builds correctly. */ @Test public void testBuilder() throws Exception { HttpTransport httpTransport = new NetHttpTransport(); OfflineCredentials offlineCredentials = new OfflineCredentials.Builder() .forApi(OfflineCredentials.Api.AD_MANAGER) .withClientSecrets("clientId", "clientSecret") .withRefreshToken("refreshToken") .withHttpTransport(httpTransport) .build(); assertEquals("clientId", offlineCredentials.getClientId()); assertEquals("clientSecret", offlineCredentials.getClientSecret()); assertEquals("refreshToken", offlineCredentials.getRefreshToken()); assertSame(httpTransport, offlineCredentials.getHttpTransport()); }
/** * Tests that the builder correctly reads from a file. */ @Test public void testReadPropertiesFromFile() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientId", "clientId"); config.setProperty("api.admanager.clientSecret", "clientSecret"); config.setProperty("api.admanager.refreshToken", "refreshToken"); when(configurationHelper.fromFile("path")).thenReturn(config); ForApiBuilder builder = new OfflineCredentials.ForApiBuilder( configurationHelper, OfflineCredentials.Api.AD_MANAGER, oAuth2Helper); OfflineCredentials offlineCredentials = builder.fromFile("path").build(); assertEquals("clientId", offlineCredentials.getClientId()); assertEquals("clientSecret", offlineCredentials.getClientSecret()); assertEquals("refreshToken", offlineCredentials.getRefreshToken()); }
/** * Tests generating OAuth2 credentials. */ @Test public void testGenerateCredential_cantRefresh() throws Exception { OfflineCredentials offlineCredentials = new OfflineCredentials.Builder(oAuth2Helper) .forApi(OfflineCredentials.Api.AD_MANAGER) .withClientSecrets("clientId", "clientSecret") .withRefreshToken("refreshToken") .build(); when(oAuth2Helper.callRefreshToken(Mockito.<Credential>anyObject())).thenReturn(false); thrown.expect(OAuthException.class); offlineCredentials.generateCredential(); } }
/** * Tests that the builder correctly leaves out the file path. */ @Test public void testReadPropertiesFromFile_clientSecretNoFilePath() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientId", "clientId"); config.setProperty("api.admanager.refreshToken", "refreshToken"); ForApiBuilder builder = new OfflineCredentials.ForApiBuilder( configurationHelper, OfflineCredentials.Api.AD_MANAGER, oAuth2Helper); thrown.expect(ValidationException.class); thrown.expectMessage("Client secret must be set." + "\nIf you do not have a client ID or secret, please create one in the API " + "console: https://console.developers.google.com/project"); builder.from(config).build(); }
/** * Tests that the builder correctly leaves out the file path. */ @Test public void testReadPropertiesFromFile_refreshTokenNoFilePath() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientId", "clientId"); config.setProperty("api.admanager.clientSecret", "clientSecret"); ForApiBuilder builder = new OfflineCredentials.ForApiBuilder( configurationHelper, OfflineCredentials.Api.AD_MANAGER, oAuth2Helper); thrown.expect(ValidationException.class); thrown.expectMessage("A refresh token must be set." + "\nIt is required for offline credentials. If you need to create one, " + "see the GetRefreshToken example."); builder.from(config).build(); }
/** * Tests that the builder correctly leaves out the file path. */ @Test public void testReadPropertiesFromFile_clientIdNoFilePath() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientSecret", "clientSecret"); config.setProperty("api.admanager.refreshToken", "refreshToken"); ForApiBuilder builder = new OfflineCredentials.ForApiBuilder( configurationHelper, OfflineCredentials.Api.AD_MANAGER, oAuth2Helper); thrown.expect(ValidationException.class); thrown.expectMessage("Client ID must be set." + "\nIf you do not have a client ID or secret, please create one in the API " + "console: https://console.developers.google.com/project"); builder.from(config).build(); }
/** * Tests that the builder correctly fails on a bad configuration. */ @Test public void testReadPropertiesFromConfiguration_multipleOAuthTypes() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientSecret", "clientSecret"); config.setProperty("api.admanager.jsonKeyFilePath", "jsonKeyFilePath"); thrown.expect(ValidationException.class); new OfflineCredentials.Builder() .forApi(OfflineCredentials.Api.AD_MANAGER) .from(config) .build(); }
/** * Tests that the builder correctly fails on a bad configuration. */ @Test public void testReadPropertiesFromConfiguration_missingRefreshToken() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientId", "clientId"); config.setProperty("api.admanager.clientSecret", "clientSecret"); thrown.expect(ValidationException.class); new OfflineCredentials.Builder() .forApi(OfflineCredentials.Api.AD_MANAGER) .from(config) .build(); }
/** * Tests generating OAuth2 credentials. */ @Test public void testGenerateCredential_IOException() throws Exception { OfflineCredentials offlineCredentials = new OfflineCredentials.Builder(oAuth2Helper) .forApi(OfflineCredentials.Api.AD_MANAGER) .withClientSecrets("clientId", "clientSecret") .withRefreshToken("refreshToken") .build(); when(oAuth2Helper.callRefreshToken(Mockito.<Credential>anyObject())) .thenThrow(new IOException()); thrown.expect(OAuthException.class); offlineCredentials.generateCredential(); }
/** * Tests that the builder correctly reads properties from a configuration. */ @Test public void testReadPropertiesFromConfiguration_properPrefixServiceAccount() throws ValidationException { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.jsonKeyFilePath", "jsonKeyFilePathDfp"); config.setProperty("api.adwords.jsonKeyFilePath", "jsonKeyFilePathAdWords"); OfflineCredentials offlineCredentials = new OfflineCredentials.Builder() .forApi(OfflineCredentials.Api.AD_MANAGER) .from(config) .build(); assertEquals("jsonKeyFilePathDfp", offlineCredentials.getJsonKeyFilePath()); }
/** * Tests that the builder does not fail when missing everything but a service account key and * service account user. */ @Test public void testReadPropertiesFromConfiguration_onlyKeyFilePathAndUser() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.jsonKeyFilePath", "jsonKeyFilePath"); config.setProperty("api.admanager.serviceAccountUser", "someUser@example.com"); OfflineCredentials credentials = new Builder().forApi(Api.AD_MANAGER).from(config).build(); assertEquals( "service account user should have been set from the config", "someUser@example.com", credentials.getServiceAccountUser()); }
/** * Tests that the builder correctly fails on a bad configuration. */ @Test public void testReadPropertiesFromConfiguration_missingClientId() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientSecret", "clientSecret"); config.setProperty("api.admanager.refreshToken", "refreshToken"); thrown.expect(ValidationException.class); new OfflineCredentials.Builder() .forApi(OfflineCredentials.Api.AD_MANAGER) .from(config) .build(); }
/** * Tests that the builder correctly fails on a bad configuration. */ @Test public void testReadPropertiesFromConfiguration_missingClientSecret() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientId", "clientId"); config.setProperty("api.admanager.refreshToken", "refreshToken"); thrown.expect(ValidationException.class); new OfflineCredentials.Builder() .forApi(OfflineCredentials.Api.AD_MANAGER) .from(config) .build(); }
/** * Tests that the builder builds correctly using a service account key file. */ @Test public void testBuilder_serviceAccount() throws Exception { OfflineCredentials offlineCredentials = new OfflineCredentials.Builder() .forApi(OfflineCredentials.Api.AD_MANAGER) .withJsonKeyFilePath("jsonKeyFilePath") .build(); assertEquals("jsonKeyFilePath", offlineCredentials.getJsonKeyFilePath()); }
/** * Tests that the builder does not fail when missing everything but a service account key. */ @Test public void testReadPropertiesFromConfiguration_onlyKeyFilePath() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.jsonKeyFilePath", "jsonKeyFilePath"); new OfflineCredentials.Builder() .forApi(OfflineCredentials.Api.AD_MANAGER) .from(config) .build(); }