Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
符节科技
Jap
提交
65c28db2
Jap
项目概览
符节科技
/
Jap
大约 1 年 前同步成功
通知
91
Star
3
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Jap
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
65c28db2
编写于
2月 18, 2021
作者:
智布道
👁
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
✅
Adding tests.
上级
87e5a64d
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
251 addition
and
0 deletion
+251
-0
jap-oauth2/src/test/java/com/fujieid/jap/oauth2/Oauth2StrategyTest.java
.../test/java/com/fujieid/jap/oauth2/Oauth2StrategyTest.java
+22
-0
jap-oauth2/src/test/java/com/fujieid/jap/oauth2/Oauth2UtilTest.java
.../src/test/java/com/fujieid/jap/oauth2/Oauth2UtilTest.java
+28
-0
jap-oauth2/src/test/java/com/fujieid/jap/oauth2/pkce/PkceHelperTest.java
...test/java/com/fujieid/jap/oauth2/pkce/PkceHelperTest.java
+86
-0
jap-oauth2/src/test/java/com/fujieid/jap/oauth2/token/AccessTokenHelperTest.java
...a/com/fujieid/jap/oauth2/token/AccessTokenHelperTest.java
+115
-0
未找到文件。
jap-oauth2/src/test/java/com/fujieid/jap/oauth2/Oauth2StrategyTest.java
浏览文件 @
65c28db2
/*
* 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
...
...
jap-oauth2/src/test/java/com/fujieid/jap/oauth2/Oauth2UtilTest.java
浏览文件 @
65c28db2
/*
* 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"
;
...
...
jap-oauth2/src/test/java/com/fujieid/jap/oauth2/pkce/PkceHelperTest.java
0 → 100644
浏览文件 @
65c28db2
/*
* 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
);
}
}
jap-oauth2/src/test/java/com/fujieid/jap/oauth2/token/AccessTokenHelperTest.java
0 → 100644
浏览文件 @
65c28db2
/*
* 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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录