@@ -15,11 +15,13 @@ jest.mock('@linode/api-v4', () => ({
1515
1616import { createBucket , createObjectStorageKeys , ObjectStorageKey , setToken } from '@linode/api-v4'
1717import { OtomiError } from 'src/error'
18- import { ObjectStorageClient } from './wizardUtils'
18+ import { ObjWizard } from 'src/otomi-models'
19+ import OtomiStack from 'src/otomi-stack'
20+ import { defineClusterId , ObjectStorageClient } from './wizardUtils'
1921
2022describe ( 'ObjectStorageClient' , ( ) => {
2123 let client : ObjectStorageClient
22- const clusterId = 12345
24+ let clusterId : string | undefined = ' 12345'
2325
2426 beforeEach ( ( ) => {
2527 client = new ObjectStorageClient ( 'test-token' )
@@ -35,7 +37,16 @@ describe('ObjectStorageClient', () => {
3537 describe ( 'createObjectStorageBucket' , ( ) => {
3638 const label = 'test-bucket'
3739 const region = 'us-east'
40+ let otomiStack : OtomiStack
41+ const domainSuffix = 'dev.linode-apl.net'
42+ beforeEach ( async ( ) => {
43+ otomiStack = new OtomiStack ( )
44+ await otomiStack . init ( )
45+ } )
3846
47+ afterEach ( ( ) => {
48+ jest . restoreAllMocks ( )
49+ } )
3950 it ( 'should successfully create bucket' , async ( ) => {
4051 const mockResponse = { label : 'test-bucket' }
4152 ; ( createBucket as jest . Mock ) . mockResolvedValue ( mockResponse )
@@ -81,6 +92,38 @@ describe('ObjectStorageClient', () => {
8192 // @ts -ignore
8293 expect ( result . code ) . toBe ( 500 )
8394 } )
95+
96+ test ( 'should return lkeClusterId' , ( ) => {
97+ const settings = { cluster : { name : 'cluster-123' } }
98+ clusterId = defineClusterId ( settings . cluster . name )
99+
100+ expect ( clusterId ) . toBe ( 'cluster-123' )
101+ } )
102+
103+ test ( 'should return stripped down clusterId when name does not include prefix' , ( ) => {
104+ const settings = { cluster : { name : 'aplinstall123' } }
105+ clusterId = defineClusterId ( settings . cluster . name )
106+
107+ expect ( clusterId ) . toBe ( '123' )
108+ } )
109+
110+ test ( 'should return undefined when cluster name is undefined' , ( ) => {
111+ const settings : any = { cluster : { } }
112+ clusterId = defineClusterId ( settings . cluster ?. name )
113+
114+ expect ( clusterId ) . toBe ( undefined )
115+ } )
116+
117+ test ( 'should return error when cluster name is undefined' , async ( ) => {
118+ const data = { apiToken : 'some-token' , regionId : 'us-east' , label : 'my-cluster' }
119+ jest . spyOn ( otomiStack , 'getSettings' ) . mockReturnValue ( {
120+ cluster : { domainSuffix, provider : 'linode' } ,
121+ } as any )
122+ const result : ObjWizard = await otomiStack . createObjWizard ( data )
123+
124+ expect ( result . status ) . toBe ( 'error' )
125+ expect ( result . errorMessage ) . toBe ( 'Cluster name is not found.' )
126+ } )
84127 } )
85128
86129 describe ( 'createObjectStorageKey' , ( ) => {
@@ -103,7 +146,7 @@ describe('ObjectStorageClient', () => {
103146 }
104147 ; ( createObjectStorageKeys as jest . Mock ) . mockResolvedValue ( mockResponse )
105148
106- const result = await client . createObjectStorageKey ( clusterId , region , bucketNames )
149+ const result = await client . createObjectStorageKey ( clusterId ! , region , bucketNames )
107150
108151 expect ( createObjectStorageKeys ) . toHaveBeenCalledWith ( {
109152 label : `lke${ clusterId } -key-1704110400000` ,
@@ -125,7 +168,7 @@ describe('ObjectStorageClient', () => {
125168 }
126169 ; ( createObjectStorageKeys as jest . Mock ) . mockRejectedValue ( mockError )
127170
128- const result = await client . createObjectStorageKey ( clusterId , region , bucketNames )
171+ const result = await client . createObjectStorageKey ( clusterId ! , region , bucketNames )
129172 expect ( result ) . toBeInstanceOf ( OtomiError )
130173 // @ts -ignore
131174 expect ( result . publicMessage ) . toBe ( 'Your OAuth token is not authorized to use this endpoint' )
0 commit comments