Skip to content

Commit 17588f6

Browse files
committed
Adding more tests
1 parent 591accb commit 17588f6

File tree

6 files changed

+340
-0
lines changed

6 files changed

+340
-0
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ dependencies {
4949
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.2.28'
5050
testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '1.3'
5151
testCompile group: 'org.skyscreamer', name: 'jsonassert', version: '1.4.0'
52+
testCompile group: 'org.jsoup', name: 'jsoup', version: '1.10.2'
5253
testCompile 'com.squareup.okhttp3:mockwebserver:3.8.1'
5354
}
5455

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2017 ThoughtWorks, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cd.go.authorization.okta;
18+
19+
import cd.go.authorization.okta.models.*;
20+
import cd.go.authorization.okta.OktaUser;
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
24+
import java.util.Collections;
25+
import java.util.List;
26+
27+
import static java.util.Collections.singletonList;
28+
import static java.util.Collections.singletonMap;
29+
import static org.hamcrest.Matchers.contains;
30+
import static org.hamcrest.Matchers.hasSize;
31+
import static org.junit.Assert.assertThat;
32+
import static org.mockito.Mockito.*;
33+
34+
public class OktaAuthorizerTest {
35+
36+
private OktaAuthorizer authorizer;
37+
private OktaUser loggedInUser;
38+
private MembershipChecker membershipChecker;
39+
private AuthConfig authConfig;
40+
41+
@Before
42+
public void setUp() throws Exception {
43+
membershipChecker = mock(MembershipChecker.class);
44+
loggedInUser = mock(OktaUser.class);
45+
authConfig = mock(AuthConfig.class);
46+
47+
authorizer = new OktaAuthorizer(membershipChecker);
48+
}
49+
50+
@Test
51+
public void shouldReturnEmptyListIfNoRoleConfiguredForGivenAuthConfig() throws Exception {
52+
final List<String> assignedRoles = authorizer.authorize(loggedInUser, authConfig, Collections.emptyList());
53+
54+
assertThat(assignedRoles, hasSize(0));
55+
verifyZeroInteractions(authConfig);
56+
verifyZeroInteractions(membershipChecker);
57+
}
58+
59+
@Test
60+
public void shouldAssignRoleIfUserIsAMemberOfAtLeastOneGroup() throws Exception {
61+
final Role role = mock(Role.class);
62+
final OktaRoleConfiguration roleConfiguration = mock(OktaRoleConfiguration.class);
63+
64+
when(role.name()).thenReturn("admin");
65+
when(role.roleConfiguration()).thenReturn(roleConfiguration);
66+
when(roleConfiguration.groups()).thenReturn(singletonList("group-1"));
67+
when(membershipChecker.isAMemberOfAtLeastOneGroup(loggedInUser, authConfig, roleConfiguration.groups())).thenReturn(true);
68+
69+
final List<String> assignedRoles = authorizer.authorize(loggedInUser, authConfig, singletonList(role));
70+
71+
assertThat(assignedRoles, hasSize(1));
72+
assertThat(assignedRoles, contains("admin"));
73+
}
74+
75+
@Test
76+
public void shouldNotAssignRoleIfUserIsNotMemberOfAnyGroup() throws Exception {
77+
final Role role = mock(Role.class);
78+
final OktaRoleConfiguration roleConfiguration = mock(OktaRoleConfiguration.class);
79+
80+
when(role.name()).thenReturn("admin");
81+
when(role.roleConfiguration()).thenReturn(roleConfiguration);
82+
when(roleConfiguration.groups()).thenReturn(singletonList("group-1"));
83+
when(membershipChecker.isAMemberOfAtLeastOneGroup(loggedInUser, authConfig, roleConfiguration.groups())).thenReturn(false);
84+
85+
final List<String> assignedRoles = authorizer.authorize(loggedInUser, authConfig, singletonList(role));
86+
87+
assertThat(assignedRoles, hasSize(0));
88+
}
89+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2017 ThoughtWorks, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cd.go.authorization.okta.executors;
18+
19+
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
20+
import org.junit.Test;
21+
import org.skyscreamer.jsonassert.JSONAssert;
22+
23+
import static org.hamcrest.Matchers.is;
24+
import static org.junit.Assert.assertThat;
25+
26+
public class GetRoleConfigMetadataRequestExecutorTest {
27+
28+
@Test
29+
public void shouldReturnRoleConfigMetadata() throws Exception {
30+
final GoPluginApiResponse response = new GetRoleConfigMetadataRequestExecutor().execute();
31+
32+
assertThat(response.responseCode(), is(200));
33+
34+
final String expectedRoleConfigMetadata = "[\n" +
35+
" {\n" +
36+
" \"key\": \"Groups\",\n" +
37+
" \"metadata\": {\n" +
38+
" \"required\": false,\n" +
39+
" \"secure\": false\n" +
40+
" }\n" +
41+
" },\n" +
42+
" {\n" +
43+
" \"key\": \"Users\",\n" +
44+
" \"metadata\": {\n" +
45+
" \"required\": false,\n" +
46+
" \"secure\": false\n" +
47+
" }\n" +
48+
" }\n" +
49+
"]";
50+
51+
JSONAssert.assertEquals(expectedRoleConfigMetadata, response.responseBody(), true);
52+
}
53+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2017 ThoughtWorks, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cd.go.authorization.okta.executors;
18+
19+
import cd.go.authorization.okta.annotation.MetadataHelper;
20+
import cd.go.authorization.okta.annotation.ProfileMetadata;
21+
import cd.go.authorization.okta.models.OktaRoleConfiguration;
22+
import cd.go.authorization.okta.utils.Util;
23+
import com.google.gson.Gson;
24+
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
25+
import org.jsoup.Jsoup;
26+
import org.jsoup.nodes.Document;
27+
import org.jsoup.select.Elements;
28+
import org.junit.Test;
29+
30+
import java.util.HashMap;
31+
import java.util.List;
32+
import java.util.Map;
33+
34+
import static org.hamcrest.Matchers.*;
35+
import static org.junit.Assert.assertThat;
36+
37+
public class GetRoleConfigViewRequestExecutorTest {
38+
39+
@Test
40+
public void allFieldsShouldBePresentInView() throws Exception {
41+
String template = Util.readResource("/role-config.template.html");
42+
final Document document = Jsoup.parse(template);
43+
44+
final List<ProfileMetadata> metadataList = MetadataHelper.getMetadata(OktaRoleConfiguration.class);
45+
for (ProfileMetadata field : metadataList) {
46+
final Elements inputFieldForKey = document.getElementsByAttributeValue("ng-model", field.getKey());
47+
assertThat(inputFieldForKey, hasSize(1));
48+
49+
final Elements spanToShowError = document.getElementsByAttributeValue("ng-class", "{'is-visible': GOINPUTNAME[" + field.getKey() + "].$error.server}");
50+
assertThat(spanToShowError, hasSize(1));
51+
assertThat(spanToShowError.attr("ng-show"), is("GOINPUTNAME[" + field.getKey() + "].$error.server"));
52+
assertThat(spanToShowError.text(), is("{{GOINPUTNAME[" + field.getKey() + "].$error.server}}"));
53+
}
54+
55+
final Elements inputs = document.select("textarea,input,select");
56+
assertThat("should contains only inputs that defined in OktaRoleConfiguration.java",inputs, hasSize(metadataList.size()));
57+
}
58+
59+
@Test
60+
public void shouldRenderTheTemplateInJSON() throws Exception {
61+
GoPluginApiResponse response = new GetRoleConfigViewRequestExecutor().execute();
62+
assertThat(response.responseCode(), is(200));
63+
Map<String, String> hashSet = new Gson().fromJson(response.responseBody(), HashMap.class);
64+
assertThat(hashSet, hasEntry("template", Util.readResource("/role-config.template.html")));
65+
}
66+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2017 ThoughtWorks, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cd.go.authorization.okta.requests;
18+
19+
import com.google.gson.Gson;
20+
import com.thoughtworks.go.plugin.api.request.GoPluginApiRequest;
21+
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.skyscreamer.jsonassert.JSONAssert;
25+
import org.skyscreamer.jsonassert.JSONCompareMode;
26+
27+
import java.util.Collections;
28+
29+
import static java.util.Collections.singletonMap;
30+
import static org.mockito.Mockito.mock;
31+
import static org.mockito.Mockito.when;
32+
33+
public class RoleConfigValidateRequestExecutorTest {
34+
private GoPluginApiRequest request;
35+
36+
@Before
37+
public void setup() throws Exception {
38+
request = mock(GoPluginApiRequest.class);
39+
}
40+
41+
@Test
42+
public void shouldValidateEmptyRoleConfig() throws Exception {
43+
when(request.requestBody()).thenReturn(new Gson().toJson(Collections.emptyMap()));
44+
45+
GoPluginApiResponse response = RoleConfigValidateRequest.from(request).execute();
46+
String json = response.responseBody();
47+
48+
String expectedJSON = "[\n" +
49+
" {\n" +
50+
" \"key\": \"Users\",\n" +
51+
" \"message\": \"At least one of the fields(groups or users) should be specified.\"\n" +
52+
" },\n" +
53+
" {\n" +
54+
" \"key\": \"Groups\",\n" +
55+
" \"message\": \"At least one of the fields(groups or users) should be specified.\"\n" +
56+
" }\n" +
57+
"]";
58+
59+
JSONAssert.assertEquals(expectedJSON, json, JSONCompareMode.NON_EXTENSIBLE);
60+
}
61+
62+
@Test
63+
public void shouldValidateValidRoleConfig() throws Exception {
64+
when(request.requestBody()).thenReturn(new Gson().toJson(singletonMap("Groups", "Users")));
65+
66+
GoPluginApiResponse response = RoleConfigValidateRequest.from(request).execute();
67+
String json = response.responseBody();
68+
69+
String expectedJSON = "[]";
70+
71+
JSONAssert.assertEquals(expectedJSON, json, JSONCompareMode.NON_EXTENSIBLE);
72+
}
73+
74+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2017 ThoughtWorks, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cd.go.authorization.okta.requests;
18+
19+
import cd.go.authorization.okta.models.OktaRoleConfiguration;
20+
import com.thoughtworks.go.plugin.api.request.GoPluginApiRequest;
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
import org.mockito.Mock;
24+
25+
import static java.util.Arrays.asList;
26+
import static org.hamcrest.Matchers.contains;
27+
import static org.hamcrest.Matchers.hasEntry;
28+
import static org.junit.Assert.assertThat;
29+
import static org.mockito.Mockito.when;
30+
import static org.mockito.MockitoAnnotations.initMocks;
31+
32+
public class RoleConfigValidateRequestTest {
33+
34+
@Mock
35+
private GoPluginApiRequest apiRequest;
36+
37+
@Before
38+
public void setUp() throws Exception {
39+
initMocks(this);
40+
}
41+
42+
@Test
43+
public void shouldDeserializeGoPluginApiRequestToRoleConfigValidateRequest() throws Exception {
44+
String responseBody = "{\n" +
45+
" \"Groups\": \"group-1,group-2\",\n" +
46+
" \"Users\": \"bob,alice\"\n" +
47+
"}";
48+
49+
when(apiRequest.requestBody()).thenReturn(responseBody);
50+
51+
final RoleConfigValidateRequest request = RoleConfigValidateRequest.from(apiRequest);
52+
final OktaRoleConfiguration oktaRoleConfiguration = request.oktaRoleConfiguration();
53+
54+
assertThat(oktaRoleConfiguration.groups(), contains("group-1", "group-2"));
55+
assertThat(oktaRoleConfiguration.users(), contains("bob", "alice"));
56+
}
57+
}

0 commit comments

Comments
 (0)