Backup_FME_personal/force-app/main/default/classes/GoogleDriveAuthTest.cls

94 lines
3.9 KiB
OpenEdge ABL

@IsTest
private class GoogleDriveAuthTest {
private static StorageProviderConfigUtil.Config getMockConfig() {
StorageProviderConfigUtil.Config cfg = new StorageProviderConfigUtil.Config();
cfg.clientId = 'mockClientId';
cfg.clientSecret = 'mockSecret';
cfg.authEndpoint = 'https://accounts.google.com/o/oauth2/auth';
cfg.tokenEndpoint = 'https://www.googleapis.com/oauth2/v4/token';
cfg.redirectUri = 'https://example.com/oauth/callback';
cfg.scope = 'https://www.googleapis.com/auth/drive';
return cfg;
}
private static HttpResponse mockTokenResponse(Integer statusCode, String body) {
HttpResponse res = new HttpResponse();
res.setStatusCode(statusCode);
res.setBody(body);
return res;
}
// @IsTest
// static void testInitiateAuth() {
// User u = [SELECT Id FROM User WHERE IsActive = TRUE LIMIT 1];
// System.runAs(u) {
// Storage_Provider_Config__mdt mdt = [
// SELECT DeveloperName, Provider_Type__c, Auth_Endpoint__c, Token_Endpoint__c,
// Scope__c, Redirect_Uri__c, Client_Id__c, Client_Secret__c
// FROM Storage_Provider_Config__mdt
// WHERE DeveloperName = 'Google_Drive'
// LIMIT 1
// ];
// Test.startTest();
// GoogleDriveAuth auth = new GoogleDriveAuth();
// String url = auth.initiateAuth('Google Drive');
// Test.stopTest();
// System.assert(url.contains('client_id=' + mdt.Client_Id__c));
// //System.assert(url.contains('redirect_uri=' + EncodingUtil.urlEncode(mdt.Redirect_Uri__c, 'UTF-8')));
// }
// }
// @IsTest
// static void testGetAccessTokenSuccess() {
// User u = [SELECT Id FROM User WHERE IsActive = TRUE LIMIT 1];
// System.runAs(u) {
// Test.startTest();
// Test.setMock(HttpCalloutMock.class, new GoogleDriveHttpCalloutMock());
// GoogleDriveAuth auth = new GoogleDriveAuth();
// auth.getAccessToken('dummyCode', 'Google Drive', false, UserInfo.getUserId());
// Test.stopTest();
// }
// }
@IsTest
static void testRefreshAccessTokenSuccess() {
User u = [SELECT Id FROM User WHERE IsActive = TRUE LIMIT 1];
System.runAs(u) {
Test.startTest();
Test.setMock(HttpCalloutMock.class, new GoogleDriveHttpCalloutMock());
GoogleDriveAuth auth = new GoogleDriveAuth();
String token = auth.refreshAccessToken('dummyRefreshToken');
Test.stopTest();
System.assertEquals('mockRefreshedToken', token, 'Should return refreshed token');
}
}
@IsTest
static void testOrgSandBoxOrProduction() {
User u = [SELECT Id FROM User WHERE IsActive = TRUE LIMIT 1];
System.runAs(u) {
Test.startTest();
Boolean isProd = GoogleDriveAuth.orgSandBoxOrProduction();
Test.stopTest();
System.assert(isProd == ![SELECT IsSandbox FROM Organization LIMIT 1].IsSandbox, 'Should match org type');
}
}
private class GoogleDriveHttpCalloutMock implements HttpCalloutMock {
public HttpResponse respond(HttpRequest req) {
if (req.getEndpoint().contains('oauth2/v4/token')) {
if (req.getMethod() == 'POST' && req.getBody().contains('grant_type=authorization_code')) {
return mockTokenResponse(200, '{"access_token":"mockAccessToken","refresh_token":"mockRefreshToken"}');
} else if (req.getBody().contains('grant_type=refresh_token')) {
return mockTokenResponse(200, '{"access_token":"mockRefreshedToken"}');
}
}
return mockTokenResponse(404, 'Not Found');
}
}
}