import { InMemoryStorage } from '../infrastructure/memory_storage' ; import { CloudAccounts } from './cloud_accounts' ; describe ( 'CloudAccounts' , ( ) => { it ( 'get account methods return null when no cloud accounts are connected' , ( ) => { const cloudAccounts = createCloudAccount ( ) ; expect ( cloudAccounts . getDigitalOceanAccount ( ) ) . toBeNull ( ) ; expect ( cloudAccounts . getGcpAccount ( ) ) . toBeNull ( ) ; } ) ; it ( 'load connects account that exist in local storage' , ( ) => { const storage = createInMemoryStorage ( 'fake-access-token' , 'fake-refresh-token' ) ; const cloudAccounts = createCloudAccount ( storage ) ; expect ( cloudAccounts . getDigitalOceanAccount ( ) ) . not . toBeNull ( ) ; expect ( cloudAccounts . getGcpAccount ( ) ) . not . toBeNull ( ) ; } ) ; it ( 'connects accounts when connect methods are invoked' , ( ) => { const cloudAccounts = createCloudAccount ( ) ; expect ( cloudAccounts . getDigitalOceanAccount ( ) ) . toBeNull ( ) ; cloudAccounts . connectDigitalOceanAccount ( 'fake-access-token' ) ; expect ( cloudAccounts . getDigitalOceanAccount ( ) ) . not . toBeNull ( ) ; expect ( cloudAccounts . getGcpAccount ( ) ) . toBeNull ( ) ; cloudAccounts . connectGcpAccount ( 'fake-access-token' ) ; expect ( cloudAccounts . getGcpAccount ( ) ) . not . toBeNull ( ) ; } ) ; it ( 'removes account when disconnect is invoked' , ( ) => { const storage = createInMemoryStorage ( 'fake-access-token' , 'fake-refresh-token' ) ; const cloudAccounts = createCloudAccount ( storage ) ; expect ( cloudAccounts . getDigitalOceanAccount ( ) ) . not . toBeNull ( ) ; cloudAccounts . disconnectDigitalOceanAccount ( ) ; expect ( cloudAccounts . getDigitalOceanAccount ( ) ) . toBeNull ( ) ; expect ( cloudAccounts . getGcpAccount ( ) ) . not . toBeNull ( ) ; cloudAccounts . disconnectGcpAccount ( ) ; expect ( cloudAccounts . getGcpAccount ( ) ) . toBeNull ( ) ; } ) ; it ( 'functional noop on calling disconnect when accounts are not connected' , ( ) => { const cloudAccounts = createCloudAccount ( ) ; expect ( cloudAccounts . getDigitalOceanAccount ( ) ) . toBeNull ( ) ; cloudAccounts . disconnectDigitalOceanAccount ( ) ; expect ( cloudAccounts . getDigitalOceanAccount ( ) ) . toBeNull ( ) ; expect ( cloudAccounts . getGcpAccount ( ) ) . toBeNull ( ) ; cloudAccounts . disconnectGcpAccount ( ) ; expect ( cloudAccounts . getGcpAccount ( ) ) . toBeNull ( ) ; } ) ; it ( 'migrates existing legacy DigitalOcean access token on load' , ( ) => { const storage = new InMemoryStorage ( ) ; storage . setItem ( 'LastDOToken' , 'legacy-digitalocean-access-token' ) ; const cloudAccounts = createCloudAccount ( storage ) ; expect ( cloudAccounts . getDigitalOceanAccount ( ) ) . not . toBeNull ( ) ; } ) ; it ( 'updates legacy DigitalOcean access token when account reconnected' , ( ) => { const storage = new InMemoryStorage ( ) ; storage . setItem ( 'LastDOToken' , 'legacy-digitalocean-access-token' ) ; const cloudAccounts = createCloudAccount ( storage ) ; expect ( storage . getItem ( 'LastDOToken' ) ) . toEqual ( 'legacy-digitalocean-access-token' ) ; cloudAccounts . connectDigitalOceanAccount ( 'new-digitalocean-access-token' ) ; expect ( storage . getItem ( 'LastDOToken' ) ) . toEqual ( 'new-digitalocean-access-token' ) ; } ) ; } ) ; function createInMemoryStorage ( digitalOceanAccessToken ? , gcpRefreshToken ? ) { const storage = new InMemoryStorage ( ) ; if ( digitalOceanAccessToken ) { storage . setItem ( 'accounts.digitalocean' , JSON . stringify ( { accessToken : digitalOceanAccessToken } ) ) ; } if ( gcpRefreshToken ) { storage . setItem ( 'accounts.gcp' , JSON . stringify ( { refreshToken : gcpRefreshToken } ) ) ; } return storage ; } function createCloudAccount ( storage = createInMemoryStorage ( ) ) { const shadowboxSettings = { imageId : 'fake-image-id' , metricsUrl : 'fake-metrics-url' , sentryApiUrl : 'fake-sentry-api' , } ; return new CloudAccounts ( shadowboxSettings , true , storage ) ; }