|
| 1 | +@isTest |
| 2 | +private class SigmaJWTControllerTest { |
| 3 | + |
| 4 | + @isTest |
| 5 | + static void testGetSignedJWT() { |
| 6 | + // Test the main JWT generation method |
| 7 | + Test.startTest(); |
| 8 | + String jwt = SigmaJWTController.getSignedJWT(); |
| 9 | + Test.stopTest(); |
| 10 | + |
| 11 | + // Verify JWT structure (header.payload.signature) |
| 12 | + System.assertNotEquals(null, jwt, 'JWT should not be null'); |
| 13 | + List<String> jwtParts = jwt.split('\\.'); |
| 14 | + System.assertEquals(3, jwtParts.size(), 'JWT should have 3 parts (header.payload.signature)'); |
| 15 | + |
| 16 | + // Verify each part is not empty |
| 17 | + System.assert(jwtParts[0].length() > 0, 'JWT header should not be empty'); |
| 18 | + System.assert(jwtParts[1].length() > 0, 'JWT payload should not be empty'); |
| 19 | + System.assert(jwtParts[2].length() > 0, 'JWT signature should not be empty'); |
| 20 | + } |
| 21 | + |
| 22 | + @isTest |
| 23 | + static void testBase64UrlEncode() { |
| 24 | + // Test base64 URL encoding for strings |
| 25 | + Test.startTest(); |
| 26 | + String testString = 'Test String'; |
| 27 | + String encoded = SigmaJWTController.base64UrlEncode(testString); |
| 28 | + Test.stopTest(); |
| 29 | + |
| 30 | + // Verify encoding doesn't contain URL-unsafe characters |
| 31 | + System.assertNotEquals(null, encoded, 'Encoded string should not be null'); |
| 32 | + System.assert(!encoded.contains('+'), 'Encoded string should not contain +'); |
| 33 | + System.assert(!encoded.contains('/'), 'Encoded string should not contain /'); |
| 34 | + System.assert(!encoded.contains('='), 'Encoded string should not contain ='); |
| 35 | + } |
| 36 | + |
| 37 | + @isTest |
| 38 | + static void testBase64UrlEncodeBlob() { |
| 39 | + // Test base64 URL encoding for blobs |
| 40 | + Test.startTest(); |
| 41 | + Blob testBlob = Blob.valueOf('Test Blob Data'); |
| 42 | + String encoded = SigmaJWTController.base64UrlEncodeBlob(testBlob); |
| 43 | + Test.stopTest(); |
| 44 | + |
| 45 | + // Verify encoding doesn't contain URL-unsafe characters |
| 46 | + System.assertNotEquals(null, encoded, 'Encoded blob should not be null'); |
| 47 | + System.assert(!encoded.contains('+'), 'Encoded blob should not contain +'); |
| 48 | + System.assert(!encoded.contains('/'), 'Encoded blob should not contain /'); |
| 49 | + System.assert(!encoded.contains('='), 'Encoded blob should not contain ='); |
| 50 | + } |
| 51 | + |
| 52 | + @isTest |
| 53 | + static void testGenerateRandomString() { |
| 54 | + // Test random string generation |
| 55 | + Test.startTest(); |
| 56 | + String randomString8 = SigmaJWTController.generateRandomString(8); |
| 57 | + String randomString16 = SigmaJWTController.generateRandomString(16); |
| 58 | + Test.stopTest(); |
| 59 | + |
| 60 | + // Verify string lengths |
| 61 | + System.assertEquals(8, randomString8.length(), 'Random string should be 8 characters'); |
| 62 | + System.assertEquals(16, randomString16.length(), 'Random string should be 16 characters'); |
| 63 | + |
| 64 | + // Verify strings are different (extremely unlikely to be same) |
| 65 | + System.assertNotEquals(randomString8, randomString16.substring(0, 8), |
| 66 | + 'Random strings should be different'); |
| 67 | + } |
| 68 | + |
| 69 | + @isTest |
| 70 | + static void testGenerateUUID() { |
| 71 | + // Test UUID generation |
| 72 | + Test.startTest(); |
| 73 | + String uuid1 = SigmaJWTController.generateUUID(); |
| 74 | + String uuid2 = SigmaJWTController.generateUUID(); |
| 75 | + Test.stopTest(); |
| 76 | + |
| 77 | + // Verify UUID format (8-4-4-4-12) |
| 78 | + System.assertNotEquals(null, uuid1, 'UUID should not be null'); |
| 79 | + List<String> parts = uuid1.split('-'); |
| 80 | + System.assertEquals(5, parts.size(), 'UUID should have 5 parts separated by dashes'); |
| 81 | + System.assertEquals(8, parts[0].length(), 'First UUID part should be 8 characters'); |
| 82 | + System.assertEquals(4, parts[1].length(), 'Second UUID part should be 4 characters'); |
| 83 | + System.assertEquals(4, parts[2].length(), 'Third UUID part should be 4 characters'); |
| 84 | + System.assertEquals(4, parts[3].length(), 'Fourth UUID part should be 4 characters'); |
| 85 | + System.assertEquals(12, parts[4].length(), 'Fifth UUID part should be 12 characters'); |
| 86 | + |
| 87 | + // Verify UUIDs are unique |
| 88 | + System.assertNotEquals(uuid1, uuid2, 'Generated UUIDs should be unique'); |
| 89 | + } |
| 90 | + |
| 91 | + @isTest |
| 92 | + static void testGetAccountNames() { |
| 93 | + // Create test account data |
| 94 | + Account testAccount = new Account( |
| 95 | + Name = 'Cooper Industries Test' |
| 96 | + ); |
| 97 | + insert testAccount; |
| 98 | + |
| 99 | + // Test account name retrieval |
| 100 | + Test.startTest(); |
| 101 | + List<String> accountNames = SigmaJWTController.getAccountNames(); |
| 102 | + Test.stopTest(); |
| 103 | + |
| 104 | + // Verify results |
| 105 | + System.assertNotEquals(null, accountNames, 'Account names list should not be null'); |
| 106 | + System.assertEquals(1, accountNames.size(), 'Should return 1 account name'); |
| 107 | + System.assertEquals('Cooper', accountNames[0], 'Account name should be trimmed to "Cooper"'); |
| 108 | + } |
| 109 | + |
| 110 | + @isTest |
| 111 | + static void testGetAccountNamesNoMatches() { |
| 112 | + // Test when no matching accounts exist |
| 113 | + // Note: We don't create any accounts with 'Cooper' in the name |
| 114 | + |
| 115 | + Test.startTest(); |
| 116 | + List<String> accountNames = SigmaJWTController.getAccountNames(); |
| 117 | + Test.stopTest(); |
| 118 | + |
| 119 | + // Verify empty list is returned |
| 120 | + System.assertNotEquals(null, accountNames, 'Account names list should not be null'); |
| 121 | + System.assertEquals(0, accountNames.size(), 'Should return 0 account names when no matches'); |
| 122 | + } |
| 123 | +} |
0 commit comments