forked from backstage/backstage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Martina Iglesias Fernandez <martina@roadie.io>
- Loading branch information
1 parent
eafb4f9
commit 7abb50d
Showing
1 changed file
with
113 additions
and
0 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
packages/backend-common/src/reading/GcsUrlReader.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright 2020 Spotify AB | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { ConfigReader, JsonObject } from '@backstage/config'; | ||
import { getVoidLogger } from '../logging'; | ||
import { ReadTreeResponseFactory } from './tree'; | ||
import { GcsUrlReader } from './GcsUrlReader'; | ||
import { UrlReaderPredicateTuple } from './types'; | ||
|
||
describe('GcsUrlReader', () => { | ||
const createReader = (config: JsonObject): UrlReaderPredicateTuple[] => { | ||
return GcsUrlReader.factory({ | ||
config: new ConfigReader(config), | ||
logger: getVoidLogger(), | ||
treeResponseFactory: ReadTreeResponseFactory.create({ | ||
config: new ConfigReader({}), | ||
}), | ||
}); | ||
}; | ||
|
||
it('does not create a reader without the gcs field', () => { | ||
const entries = createReader({ | ||
integrations: {}, | ||
}); | ||
expect(entries).toHaveLength(0); | ||
}); | ||
|
||
it('creates a reader with credentials correctly configured', () => { | ||
const entries = createReader({ | ||
integrations: { | ||
gcs: [ | ||
{ | ||
privateKey: '--- BEGIN KEY ---- fakekey --- END KEY ---', | ||
clientEmail: 'someone@example.com', | ||
}, | ||
{ | ||
host: 'proxy.storage.cloud.google.com', | ||
privateKey: '--- BEGIN KEY ---- fakekey2 --- END KEY ---', | ||
clientEmail: 'someone2@example.com', | ||
}, | ||
], | ||
}, | ||
}); | ||
expect(entries).toHaveLength(2); | ||
}); | ||
|
||
it('does not create a reader if the privateKey is missing', () => { | ||
const entries = createReader({ | ||
integrations: { | ||
gcs: [ | ||
{ | ||
clientEmail: 'someone@example.com', | ||
}, | ||
], | ||
}, | ||
}); | ||
expect(entries).toHaveLength(0); | ||
}); | ||
|
||
it('does not create a reader if the clientEmail is missing', () => { | ||
const entries = createReader({ | ||
integrations: { | ||
gcs: [ | ||
{ | ||
privateKey: | ||
'-----BEGIN PRIVATE KEY----- fakekey -----END PRIVATE KEY-----', | ||
}, | ||
], | ||
}, | ||
}); | ||
expect(entries).toHaveLength(0); | ||
}); | ||
|
||
it('predicates', () => { | ||
const readers = createReader({ | ||
integrations: { | ||
gcs: [ | ||
{ | ||
privateKey: | ||
'-----BEGIN PRIVATE KEY----- fakekey -----END PRIVATE KEY-----', | ||
clientEmail: 'someone@example.com', | ||
}, | ||
], | ||
}, | ||
}); | ||
const predicate = readers[0].predicate; | ||
expect(predicate(new URL('https://storage.cloud.google.com'))).toBe(true); | ||
expect( | ||
predicate( | ||
new URL( | ||
'https://storage.cloud.google.com/team1/service1/catalog-info.yaml', | ||
), | ||
), | ||
).toBe(true); | ||
expect(predicate(new URL('https://storage2.cloud.google.com'))).toBe(false); | ||
expect(predicate(new URL('https://cloud.google.com'))).toBe(false); | ||
expect(predicate(new URL('https://google.com'))).toBe(false); | ||
expect(predicate(new URL('https://a.example.com/test'))).toBe(false); | ||
}); | ||
}); |