@@ -7,15 +7,22 @@ import com.intellij.openapi.util.SystemInfo
77import com.intellij.openapi.util.io.NioFiles
88import com.intellij.testFramework.ApplicationExtension
99import org.assertj.core.api.Assertions.assertThat
10+ import org.junit.Assert.assertEquals
11+ import org.junit.Assert.assertNotNull
12+ import org.junit.Assert.assertNull
1013import org.junit.jupiter.api.BeforeEach
1114import org.junit.jupiter.api.Test
1215import org.junit.jupiter.api.condition.DisabledOnOs
1316import org.junit.jupiter.api.condition.OS
1417import org.junit.jupiter.api.extension.ExtendWith
1518import org.junit.jupiter.api.io.TempDir
19+ import org.mockito.Mockito.mockStatic
20+ import org.mockito.kotlin.any
21+ import org.mockito.kotlin.whenever
1622import software.aws.toolkits.core.utils.readText
1723import software.aws.toolkits.core.utils.test.assertPosixPermissions
1824import software.aws.toolkits.core.utils.writeText
25+ import java.io.IOException
1926import java.nio.file.Files
2027import java.nio.file.Path
2128import 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