提交 65c28db2 编写于 作者: 智布道's avatar 智布道 👁

Adding tests.

上级 87e5a64d
/*
* Copyright (c) 2020-2040, 北京符节科技有限公司 (support@fujieid.com & https://www.fujieid.com).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.fujieid.jap.oauth2;
import com.fujieid.jap.core.AuthenticateConfig;
......@@ -20,6 +35,13 @@ import java.util.Map;
import static org.mockito.Mockito.when;
/**
* unit test
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @since 1.0.0
*/
public class Oauth2StrategyTest {
@Mock
......
/*
* Copyright (c) 2020-2040, 北京符节科技有限公司 (support@fujieid.com & https://www.fujieid.com).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.fujieid.jap.oauth2;
import com.fujieid.jap.core.cache.JapCache;
......@@ -14,6 +29,13 @@ import org.mockito.MockitoAnnotations;
import javax.servlet.http.HttpServletRequest;
/**
* unit test
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @since 1.0.0
*/
public class Oauth2UtilTest {
@Mock
......@@ -115,6 +137,12 @@ public class Oauth2UtilTest {
Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkOauthCallbackRequest(requestErrorParam, requestErrorDescParam, bizErrorMsg));
}
@Test
public void checkStateNullStateAndNullClientId() {
boolean verifyState = true;
Assert.assertThrows(JapOauth2Exception.class, () -> Oauth2Util.checkState(null, null, verifyState));
}
@Test
public void checkStateNoVerify() {
String state = "xxx";
......
/*
* Copyright (c) 2020-2040, 北京符节科技有限公司 (support@fujieid.com & https://www.fujieid.com).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.fujieid.jap.oauth2.pkce;
import com.fujieid.jap.core.cache.JapCache;
import com.fujieid.jap.core.cache.JapCacheContextHolder;
import com.fujieid.jap.core.cache.JapLocalCache;
import com.fujieid.jap.oauth2.OAuthConfig;
import org.junit.Assert;
import org.junit.Test;
import java.util.Map;
/**
* unit test
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @since 1.0.0
*/
public class PkceHelperTest {
@Test
public void generatePkceParametersNullCodeChallengeMethod() {
Map<String, Object> pkceInfo = PkceHelper.generatePkceParameters(new OAuthConfig()
.setClientId("clientId")
.setCodeVerifierTimeout(1000));
Assert.assertEquals(2, pkceInfo.size());
Assert.assertNotNull(pkceInfo.get(PkceParams.CODE_CHALLENGE));
Assert.assertNotNull(pkceInfo.get(PkceParams.CODE_CHALLENGE_METHOD));
Assert.assertEquals(PkceCodeChallengeMethod.S256, pkceInfo.get(PkceParams.CODE_CHALLENGE_METHOD));
}
@Test
public void generatePkceParametersPlainCodeChallengeMethod() {
Map<String, Object> pkceInfo = PkceHelper.generatePkceParameters(new OAuthConfig()
.setCodeChallengeMethod(PkceCodeChallengeMethod.PLAIN)
.setClientId("clientId")
.setCodeVerifierTimeout(1000));
Assert.assertEquals(2, pkceInfo.size());
Assert.assertNotNull(pkceInfo.get(PkceParams.CODE_CHALLENGE));
Assert.assertNotNull(pkceInfo.get(PkceParams.CODE_CHALLENGE_METHOD));
Assert.assertEquals(PkceCodeChallengeMethod.PLAIN, pkceInfo.get(PkceParams.CODE_CHALLENGE_METHOD));
}
@Test
public void generatePkceParametersS256CodeChallengeMethod() {
Map<String, Object> pkceInfo = PkceHelper.generatePkceParameters(new OAuthConfig()
.setCodeChallengeMethod(PkceCodeChallengeMethod.S256)
.setClientId("clientId")
.setCodeVerifierTimeout(1000));
Assert.assertEquals(2, pkceInfo.size());
Assert.assertNotNull(pkceInfo.get(PkceParams.CODE_CHALLENGE));
Assert.assertNotNull(pkceInfo.get(PkceParams.CODE_CHALLENGE_METHOD));
Assert.assertEquals(PkceCodeChallengeMethod.S256, pkceInfo.get(PkceParams.CODE_CHALLENGE_METHOD));
}
@Test
public void getCacheCodeVerifierNullClientId() {
String res = PkceHelper.getCacheCodeVerifier(null);
Assert.assertNull(res);
}
@Test
public void getCacheCodeVerifier() {
JapCache japCache = new JapLocalCache();
JapCacheContextHolder.enable(japCache);
JapCacheContextHolder.getCache().set("clientId", "111", 111111);
String res = PkceHelper.getCacheCodeVerifier("clientId");
Assert.assertNotNull(res);
Assert.assertEquals("111", res);
}
}
/*
* Copyright (c) 2020-2040, 北京符节科技有限公司 (support@fujieid.com & https://www.fujieid.com).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.fujieid.jap.oauth2.token;
import cn.hutool.core.io.IORuntimeException;
import com.fujieid.jap.core.exception.JapOauth2Exception;
import com.fujieid.jap.oauth2.OAuthConfig;
import com.fujieid.jap.oauth2.Oauth2GrantType;
import com.fujieid.jap.oauth2.Oauth2ResponseType;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import javax.servlet.http.HttpServletRequest;
/**
* unit test
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @since 1.0.0
*/
public class AccessTokenHelperTest {
@Mock
private HttpServletRequest httpServletRequestMock;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
@Test
public void getTokenNullOAuthConfig() {
Assert.assertThrows(JapOauth2Exception.class, () -> AccessTokenHelper.getToken(httpServletRequestMock, null));
}
@Test
public void getTokenEmptyOAuthConfig() {
Assert.assertThrows(JapOauth2Exception.class, () -> AccessTokenHelper.getToken(httpServletRequestMock, new OAuthConfig()));
}
@Test
public void getTokenCodeResponseType() {
Assert.assertThrows(JapOauth2Exception.class, () -> AccessTokenHelper.getToken(httpServletRequestMock, new OAuthConfig()
.setResponseType(Oauth2ResponseType.code)));
}
@Test
public void getTokenCodeResponseTypeDoNotCheckState() {
// Http url must be not blank!
Assert.assertThrows(IllegalArgumentException.class, () -> AccessTokenHelper.getToken(httpServletRequestMock, new OAuthConfig()
.setVerifyState(false)
.setResponseType(Oauth2ResponseType.code)));
}
@Test
public void getTokenCodeResponseTypeErrorTokenUrl() {
// UnknownHostException: setTokenUrl
Assert.assertThrows(IORuntimeException.class, () -> AccessTokenHelper.getToken(httpServletRequestMock, new OAuthConfig()
.setVerifyState(false)
.setResponseType(Oauth2ResponseType.code)
.setEnablePkce(true)
.setCallbackUrl("setCallbackUrl")
.setTokenUrl("setTokenUrl")));
}
@Test
public void getTokenTokenResponseType() {
// Oauth2Strategy failed to get AccessToken.
Assert.assertThrows(JapOauth2Exception.class, () -> AccessTokenHelper.getToken(httpServletRequestMock, new OAuthConfig()
.setResponseType(Oauth2ResponseType.token)));
}
@Test
public void getTokenPasswordGrantTypeNullTokenUrl() {
// Http url must be not blank!
Assert.assertThrows(IllegalArgumentException.class, () -> AccessTokenHelper.getToken(httpServletRequestMock, new OAuthConfig()
.setGrantType(Oauth2GrantType.password)
.setScopes(new String[]{"read"})));
}
@Test
public void getTokenPasswordGrantTypeErrorTokenUrl() {
// UnknownHostException: setTokenUrl
Assert.assertThrows(IORuntimeException.class, () -> AccessTokenHelper.getToken(httpServletRequestMock, new OAuthConfig()
.setGrantType(Oauth2GrantType.password)
.setScopes(new String[]{"read"})
.setTokenUrl("setTokenUrl")));
}
@Test
public void getTokenClientCredentialsGrantTypeErrorTokenUrl() {
// UnknownHostException: setTokenUrl
Assert.assertThrows(IORuntimeException.class, () -> AccessTokenHelper.getToken(httpServletRequestMock, new OAuthConfig()
.setGrantType(Oauth2GrantType.client_credentials)
.setScopes(new String[]{"read"})
.setTokenUrl("setTokenUrl")));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册