Skip to content

Commit

Permalink
FFM-10286 Unit test for missing auth token
Browse files Browse the repository at this point in the history
  • Loading branch information
erdirowlands committed Dec 29, 2023
1 parent 244758c commit 37c123d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
40 changes: 40 additions & 0 deletions force-app/test/FFClientTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ private class FFClientTest {
private FFModels.AuthInfo mockAuthInfo;
private Long mockLastAuthTime;
public Boolean authenticateCalled = false;
public Boolean simulateEvictedAuthToken;

public MockFFAuthService(FFModels.AuthInfo mockAuthInfo) {
this.mockAuthInfo = mockAuthInfo;
}

public MockFFAuthService(FFModels.AuthInfo mockAuthInfo, Boolean simulateEvictedAuthToken) {
this.mockAuthInfo = mockAuthInfo;
this.simulateEvictedAuthToken = simulateEvictedAuthToken;
}

public FFModels.AuthInfo authenticate(Boolean force) {
authenticateCalled = true;
Expand All @@ -39,6 +45,9 @@ private class FFClientTest {
}

public Boolean isAuthTokenPresent() {
if (this.simulateEvictedAuthToken) {
return false;
}
return true;
}

Expand Down Expand Up @@ -285,6 +294,37 @@ private class FFClientTest {
System.assertEquals(true, mockAuthService.authenticateCalled, 'Reauthentication was not triggered as expected');
}

@isTest
private static void shouldReauthenticateWhenTokenIsMissing() {
// Create mock authentication information
FFModels.AuthInfo mockAuthInfo = new FFModels.AuthInfo('mockToken', 'envUUID', 'cluster');
MockFFAuthService mockAuthService = new MockFFAuthService(mockAuthInfo, true);

// Set the last authentication time to now
Long pastTimeMillis = Datetime.now().getTime();
mockAuthService.setLastAuthTime(pastTimeMillis);

// Create the FFClient configuration with a specified expiration interval
FFConfig config = FFConfig.builder().authExpireAfter(24 * 60 * 60) // Token expires after 24 hours
.cache(new FFMockCache()).build();

List<FFModelsFeatures.FeatureConfig> features = new List<FFModelsFeatures.FeatureConfig>();
List<FFModelsFeatures.TargetSegment> targets = new List<FFModelsFeatures.TargetSegment>();
// Instantiate FFClient with the mock auth service
FFClient client = new FFClient(mockAuthService, mockApi(features, targets), mockMetricsApi(features, targets), config, 'test', 'test');

// Instantiate the CacheUpdator with the FFClient instance
FFClient.CacheUpdator cacheUpdator = new FFClient.CacheUpdator(client);

// Directly invoke the logic that would be triggered by the poller
// Call the cacheUpdator twice, because it skips on the first iteration.
cacheUpdator.call(null, null);
cacheUpdator.call(null, null);

// Verify that reauthentication was triggered due to the token missing
System.assertEquals(true, mockAuthService.authenticateCalled, 'Reauthentication was not triggered as expected');
}

@isTest
private static void shouldNotReauthenticateWhenTokenIsStillValid() {
// Create mock authentication information
Expand Down
4 changes: 4 additions & 0 deletions force-app/test/FFMockCache.cls
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public class FFMockCache implements FFCache {
this.cache.put('auth'+key, authData);
}

public void deleteAuth(String key) {
this.cache.remove('auth'+key);
}


public void putLastAuthTimeWithoutTTL(String key, Long lastAuthTime) {
this.cache.put('lastAuth'+key, lastAuthTime);
Expand Down

0 comments on commit 37c123d

Please sign in to comment.