Skip to content

Commit ec908bb

Browse files
luanderjgrandja
authored andcommitted
Add unit tests for endpoints package
Fixes gh-4499 This commit contains unit tests for the endpoints package in oauth2-core.
1 parent bc6be86 commit ec908bb

File tree

7 files changed

+368
-5
lines changed

7 files changed

+368
-5
lines changed

oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/AuthorizationCodeTokenRequestAttributes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,18 @@ private Builder(String code) {
5858
}
5959

6060
public Builder clientId(String clientId) {
61-
Assert.hasText(clientId, "clientId cannot be empty");
6261
this.authorizationCodeTokenRequest.clientId = clientId;
6362
return this;
6463
}
6564

6665
public Builder redirectUri(String redirectUri) {
67-
Assert.hasText(redirectUri, "redirectUri cannot be empty");
6866
this.authorizationCodeTokenRequest.redirectUri = redirectUri;
6967
return this;
7068
}
7169

7270
public AuthorizationCodeTokenRequestAttributes build() {
71+
Assert.hasText(this.authorizationCodeTokenRequest.clientId, "clientId cannot be empty");
72+
Assert.hasText(this.authorizationCodeTokenRequest.redirectUri, "redirectUri cannot be empty");
7373
return this.authorizationCodeTokenRequest;
7474
}
7575
}

oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/AuthorizationRequestAttributes.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,16 @@ private Builder(AuthorizationGrantType authorizationGrantType) {
9292
}
9393

9494
public Builder authorizeUri(String authorizeUri) {
95-
Assert.hasText(authorizeUri, "authorizeUri cannot be empty");
9695
this.authorizationRequest.authorizeUri = authorizeUri;
9796
return this;
9897
}
9998

10099
public Builder clientId(String clientId) {
101-
Assert.hasText(clientId, "clientId cannot be empty");
102100
this.authorizationRequest.clientId = clientId;
103101
return this;
104102
}
105103

106104
public Builder redirectUri(String redirectUri) {
107-
Assert.hasText(redirectUri, "redirectUri cannot be empty");
108105
this.authorizationRequest.redirectUri = redirectUri;
109106
return this;
110107
}
@@ -121,6 +118,8 @@ public Builder state(String state) {
121118
}
122119

123120
public AuthorizationRequestAttributes build() {
121+
Assert.hasText(this.authorizationRequest.clientId, "clientId cannot be empty");
122+
Assert.hasText(this.authorizationRequest.authorizeUri, "authorizeUri cannot be empty");
124123
return this.authorizationRequest;
125124
}
126125
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
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+
package org.springframework.security.oauth2.core.endpoint;
17+
18+
import org.junit.Test;
19+
20+
/**
21+
* Tests {@link AuthorizationCodeAuthorizationResponseAttributes}
22+
*
23+
* @author Luander Ribeiro
24+
*/
25+
public class AuthorizationCodeAuthorizationResponseAttributesTest {
26+
27+
@Test(expected = IllegalArgumentException.class)
28+
public void constructorWhenCodeIsNullThenThrowIllegalArgumentException() {
29+
new AuthorizationCodeAuthorizationResponseAttributes(null, "xyz");
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
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+
package org.springframework.security.oauth2.core.endpoint;
17+
18+
import org.junit.Test;
19+
import org.springframework.security.oauth2.core.AuthorizationGrantType;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
/**
24+
* Tests {@link AuthorizationCodeTokenRequestAttributes}
25+
*
26+
* @author Luander Ribeiro
27+
*/
28+
public class AuthorizationCodeTokenRequestAttributesTest {
29+
private static final String CODE = "code";
30+
private static final String CLIENT_ID = "client id";
31+
private static final String REDIRECT_URI = "http://redirect.uri/";
32+
33+
@Test(expected = IllegalArgumentException.class)
34+
public void buildWhenCodeIsNullThenThrowIllegalArgumentException() {
35+
AuthorizationCodeTokenRequestAttributes
36+
.withCode(null)
37+
.clientId(CLIENT_ID)
38+
.redirectUri(REDIRECT_URI)
39+
.build();
40+
}
41+
42+
@Test(expected = IllegalArgumentException.class)
43+
public void buildWhenClientIdIsNullThenThrowIllegalArgumentException() {
44+
AuthorizationCodeTokenRequestAttributes
45+
.withCode(CODE)
46+
.clientId(null)
47+
.redirectUri(REDIRECT_URI)
48+
.build();
49+
}
50+
51+
@Test(expected = IllegalArgumentException.class)
52+
public void buildWhenRedirectUriIsNullThenThrowIllegalArgumentException() {
53+
AuthorizationCodeTokenRequestAttributes
54+
.withCode(CODE)
55+
.clientId(CLIENT_ID)
56+
.redirectUri(null)
57+
.build();
58+
}
59+
60+
@Test(expected = IllegalArgumentException.class)
61+
public void buildWhenClientIdNotSetThenThrowIllegalArgumentException() {
62+
AuthorizationCodeTokenRequestAttributes
63+
.withCode(CODE)
64+
.redirectUri(REDIRECT_URI)
65+
.build();
66+
}
67+
68+
@Test(expected = IllegalArgumentException.class)
69+
public void buildWhenRedirectUriNotSetThenThrowIllegalArgumentException() {
70+
AuthorizationCodeTokenRequestAttributes
71+
.withCode(CODE)
72+
.clientId(CLIENT_ID)
73+
.build();
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
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+
package org.springframework.security.oauth2.core.endpoint;
17+
18+
import org.junit.Test;
19+
20+
import java.util.Collections;
21+
import java.util.Set;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
import static org.assertj.core.api.Assertions.assertThatCode;
25+
26+
/**
27+
* Tests {@link AuthorizationRequestAttributes}
28+
*
29+
* @author Luander Ribeiro
30+
*/
31+
public class AuthorizationRequestAttributesTest {
32+
private static final String AUTHORIZE_URI = "http://authorize.uri/";
33+
private static final String CLIENT_ID = "client id";
34+
private static final String REDIRECT_URI = "http://redirect.uri/";
35+
private static final Set<String> SCOPES = Collections.singleton("scope");
36+
private static final String STATE = "xyz";
37+
38+
@Test(expected = IllegalArgumentException.class)
39+
public void buildWhenAuthorizationUriIsNullThenThrowIllegalArgumentException() {
40+
AuthorizationRequestAttributes.withAuthorizationCode()
41+
.authorizeUri(null)
42+
.clientId(CLIENT_ID)
43+
.redirectUri(REDIRECT_URI)
44+
.scopes(SCOPES)
45+
.state(STATE)
46+
.build();
47+
}
48+
49+
@Test(expected = IllegalArgumentException.class)
50+
public void buildWhenAuthorizeUriNotSetThenThrowIllegalArgumentException() {
51+
AuthorizationRequestAttributes.withAuthorizationCode()
52+
.clientId(CLIENT_ID)
53+
.redirectUri(REDIRECT_URI)
54+
.scopes(SCOPES)
55+
.state(STATE)
56+
.build();
57+
}
58+
59+
@Test(expected = IllegalArgumentException.class)
60+
public void buildWhenClientIdIsNullThenThrowIllegalArgumentException() {
61+
AuthorizationRequestAttributes.withAuthorizationCode()
62+
.authorizeUri(AUTHORIZE_URI)
63+
.clientId(null)
64+
.redirectUri(REDIRECT_URI)
65+
.scopes(SCOPES)
66+
.state(STATE)
67+
.build();
68+
}
69+
70+
@Test(expected = IllegalArgumentException.class)
71+
public void buildWhenClientIdNotSetThenThrowIllegalArgumentException() {
72+
AuthorizationRequestAttributes.withAuthorizationCode()
73+
.authorizeUri(AUTHORIZE_URI)
74+
.redirectUri(REDIRECT_URI)
75+
.scopes(SCOPES)
76+
.state(STATE)
77+
.build();
78+
}
79+
80+
@Test
81+
public void buildWhenGetResponseTypeIsCalledThenReturnCode() {
82+
AuthorizationRequestAttributes attributes;
83+
attributes = AuthorizationRequestAttributes.withAuthorizationCode()
84+
.authorizeUri(AUTHORIZE_URI)
85+
.clientId(CLIENT_ID)
86+
.redirectUri(REDIRECT_URI)
87+
.scopes(SCOPES)
88+
.state(STATE)
89+
.build();
90+
91+
assertThat(attributes.getResponseType()).isEqualTo(ResponseType.CODE);
92+
}
93+
94+
@Test
95+
public void buildWhenRedirectUriIsNullThenDoesNotThrowAnyException() {
96+
assertThatCode(() -> AuthorizationRequestAttributes.withAuthorizationCode()
97+
.authorizeUri(AUTHORIZE_URI)
98+
.clientId(CLIENT_ID)
99+
.redirectUri(null)
100+
.scopes(SCOPES)
101+
.state(STATE)
102+
.build()).doesNotThrowAnyException();
103+
}
104+
105+
@Test
106+
public void buildWhenRedirectUriNotSetThenDoesNotThrowAnyException() {
107+
assertThatCode(() -> AuthorizationRequestAttributes.withAuthorizationCode()
108+
.authorizeUri(AUTHORIZE_URI)
109+
.clientId(CLIENT_ID)
110+
.scopes(SCOPES)
111+
.state(STATE)
112+
.build()).doesNotThrowAnyException();
113+
}
114+
115+
@Test
116+
public void buildWhenScopesIsNullThenDoesNotThrowAnyException() {
117+
assertThatCode(() -> AuthorizationRequestAttributes.withAuthorizationCode()
118+
.authorizeUri(AUTHORIZE_URI)
119+
.clientId(CLIENT_ID)
120+
.redirectUri(REDIRECT_URI)
121+
.scopes(null)
122+
.state(STATE)
123+
.build()).doesNotThrowAnyException();
124+
}
125+
126+
@Test
127+
public void buildWhenScopesNotSetThenDoesNotThrowAnyException() {
128+
assertThatCode(() -> AuthorizationRequestAttributes.withAuthorizationCode()
129+
.authorizeUri(AUTHORIZE_URI)
130+
.clientId(CLIENT_ID)
131+
.redirectUri(REDIRECT_URI)
132+
.state(STATE)
133+
.build()).doesNotThrowAnyException();
134+
}
135+
136+
@Test
137+
public void buildWhenStateIsNullThenDoesNotThrowAnyException() {
138+
assertThatCode(() -> AuthorizationRequestAttributes.withAuthorizationCode()
139+
.authorizeUri(AUTHORIZE_URI)
140+
.clientId(CLIENT_ID)
141+
.redirectUri(REDIRECT_URI)
142+
.scopes(SCOPES)
143+
.state(null)
144+
.build()).doesNotThrowAnyException();
145+
}
146+
147+
@Test
148+
public void buildWhenStateNotSetThenDoesNotThrowAnyException() {
149+
assertThatCode(() -> AuthorizationRequestAttributes.withAuthorizationCode()
150+
.authorizeUri(AUTHORIZE_URI)
151+
.clientId(CLIENT_ID)
152+
.redirectUri(REDIRECT_URI)
153+
.scopes(SCOPES)
154+
.build()).doesNotThrowAnyException();
155+
}
156+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
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+
package org.springframework.security.oauth2.core.endpoint;
17+
18+
import org.junit.Test;
19+
20+
/**
21+
* Tests {@link ErrorResponseAttributes}
22+
*
23+
* @author Luander Ribeiro
24+
*/
25+
public class ErrorResponseAttributesTest {
26+
27+
@Test(expected = IllegalArgumentException.class)
28+
public void withErrorCodeWhenCodeIsNullThenThrowIllegalArgumentException() {
29+
ErrorResponseAttributes.withErrorCode(null)
30+
.build();
31+
}
32+
}

0 commit comments

Comments
 (0)