Skip to content

Commit df7f919

Browse files
committed
tests
1 parent a7be998 commit df7f919

File tree

2 files changed

+119
-1
lines changed
  • plugins/core/jetbrains-community
    • src/software/aws/toolkits/jetbrains/core/credentials/sso
    • tst/software/aws/toolkits/jetbrains/core/credentials/sso

2 files changed

+119
-1
lines changed

plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/core/credentials/sso/DiskCache.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import com.fasterxml.jackson.databind.util.StdDateFormat
2121
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
2222
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
2323
import com.fasterxml.jackson.module.kotlin.readValue
24+
import org.jetbrains.annotations.VisibleForTesting
2425
import software.aws.toolkits.core.utils.createParentDirectories
2526
import software.aws.toolkits.core.utils.debug
2627
import software.aws.toolkits.core.utils.deleteIfExists
@@ -329,7 +330,8 @@ class DiskCache(
329330
InMemoryCache.put(path.toString(), data)
330331
}
331332

332-
private object InMemoryCache {
333+
@VisibleForTesting
334+
internal object InMemoryCache {
333335
private val cache = ConcurrentHashMap<String, ByteArray>()
334336

335337
fun put(key: String, value: ByteArray) {

plugins/core/jetbrains-community/tst/software/aws/toolkits/jetbrains/core/credentials/sso/DiskCacheTest.kt

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,22 @@ import com.intellij.openapi.util.SystemInfo
77
import com.intellij.openapi.util.io.NioFiles
88
import com.intellij.testFramework.ApplicationExtension
99
import org.assertj.core.api.Assertions.assertThat
10+
import org.junit.Assert.assertEquals
11+
import org.junit.Assert.assertNotNull
12+
import org.junit.Assert.assertNull
1013
import org.junit.jupiter.api.BeforeEach
1114
import org.junit.jupiter.api.Test
1215
import org.junit.jupiter.api.condition.DisabledOnOs
1316
import org.junit.jupiter.api.condition.OS
1417
import org.junit.jupiter.api.extension.ExtendWith
1518
import org.junit.jupiter.api.io.TempDir
19+
import org.mockito.Mockito.mockStatic
20+
import org.mockito.kotlin.any
21+
import org.mockito.kotlin.whenever
1622
import software.aws.toolkits.core.utils.readText
1723
import software.aws.toolkits.core.utils.test.assertPosixPermissions
1824
import software.aws.toolkits.core.utils.writeText
25+
import java.io.IOException
1926
import java.nio.file.Files
2027
import java.nio.file.Path
2128
import java.nio.file.Paths
@@ -49,6 +56,14 @@ class DiskCacheTest {
4956
sut = DiskCache(cacheLocation, clock)
5057
}
5158

59+
fun setupMockOutputStreamThrowingIOException() {
60+
mockStatic(Files::class.java).use { mockedFiles ->
61+
whenever(Files.newOutputStream(any<Path>())).thenThrow(
62+
IOException("No space left on device")
63+
)
64+
}
65+
}
66+
5267
@Test
5368
fun nonExistentClientRegistrationReturnsNull() {
5469
assertThat(
@@ -713,4 +728,105 @@ class DiskCacheTest {
713728
.usingRecursiveComparison()
714729
.isEqualTo(sut.loadAccessToken(key2))
715730
}
731+
732+
@Test
733+
fun `saveAccessToken falls back to InMemoryCache when disk is full`() {
734+
setupMockOutputStreamThrowingIOException()
735+
// Mock the writeKey method to simulate a disk full scenario
736+
val key = PKCEAccessTokenCacheKey(ssoUrl, ssoRegion, scopes)
737+
val testToken = PKCEAuthorizationGrantToken(
738+
ssoUrl,
739+
ssoRegion,
740+
"test_access_token",
741+
"test_refresh_token",
742+
Instant.now().plusSeconds(3600),
743+
Instant.now()
744+
)
745+
746+
sut.saveAccessToken(key, testToken)
747+
748+
val loadedToken = sut.loadAccessToken(key)
749+
assertNotNull(loadedToken)
750+
assertEquals(testToken, loadedToken)
751+
}
752+
753+
@Test
754+
fun `saveClientRegistration falls back to InMemoryCache when disk is full`() {
755+
setupMockOutputStreamThrowingIOException()
756+
val key = PKCEClientRegistrationCacheKey(
757+
issuerUrl = ssoUrl,
758+
scopes = scopes,
759+
region = ssoRegion,
760+
clientType = "public",
761+
grantTypes = listOf("authorization_code", "refresh_token"),
762+
redirectUris = listOf("http://127.0.0.1/oauth/callback")
763+
)
764+
val testRegistration = PKCEClientRegistration(
765+
"test_client_id",
766+
"test_client_secret",
767+
Instant.now().plusSeconds(3600),
768+
scopes,
769+
ssoUrl,
770+
ssoRegion,
771+
"public",
772+
listOf("authorization_code", "refresh_token"),
773+
listOf("http://127.0.0.1/oauth/callback")
774+
)
775+
776+
sut.saveClientRegistration(key, testRegistration)
777+
778+
val loadedRegistration = sut.loadClientRegistration(key)
779+
assertNotNull(loadedRegistration)
780+
assertEquals(testRegistration, loadedRegistration)
781+
}
782+
783+
@Test
784+
fun `invalidateAccessToken removes token from InMemoryCache when disk is full`() {
785+
setupMockOutputStreamThrowingIOException()
786+
val key = PKCEAccessTokenCacheKey(ssoUrl, ssoRegion, scopes)
787+
val testToken = PKCEAuthorizationGrantToken(
788+
ssoUrl,
789+
ssoRegion,
790+
"test_access_token",
791+
"test_refresh_token",
792+
Instant.now().plusSeconds(3600),
793+
Instant.now()
794+
)
795+
796+
sut.saveAccessToken(key, testToken)
797+
sut.invalidateAccessToken(key)
798+
799+
val loadedToken = sut.loadAccessToken(key)
800+
assertNull(loadedToken)
801+
}
802+
803+
@Test
804+
fun `invalidateClientRegistration removes registration from InMemoryCache when disk is full`() {
805+
setupMockOutputStreamThrowingIOException()
806+
val key = PKCEClientRegistrationCacheKey(
807+
issuerUrl = ssoUrl,
808+
scopes = scopes,
809+
region = ssoRegion,
810+
clientType = "public",
811+
grantTypes = listOf("authorization_code", "refresh_token"),
812+
redirectUris = listOf("http://127.0.0.1/oauth/callback")
813+
)
814+
val testRegistration = PKCEClientRegistration(
815+
"test_client_id",
816+
"test_client_secret",
817+
Instant.now().plusSeconds(3600),
818+
scopes,
819+
ssoUrl,
820+
ssoRegion,
821+
"public",
822+
listOf("authorization_code", "refresh_token"),
823+
listOf("http://127.0.0.1/oauth/callback")
824+
)
825+
826+
sut.saveClientRegistration(key, testRegistration)
827+
sut.invalidateClientRegistration(key)
828+
829+
val loadedRegistration = sut.loadClientRegistration(key)
830+
assertNull(loadedRegistration)
831+
}
716832
}

0 commit comments

Comments
 (0)