From d24e0f7146bda295c4b417de5d62fd0e25fc9176 Mon Sep 17 00:00:00 2001 From: Brian Demers Date: Tue, 16 May 2017 11:06:08 -0400 Subject: [PATCH] Set the baseURL for group HREFs Fixes: #1329 --- .../sdk/impl/group/DefaultGroup.java | 2 +- .../impl/okta/OktaUserAccountConverter.java | 7 ++- .../sdk/impl/group/DefaultGroupTest.groovy | 24 ++++---- .../okta/OktaUserAccountConverterTest.groovy | 60 ++++++++++++++----- 4 files changed, 62 insertions(+), 31 deletions(-) diff --git a/impl/src/main/java/com/stormpath/sdk/impl/group/DefaultGroup.java b/impl/src/main/java/com/stormpath/sdk/impl/group/DefaultGroup.java index b97709873f..57d7c057aa 100644 --- a/impl/src/main/java/com/stormpath/sdk/impl/group/DefaultGroup.java +++ b/impl/src/main/java/com/stormpath/sdk/impl/group/DefaultGroup.java @@ -65,7 +65,7 @@ public DefaultGroup(InternalDataStore dataStore) { } public DefaultGroup(InternalDataStore dataStore, Map properties) { - super(dataStore, OktaUserAccountConverter.toStormpathGroup(properties)); + super(dataStore, OktaUserAccountConverter.toStormpathGroup(properties, dataStore.getBaseUrl())); } @Override diff --git a/impl/src/main/java/com/stormpath/sdk/impl/okta/OktaUserAccountConverter.java b/impl/src/main/java/com/stormpath/sdk/impl/okta/OktaUserAccountConverter.java index cc2059d1dc..265fa8df10 100644 --- a/impl/src/main/java/com/stormpath/sdk/impl/okta/OktaUserAccountConverter.java +++ b/impl/src/main/java/com/stormpath/sdk/impl/okta/OktaUserAccountConverter.java @@ -142,9 +142,10 @@ public static Map toAccount(Map userMap, String nullSafePut(accountMap, STORMPATH_EMAIL_VERIFICATION_STATUS, fromEmailStatus(profileMap.get(OKTA_EMAIL_VERIFICATION_STATUS))); String emailVerificationToken = (String) profileMap.get(OKTA_EMAIL_VERIFICATION_TOKEN); - if (!Strings.hasText(emailVerificationToken)) { + if (Strings.hasText(emailVerificationToken)) { Map verificationTokenMap = new LinkedHashMap<>(); verificationTokenMap.put(STORMPATH_HREF, "/emailVerificationTokens/"+emailVerificationToken); + accountMap.put(STORMPATH_EMAIL_VERIFICATION_TOKEN, verificationTokenMap); } // build full name @@ -326,7 +327,7 @@ private static Map trimMap(Map map, String... ke return result; } - public static Map toStormpathGroup(Map oktaGroup) { + public static Map toStormpathGroup(Map oktaGroup, String baseUrl) { if (Collections.isEmpty(oktaGroup) || !oktaGroup.containsKey(OKTA_PROFILE)) { return oktaGroup; @@ -346,7 +347,7 @@ public static Map toStormpathGroup(Map oktaGroup } // _links.self.href -> href - nullSafePut(stormpathGroup, STORMPATH_HREF, "/api/v1/groups/" + oktaGroup.get("id")); + nullSafePut(stormpathGroup, STORMPATH_HREF, baseUrl + "/api/v1/groups/" + oktaGroup.get("id")); stormpathGroup.put(STORMPATH_CUSTOM_DATA, new LinkedHashMap()); diff --git a/impl/src/test/groovy/com/stormpath/sdk/impl/group/DefaultGroupTest.groovy b/impl/src/test/groovy/com/stormpath/sdk/impl/group/DefaultGroupTest.groovy index 60364b51ea..68f0941bf3 100644 --- a/impl/src/test/groovy/com/stormpath/sdk/impl/group/DefaultGroupTest.groovy +++ b/impl/src/test/groovy/com/stormpath/sdk/impl/group/DefaultGroupTest.groovy @@ -73,18 +73,8 @@ class DefaultGroupTest { accountMemberships: [href: "https://api.stormpath.com/v1/groups/koaertnw47ufsjngDFSs/accountMemberships"]] InternalDataStore internalDataStore = createStrictMock(InternalDataStore) - Group defaultGroup = new DefaultGroup(internalDataStore, properties) - - assertNull(defaultGroup.getStatus()) - - defaultGroup = defaultGroup.setName("My new group") - .setDescription("My new description") - .setStatus(GroupStatus.DISABLED) - - assertEquals(defaultGroup.getName(), "My new group") - assertEquals(defaultGroup.getDescription(), "My new description") - assertEquals(defaultGroup.getStatus(), GroupStatus.DISABLED) + expect(internalDataStore.getBaseUrl()).andReturn("https://api.stormpath.com") expect(internalDataStore.instantiate(Directory, properties.directory)).andReturn(new DefaultDirectory(internalDataStore, properties.directory)) expect(internalDataStore.instantiate(Tenant, properties.tenant)).andReturn(new DefaultTenant(internalDataStore, properties.tenant)) @@ -97,7 +87,7 @@ class DefaultGroupTest { expect(internalDataStore.instantiate(GroupMembershipList, properties.accountMemberships)).andReturn(new DefaultGroupMembershipList(internalDataStore, properties.accountMemberships)) - expect(internalDataStore.delete(defaultGroup)) + expect(internalDataStore.delete(anyObject(Group))) def groupMembership = new DefaultGroupMembership(internalDataStore) Account account = createStrictMock(Account) @@ -107,6 +97,16 @@ class DefaultGroupTest { replay internalDataStore, account + Group defaultGroup = new DefaultGroup(internalDataStore, properties) + assertNull(defaultGroup.getStatus()) + defaultGroup = defaultGroup.setName("My new group") + .setDescription("My new description") + .setStatus(GroupStatus.DISABLED) + + assertEquals(defaultGroup.getName(), "My new group") + assertEquals(defaultGroup.getDescription(), "My new description") + assertEquals(defaultGroup.getStatus(), GroupStatus.DISABLED) + def resource = defaultGroup.getDirectory() assertTrue(resource instanceof DefaultDirectory && resource.getHref().equals(properties.directory.href)) diff --git a/impl/src/test/groovy/com/stormpath/sdk/impl/okta/OktaUserAccountConverterTest.groovy b/impl/src/test/groovy/com/stormpath/sdk/impl/okta/OktaUserAccountConverterTest.groovy index 80310bc567..da98ee6be6 100644 --- a/impl/src/test/groovy/com/stormpath/sdk/impl/okta/OktaUserAccountConverterTest.groovy +++ b/impl/src/test/groovy/com/stormpath/sdk/impl/okta/OktaUserAccountConverterTest.groovy @@ -6,6 +6,7 @@ import com.stormpath.sdk.impl.account.DefaultAccount import com.stormpath.sdk.impl.account.DefaultAccountList import com.stormpath.sdk.impl.directory.DefaultCustomData import com.stormpath.sdk.impl.ds.InternalDataStore +import com.stormpath.sdk.impl.group.DefaultGroup import com.stormpath.sdk.resource.Resource import org.easymock.Capture import org.easymock.EasyMock @@ -13,18 +14,9 @@ import org.easymock.IAnswer import org.joda.time.Instant import org.testng.annotations.Test -import static org.easymock.EasyMock.anyObject -import static org.easymock.EasyMock.anyString -import static org.easymock.EasyMock.capture -import static org.easymock.EasyMock.createStrictMock -import static org.easymock.EasyMock.expect -import static org.easymock.EasyMock.isNull -import static org.easymock.EasyMock.replay -import static org.easymock.EasyMock.verify +import static org.easymock.EasyMock.* import static org.hamcrest.MatcherAssert.assertThat -import static org.hamcrest.Matchers.allOf -import static org.hamcrest.Matchers.hasEntry -import static org.hamcrest.Matchers.is +import static org.hamcrest.Matchers.* /** * Tests for {@link OktaUserAccountConverter}. @@ -65,9 +57,6 @@ class OktaUserAccountConverterTest { ] ] - // convert Account map - //def accountMap = new OktaUserAccountConverter().toAccount(userMap) - // create Account object def internalDataStore = createStrictMock(InternalDataStore) def customDataPropsCapture = new Capture() @@ -83,7 +72,7 @@ class OktaUserAccountConverterTest { def account = new DefaultAccount(internalDataStore, userMap) // validate - // verify internalDataStore +// verify internalDataStore assertThat account.givenName, is("Joe") assertThat account.customData, allOf(hasEntry("myCustomAttribute", "foobar")) assertThat account.createdAt, is(Instant.parse("2017-03-06T22:16:30.000Z").toDate()) @@ -91,4 +80,45 @@ class OktaUserAccountConverterTest { } + @Test + void toStormpathGroupTest() { + // define Group map + Map groupMap = [ + id : "foobarGroup", + created : "2017-03-06T18:28:15.000Z", + lastUpdated : "2017-03-06T22:16:30.000Z", + lastMembershipUpdated : "2017-03-06T22:16:30.000Z", + created : "2017-03-06T22:16:30.000Z", + objectClass : [ + "okta:user_group" + ], + profile: [ + name : "Everyone", + description : "All users in your organization" + ] + ] + + // create Account object + def internalDataStore = createMock(InternalDataStore) + expect(internalDataStore.getBaseUrl()).andReturn("https://api.example.com") + expect(internalDataStore.instantiate(eq(CustomData), anyObject(Map))).andAnswer(new IAnswer() { + @Override + CustomData answer() throws Throwable { + return new DefaultCustomData(internalDataStore, null) + } + }).anyTimes() + + replay internalDataStore + + def group = new DefaultGroup(internalDataStore, groupMap) + + // verify internalDataStore + assertThat group.getName(), is("Everyone") + assertThat group.getDescription(), is("All users in your organization") + assertThat group.createdAt, is(Instant.parse("2017-03-06T22:16:30.000Z").toDate()) + assertThat group.modifiedAt, is(Instant.parse("2017-03-06T22:16:30.000Z").toDate()) + assertThat group.getHref(), is("https://api.example.com/api/v1/groups/foobarGroup") + + } + }