Skip to content
This repository has been archived by the owner on Dec 12, 2018. It is now read-only.

Commit

Permalink
Set the baseURL for group HREFs
Browse files Browse the repository at this point in the history
Fixes: #1329
  • Loading branch information
bdemers committed May 16, 2017
1 parent 23d219e commit d24e0f7
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public DefaultGroup(InternalDataStore dataStore) {
}

public DefaultGroup(InternalDataStore dataStore, Map<String, Object> properties) {
super(dataStore, OktaUserAccountConverter.toStormpathGroup(properties));
super(dataStore, OktaUserAccountConverter.toStormpathGroup(properties, dataStore.getBaseUrl()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,10 @@ public static Map<String, Object> toAccount(Map<String, Object> 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<String, Object> verificationTokenMap = new LinkedHashMap<>();
verificationTokenMap.put(STORMPATH_HREF, "/emailVerificationTokens/"+emailVerificationToken);
accountMap.put(STORMPATH_EMAIL_VERIFICATION_TOKEN, verificationTokenMap);
}

// build full name
Expand Down Expand Up @@ -326,7 +327,7 @@ private static Map<String, Object> trimMap(Map<String, Object> map, String... ke
return result;
}

public static Map<String, Object> toStormpathGroup(Map<String, Object> oktaGroup) {
public static Map<String, Object> toStormpathGroup(Map<String, Object> oktaGroup, String baseUrl) {

if (Collections.isEmpty(oktaGroup) || !oktaGroup.containsKey(OKTA_PROFILE)) {
return oktaGroup;
Expand All @@ -346,7 +347,7 @@ public static Map<String, Object> toStormpathGroup(Map<String, Object> 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<String, Object>());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
Expand All @@ -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))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,17 @@ 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
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}.
Expand Down Expand Up @@ -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<Map>()
Expand All @@ -83,12 +72,53 @@ 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())
assertThat account.modifiedAt, is(Instant.parse("2017-03-06T22:16:30.000Z").toDate())

}

@Test
void toStormpathGroupTest() {
// define Group map
Map<String, Object> 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<CustomData>() {
@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")

}

}

0 comments on commit d24e0f7

Please sign in to comment.