15
15
import com .getcapacitor .JSArray ;
16
16
import com .getcapacitor .JSObject ;
17
17
import com .getcapacitor .PluginCall ;
18
- import com .microsoft .identity .client .IAccount ;
19
- import com .microsoft .identity .client .IAuthenticationResult ;
20
- import com .microsoft .identity .client .ISingleAccountPublicClientApplication ;
18
+ import com .microsoft .identity .client .*;
21
19
import com .microsoft .identity .client .exception .MsalException ;
22
20
import java .io .File ;
23
21
import java .util .List ;
@@ -59,38 +57,38 @@ class MsAuthPluginTest {
59
57
void setUp () throws MsalException , InterruptedException {
60
58
reset (mockedContext , mockedActivity , publicClientApplicationFactoryMock , singleAccountPublicClientApplication );
61
59
62
- when (publicClientApplicationFactoryMock .createSingleAccountPublicClientApplication (any (Context .class ), any (File .class )))
63
- .thenReturn (singleAccountPublicClientApplication );
60
+ when (publicClientApplicationFactoryMock .createSingleAccountPublicClientApplication (any (Context .class ), any (File .class ))).thenReturn (
61
+ singleAccountPublicClientApplication
62
+ );
64
63
65
- plugin =
66
- new MsAuthPlugin (publicClientApplicationFactoryMock ) {
67
- final Context applicationContext = mock (Context .class );
64
+ plugin = new MsAuthPlugin (publicClientApplicationFactoryMock ) {
65
+ final Context applicationContext = mock (Context .class );
68
66
69
- @ Override
70
- public Context getContext () {
71
- when (mockedContext .getApplicationContext ()).thenReturn (applicationContext );
67
+ @ Override
68
+ public Context getContext () {
69
+ when (mockedContext .getApplicationContext ()).thenReturn (applicationContext );
72
70
73
- return mockedContext ;
74
- }
71
+ return mockedContext ;
72
+ }
75
73
76
- @ Override
77
- public AppCompatActivity getActivity () {
78
- lenient ().when (applicationContext .getPackageName ()).thenReturn ("nl.recognize.project-x" );
79
- lenient ().when (mockedActivity .getApplicationContext ()).thenReturn (applicationContext );
74
+ @ Override
75
+ public AppCompatActivity getActivity () {
76
+ lenient ().when (applicationContext .getPackageName ()).thenReturn ("nl.recognize.project-x" );
77
+ lenient ().when (mockedActivity .getApplicationContext ()).thenReturn (applicationContext );
80
78
81
- return mockedActivity ;
82
- }
79
+ return mockedActivity ;
80
+ }
83
81
84
- @ Override
85
- protected String getLogTag () {
86
- return "LogTag" ;
87
- }
82
+ @ Override
83
+ protected String getLogTag () {
84
+ return "LogTag" ;
85
+ }
88
86
89
- @ Override
90
- protected String getAuthorityUrl (ISingleAccountPublicClientApplication context ) {
91
- return AUTHORITY_URL ;
92
- }
93
- };
87
+ @ Override
88
+ protected String getAuthorityUrl (ISingleAccountPublicClientApplication context ) {
89
+ return AUTHORITY_URL ;
90
+ }
91
+ };
94
92
}
95
93
96
94
@ Test
@@ -103,14 +101,16 @@ void loginExpectAcquireTokenSilent() throws JSONException, MsalException, Interr
103
101
ID_TOKEN ,
104
102
new String [] { "mocked-scope" , "openid" , "profile" }
105
103
);
104
+ ICurrentAccountResult currentAccountResult = new CurrentAccountResult (result .getAccount (), null , true );
105
+ when (singleAccountPublicClientApplication .getCurrentAccount ()).thenReturn (currentAccountResult );
106
+
106
107
when (
107
108
singleAccountPublicClientApplication .acquireTokenSilent (
108
109
argThat (
109
110
parameters -> parameters .getScopes ().equals (List .of ("mocked-scope" )) && parameters .getAuthority ().equals (AUTHORITY_URL )
110
111
)
111
112
)
112
- )
113
- .thenReturn (result );
113
+ ).thenReturn (result );
114
114
115
115
ArgumentCaptor <JSObject > jsObjectCaptor = ArgumentCaptor .forClass (JSObject .class );
116
116
doNothing ().when (pluginCallMock ).resolve (jsObjectCaptor .capture ());
@@ -123,31 +123,31 @@ void loginExpectAcquireTokenSilent() throws JSONException, MsalException, Interr
123
123
assertEquals ("access-token" , resolve .getString ("accessToken" ));
124
124
assertEquals (ID_TOKEN , resolve .getString ("idToken" ));
125
125
126
- verify (singleAccountPublicClientApplication )
127
- .acquireTokenSilent (argThat (parameters -> parameters .getAuthority ().equals (AUTHORITY_URL )));
126
+ verify (singleAccountPublicClientApplication ).acquireTokenSilent (
127
+ argThat (parameters -> parameters .getAuthority ().equals (AUTHORITY_URL ))
128
+ );
128
129
}
129
130
130
131
private void initializePluginCallMockWithDefaults (PluginCall pluginCallMock ) throws JSONException {
131
132
when (pluginCallMock .getArray ("scopes" )).thenReturn (new JSArray (new String [] { "mocked-scope" }));
132
- when (pluginCallMock .getString (any ()))
133
- .thenAnswer (
134
- (Answer <String >) invocation -> {
135
- switch (invocation .getArgument (0 ).toString ()) {
136
- case "clientId" :
137
- return CLIENT_ID ;
138
- case "domainHint" :
139
- return DOMAIN_HINT ;
140
- case "tenant" :
141
- return TENANT ;
142
- case "keyHash" :
143
- return KEY_HASH ;
144
- case "authorityUrl" :
145
- return AUTHORITY_URL ;
146
- }
147
-
148
- return null ;
133
+ when (pluginCallMock .getString (any ())).thenAnswer (
134
+ (Answer <String >) invocation -> {
135
+ switch (invocation .getArgument (0 ).toString ()) {
136
+ case "clientId" :
137
+ return CLIENT_ID ;
138
+ case "domainHint" :
139
+ return DOMAIN_HINT ;
140
+ case "tenant" :
141
+ return TENANT ;
142
+ case "keyHash" :
143
+ return KEY_HASH ;
144
+ case "authorityUrl" :
145
+ return AUTHORITY_URL ;
149
146
}
150
- );
147
+
148
+ return null ;
149
+ }
150
+ );
151
151
when (pluginCallMock .getString ("authorityType" , AuthorityType .AAD .name ())).thenReturn ("AAD" );
152
152
}
153
153
0 commit comments