/
ApiKeyCredentialsMatcherTest.java
92 lines (76 loc) · 4.24 KB
/
ApiKeyCredentialsMatcherTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*******************************************************************************
* Copyright (c) 2021, 2022 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.service.authentication.shiro.realm;
import org.eclipse.kapua.qa.markers.junit.JUnitTests;
import org.eclipse.kapua.service.authentication.credential.Credential;
import org.eclipse.kapua.service.authentication.credential.CredentialType;
import org.eclipse.kapua.service.authentication.shiro.JwtCredentialsImpl;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.mockito.Mockito;
@Category(JUnitTests.class)
public class ApiKeyCredentialsMatcherTest {
ApiKeyCredentialsMatcher apiKeyCredentialsMatcher;
JwtCredentialsImpl authenticationToken;
LoginAuthenticationInfo authenticationInfo;
Credential credential;
@Before
public void initialize() {
apiKeyCredentialsMatcher = new ApiKeyCredentialsMatcher();
authenticationToken = Mockito.mock(JwtCredentialsImpl.class);
authenticationInfo = Mockito.mock(LoginAuthenticationInfo.class);
credential = Mockito.mock(Credential.class);
}
@Test(expected = NullPointerException.class)
public void doCredentialsMatchNullAuthenticationTokenTest() {
apiKeyCredentialsMatcher.doCredentialsMatch(null, authenticationInfo);
}
@Test(expected = NullPointerException.class)
public void doCredentialsMatchNullAuthenticationInfoTest() {
Mockito.when(authenticationToken.getCredentials()).thenReturn("tokenApiFullKey");
apiKeyCredentialsMatcher.doCredentialsMatch(authenticationToken, null);
}
@Test
public void doCredentialsMatchDifferentCredentialTypesTest() {
Mockito.when(authenticationToken.getCredentials()).thenReturn("tokenApiFullKey");
Mockito.when(authenticationInfo.getCredentials()).thenReturn(credential);
Mockito.when(credential.getCredentialType()).thenReturn(CredentialType.PASSWORD);
Assert.assertFalse("False expected.", apiKeyCredentialsMatcher.doCredentialsMatch(authenticationToken, authenticationInfo));
}
@Test
public void doCredentialsMatchDifferentTokenAndInfoPreTest() {
Mockito.when(authenticationToken.getCredentials()).thenReturn("tokenApiFullKey");
Mockito.when(authenticationInfo.getCredentials()).thenReturn(credential);
Mockito.when(credential.getCredentialType()).thenReturn(CredentialType.API_KEY);
Mockito.when(credential.getCredentialKey()).thenReturn("FullApiK:Credential");
Assert.assertFalse("False expected.", apiKeyCredentialsMatcher.doCredentialsMatch(authenticationToken, authenticationInfo));
}
@Test
public void doCredentialsMatchEqualTokenAndInfoPreFalseCheckPwTest() {
Mockito.when(authenticationToken.getCredentials()).thenReturn("FullApiKCredential");
Mockito.when(authenticationInfo.getCredentials()).thenReturn(credential);
Mockito.when(credential.getCredentialType()).thenReturn(CredentialType.API_KEY);
Mockito.when(credential.getCredentialKey()).thenReturn("FullApiK:$2a$12$2AZYOAvilJyNvG8b6rBDaOSIcM3mKc6iyNQUYIXOF4ZFEAYdzM7Jm");
Assert.assertFalse("False expected.", apiKeyCredentialsMatcher.doCredentialsMatch(authenticationToken, authenticationInfo));
}
@Test
public void doCredentialsMatchTest() {
Mockito.when(authenticationToken.getCredentials()).thenReturn("FullApiKplainValue");
Mockito.when(authenticationInfo.getCredentials()).thenReturn(credential);
Mockito.when(credential.getCredentialType()).thenReturn(CredentialType.API_KEY);
Mockito.when(credential.getCredentialKey()).thenReturn("FullApiK:$2a$12$2AZYOAvilJyNvG8b6rBDaOSIcM3mKc6iyNQUYIXOF4ZFEAYdzM7Jm");
Assert.assertTrue("True expected.", apiKeyCredentialsMatcher.doCredentialsMatch(authenticationToken, authenticationInfo));
}
}