-
-
Notifications
You must be signed in to change notification settings - Fork 12
[Don't merge] Added staging tests to CI/CD #687
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rmgpinto
wants to merge
2
commits into
main
Choose a base branch
from
add-staging-cicd-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
153 changes: 153 additions & 0 deletions
153
src/storage/gcloud-storage/gcp-storage.service.integration.test.ts
This file contains hidden or 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,153 @@ | ||
import { readFileSync } from 'node:fs'; | ||
import path from 'node:path'; | ||
import { getError, getValue, isError } from 'core/result'; | ||
import { File as NodeFile } from 'fetch-blob/file.js'; | ||
import { beforeAll, describe, expect, it } from 'vitest'; | ||
import { GCPStorageService } from './gcp-storage.service'; | ||
|
||
const logger = { | ||
info: console.log, | ||
error: console.error, | ||
warn: console.warn, | ||
} as unknown as import('@logtape/logtape').Logger; | ||
|
||
const TEST_IMAGE_PATH = path.join(__dirname, 'assets/dog.jpg'); | ||
const TEST_ACCOUNT_UUID = 'integration-tests'; | ||
|
||
describe('GCPStorageService Integration', () => { | ||
let service: GCPStorageService; | ||
|
||
beforeAll(async () => { | ||
service = new GCPStorageService(logger); | ||
await service.init(); | ||
}); | ||
|
||
describe('saveFile', () => { | ||
it('should save an image file to the bucket and return a valid URL', async () => { | ||
const buffer = readFileSync(TEST_IMAGE_PATH); | ||
const file = new NodeFile([buffer], 'dog.jpg', { | ||
type: 'image/jpeg', | ||
}); | ||
|
||
const result = await service.saveFile( | ||
file as unknown as File, | ||
TEST_ACCOUNT_UUID, | ||
); | ||
|
||
expect(isError(result)).toBe(false); | ||
if (!isError(result)) { | ||
const url = getValue(result); | ||
expect(url).toBeTruthy(); | ||
expect(() => new URL(url)).not.toThrow(); | ||
expect(url).toContain(TEST_ACCOUNT_UUID); | ||
|
||
if (process.env.GCP_STORAGE_EMULATOR_HOST) { | ||
expect(url).toContain( | ||
'localhost:4443/storage/v1/b/activitypub/o/', | ||
); | ||
expect(url).toContain('?alt=media'); | ||
} else { | ||
const res = await fetch(url); | ||
expect(res.status).toBe(200); | ||
} | ||
} | ||
}); | ||
|
||
it('should reject files larger than 5MB', async () => { | ||
// Create a 6MB buffer | ||
const largeBuffer = Buffer.alloc(6 * 1024 * 1024); | ||
const file = new NodeFile([largeBuffer], 'large.jpg', { | ||
type: 'image/jpeg', | ||
}); | ||
|
||
const result = await service.saveFile( | ||
file as unknown as File, | ||
TEST_ACCOUNT_UUID, | ||
); | ||
|
||
expect(isError(result)).toBe(true); | ||
if (isError(result)) { | ||
expect(getError(result)).toBe('file-too-large'); | ||
} | ||
}); | ||
|
||
it('should reject unsupported file types', async () => { | ||
const buffer = readFileSync(TEST_IMAGE_PATH); | ||
const file = new NodeFile([buffer], 'test.gif', { | ||
type: 'image/gif', | ||
}); | ||
|
||
const result = await service.saveFile( | ||
file as unknown as File, | ||
TEST_ACCOUNT_UUID, | ||
); | ||
|
||
expect(isError(result)).toBe(true); | ||
if (isError(result)) { | ||
expect(getError(result)).toBe('file-type-not-supported'); | ||
} | ||
}); | ||
}); | ||
|
||
describe('verifyImageUrl', () => { | ||
it('should verify a valid image URL', async () => { | ||
const buffer = readFileSync(TEST_IMAGE_PATH); | ||
const file = new NodeFile([buffer], 'dog.jpg', { | ||
type: 'image/jpeg', | ||
}); | ||
|
||
const saveResult = await service.saveFile( | ||
file as unknown as File, | ||
TEST_ACCOUNT_UUID, | ||
); | ||
|
||
expect(isError(saveResult)).toBe(false); | ||
if (!isError(saveResult)) { | ||
const url = new URL(getValue(saveResult)); | ||
const verifyResult = await service.verifyImageUrl(url); | ||
expect(isError(verifyResult)).toBe(false); | ||
if (!isError(verifyResult)) { | ||
expect(getValue(verifyResult)).toBe(true); | ||
} | ||
} | ||
}); | ||
|
||
it('should reject invalid URLs', async () => { | ||
const invalidUrl = new URL('https://example.com/invalid.jpg'); | ||
const result = await service.verifyImageUrl(invalidUrl); | ||
|
||
expect(isError(result)).toBe(true); | ||
if (isError(result)) { | ||
expect(getError(result)).toBe('invalid-url'); | ||
} | ||
}); | ||
|
||
it('should reject URLs with invalid file paths', async () => { | ||
const invalidPathUrl = new URL( | ||
'https://storage.googleapis.com/activitypub/invalid/path.jpg', | ||
); | ||
const result = await service.verifyImageUrl(invalidPathUrl); | ||
|
||
expect(isError(result)).toBe(true); | ||
if (isError(result)) { | ||
process.env.GCP_STORAGE_EMULATOR_HOST | ||
? expect(getError(result)).toBe('invalid-url') | ||
: expect(getError(result)).toBe('invalid-file-path'); | ||
} | ||
}); | ||
|
||
it('should reject non-existent files', async () => { | ||
const nonExistentUrl = new URL( | ||
`https://storage.googleapis.com/${process.env.GCP_BUCKET_NAME}/images/nonexistent.jpg`, | ||
); | ||
const result = await service.verifyImageUrl(nonExistentUrl); | ||
|
||
expect(isError(result)).toBe(true); | ||
if (isError(result)) { | ||
process.env.GCP_STORAGE_EMULATOR_HOST | ||
? expect(getError(result)).toBe('invalid-url') | ||
: expect(getError(result)).toBe('file-not-found'); | ||
} | ||
}); | ||
}); | ||
}); |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.