151 lines
4.0 KiB
OpenEdge ABL
151 lines
4.0 KiB
OpenEdge ABL
@IsTest
|
|
public with sharing class MicrosoftAuthTest {
|
|
/*
|
|
* Mock response for token endpoint
|
|
*/
|
|
private class TokenMock implements HttpCalloutMock {
|
|
public HTTPResponse respond(HTTPRequest req) {
|
|
|
|
System.debug('Mock Endpoint >>> ' + req.getEndpoint());
|
|
System.debug('Mock Body >>> ' + req.getBody());
|
|
|
|
HttpResponse res = new HttpResponse();
|
|
res.setStatusCode(200);
|
|
res.setHeader('Content-Type', 'application/json');
|
|
|
|
// Fake OAuth response
|
|
res.setBody('{' +
|
|
'"access_token":"TEST_ACCESS_TOKEN",' +
|
|
'"refresh_token":"TEST_REFRESH_TOKEN"' +
|
|
'}');
|
|
return res;
|
|
}
|
|
}
|
|
|
|
@IsTest
|
|
static void testGetAccessToken_Success() {
|
|
|
|
System.debug('===== testGetAccessToken_Success START =====');
|
|
|
|
// Set mock
|
|
Test.setMock(HttpCalloutMock.class, new TokenMock());
|
|
|
|
Test.startTest();
|
|
|
|
// Call method
|
|
MicrosoftAuth obj = new MicrosoftAuth();
|
|
obj.getAccessToken(
|
|
'dummyAuthCode',
|
|
'OneDrive',
|
|
true,
|
|
UserInfo.getUserId()
|
|
);
|
|
|
|
Test.stopTest();
|
|
|
|
System.debug('===== testGetAccessToken_Success END =====');
|
|
|
|
// OPTIONAL: If OAuthTokenManager.storeToken inserts record,
|
|
// you can verify that record here with SOQL.
|
|
}
|
|
|
|
|
|
/*
|
|
* Mock for SUCCESS response (Status 200)
|
|
*/
|
|
private class RefreshTokenSuccessMock implements HttpCalloutMock {
|
|
public HTTPResponse respond(HTTPRequest req) {
|
|
|
|
System.debug('Mock Endpoint >>> ' + req.getEndpoint());
|
|
System.debug('Mock Body >>> ' + req.getBody());
|
|
|
|
HttpResponse res = new HttpResponse();
|
|
res.setStatusCode(200);
|
|
res.setHeader('Content-Type', 'application/json');
|
|
|
|
res.setBody('{' +
|
|
'"access_token":"NEW_ACCESS_TOKEN_123"' +
|
|
'}');
|
|
|
|
return res;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Mock for FAILURE response (Status 400)
|
|
*/
|
|
private class RefreshTokenFailMock implements HttpCalloutMock {
|
|
public HTTPResponse respond(HTTPRequest req) {
|
|
|
|
HttpResponse res = new HttpResponse();
|
|
res.setStatusCode(400);
|
|
res.setBody('Invalid refresh token');
|
|
|
|
return res;
|
|
}
|
|
}
|
|
|
|
|
|
@IsTest
|
|
static void testRefreshAccessToken_Success() {
|
|
|
|
System.debug('===== testRefreshAccessToken_Success START =====');
|
|
|
|
Test.setMock(HttpCalloutMock.class, new RefreshTokenSuccessMock());
|
|
|
|
Test.startTest();
|
|
|
|
String token = new MicrosoftAuth ()
|
|
.refreshAccessToken('DUMMY_REFRESH_TOKEN');
|
|
|
|
Test.stopTest();
|
|
|
|
System.debug('Returned Token >>> ' + token);
|
|
System.assertEquals('NEW_ACCESS_TOKEN_123', token,
|
|
'Token should match mock response');
|
|
|
|
System.debug('===== testRefreshAccessToken_Success END =====');
|
|
}
|
|
|
|
|
|
@IsTest
|
|
static void testRefreshAccessToken_Failure() {
|
|
|
|
System.debug('===== testRefreshAccessToken_Failure START =====');
|
|
|
|
Test.setMock(HttpCalloutMock.class, new RefreshTokenFailMock());
|
|
|
|
Test.startTest();
|
|
|
|
String token = new MicrosoftAuth ()
|
|
.refreshAccessToken('BAD_REFRESH_TOKEN');
|
|
|
|
Test.stopTest();
|
|
|
|
System.debug('Returned Token >>> ' + token);
|
|
|
|
System.assertEquals(null, token,
|
|
'Token should be null when status code is not 200');
|
|
|
|
System.debug('===== testRefreshAccessToken_Failure END =====');
|
|
}
|
|
|
|
@IsTest
|
|
static void testRevokeAccessToken() {
|
|
|
|
System.debug('===== testRevokeAccessToken START =====');
|
|
|
|
Test.startTest();
|
|
|
|
// Call method
|
|
MicrosoftAuth.revokeAccessToken('DUMMY_ACCESS_TOKEN');
|
|
|
|
Test.stopTest();
|
|
|
|
// Since method returns void and has no logic,
|
|
// we only verify that no exception was thrown
|
|
System.assert(true, 'Method executed successfully');
|
|
|
|
System.debug('===== testRevokeAccessToken END =====');
|
|
}
|
|
} |