Skip to content

Commit

Permalink
Organize S3 mock usage more carefully. Other cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
svenaas committed Oct 26, 2023
1 parent 8aae278 commit 49cffe4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 23 deletions.
7 changes: 4 additions & 3 deletions test/api/requests/published-branch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ const factory = require('../support/factory');
const { authenticatedSession } = require('../support/session');
const validateAgainstJSONSchema = require('../support/validateAgainstJSONSchema');

const s3Mock = mockClient(S3Client);

describe('Published Branches API', () => {
after(() => s3Mock.restore());
afterEach(() => nock.cleanAll());
beforeEach(() => s3Mock.reset());

describe('GET /v0/site/:site_id/published-branch', () => {
it('should require authentication', (done) => {
Expand Down Expand Up @@ -60,7 +64,6 @@ describe('Published Branches API', () => {
const user = factory.user();
const sitePromise = factory.site({ users: Promise.all([user]) });

const s3Mock = mockClient(S3Client);
s3Mock.on(ListObjectsV2Command).resolvesOnce({
IsTruncated: true,
CommonPrefixes: [
Expand Down Expand Up @@ -109,7 +112,6 @@ describe('Published Branches API', () => {
mockTokenRequest();
apiNocks.mockDefaultCredentials();

const s3Mock = mockClient(S3Client);
s3Mock.on(ListObjectsV2Command).resolves({
IsTruncated: false,
CommonPrefixes: [
Expand Down Expand Up @@ -159,7 +161,6 @@ describe('Published Branches API', () => {
mockTokenRequest();
apiNocks.mockDefaultCredentials();

const s3Mock = mockClient(S3Client);
s3Mock.on(ListObjectsV2Command).rejects({
code: 'InvalidAccessKeyId',
});
Expand Down
13 changes: 8 additions & 5 deletions test/api/requests/published-file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ const { mockClient } = require('aws-sdk-client-mock');
const mockTokenRequest = require('../support/cfAuthNock');
const apiNocks = require('../support/cfAPINocks');
const app = require('../../../app');
const config = require('../../../config');
const factory = require('../support/factory');
const { authenticatedSession } = require('../support/session');
const validateAgainstJSONSchema = require('../support/validateAgainstJSONSchema');

const s3Mock = mockClient(S3Client);

describe('Published Files API', () => {
after(() => s3Mock.restore());
afterEach(() => nock.cleanAll());

describe('GET /v0/site/:site_id/published-branch/:branch/file', () => {
beforeEach(() => s3Mock.reset());

it('should require authentication', (done) => {
factory.site()
.then(site => request(app)
Expand Down Expand Up @@ -49,21 +53,20 @@ describe('Published Files API', () => {
prefix = `site/${site.owner}/${site.repository}/`;

// Initializing the mock in here because it needs prefix
const s3Mock = mockClient(S3Client);
s3Mock.on(ListObjectsV2Command).resolvesOnce({
IsTruncated: true,
Contents: [
{ Key: `${prefix}abc`, Size: 123 },
{ Key: `${prefix}abc/def`, Size: 456 },
],
ContinuationToken: "A",
NextContinuationToken: "B",
ContinuationToken: 'A',
NextContinuationToken: 'B',
}).resolvesOnce({
IsTruncated: false,
Contents: [
{ Key: `${prefix}ghi`, Size: 789 },
],
ContinuationToken: "B",
ContinuationToken: 'B',
NextContinuationToken: null,
});

Expand Down
10 changes: 5 additions & 5 deletions test/api/unit/services/S3Helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ const config = require('../../../../config');

const S3Helper = require('../../../../api/services/S3Helper');

const s3Mock = mockClient(S3Client);

describe('S3Helper', () => {
after(() => s3Mock.restore());
beforeEach(() => s3Mock.reset());

describe('.listObjects(prefix)', () => {
it('can get objects', (done) => {
const prefix = 'some-prefix/';

const s3Mock = mockClient(S3Client);
s3Mock.on(ListObjectsV2Command, {
Bucket: config.s3.bucket,
Prefix: prefix,
Expand All @@ -39,7 +43,6 @@ describe('S3Helper', () => {
it('can get all objects when initial response is truncated', (done) => {
const prefix = 'abc/123/';

const s3Mock = mockClient(S3Client);
s3Mock.on(ListObjectsV2Command, {
Bucket: config.s3.bucket,
Prefix: prefix,
Expand Down Expand Up @@ -83,7 +86,6 @@ describe('S3Helper', () => {
...extras,
};

const s3Mock = mockClient(S3Client);
s3Mock.on(PutObjectCommand, expected).resolves();

const client = new S3Helper.S3Client(config.s3);
Expand All @@ -94,7 +96,6 @@ describe('S3Helper', () => {
const body = 'Hello World';
const key = 'hello.html';

const s3Mock = mockClient(S3Client);
s3Mock.on(PutObjectCommand).rejects();

const client = new S3Helper.S3Client(config.s3);
Expand All @@ -115,7 +116,6 @@ describe('S3Helper', () => {
...extras,
};

const s3Mock = mockClient(S3Client);
s3Mock.on(GetObjectCommand, expected).resolves({ Body: body });

const client = new S3Helper.S3Client(config.s3);
Expand Down
13 changes: 4 additions & 9 deletions test/api/unit/services/S3PublishedFileLister.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ const apiNocks = require('../../support/cfAPINocks');
const factory = require('../../support/factory');
const S3PublishedFileLister = require('../../../../api/services/S3PublishedFileLister');

const s3Mock = mockClient(S3Client);

describe('S3PublishedFileLister', () => {
after(() => s3Mock.restore());
afterEach(() => nock.cleanAll());
beforeEach(() => s3Mock.reset());

describe('.listPublishedPreviews(site)', () => {
it('should resolve with a list of published previews for the given site', (done) => {
Expand All @@ -21,7 +25,6 @@ describe('S3PublishedFileLister', () => {
factory.site().then((model) => {
site = model;

const s3Mock = mockClient(S3Client);
s3Mock.on(ListObjectsV2Command).resolvesOnce({
IsTruncated: true,
CommonPrefixes: [
Expand All @@ -47,7 +50,6 @@ describe('S3PublishedFileLister', () => {
mockTokenRequest();
apiNocks.mockDefaultCredentials();

const s3Mock = mockClient(S3Client);
s3Mock.on(ListObjectsV2Command).rejects({
code: 'InvalidAccessKeyId',
});
Expand Down Expand Up @@ -76,8 +78,6 @@ describe('S3PublishedFileLister', () => {
site = model;
prefix = `site/${site.owner}/${site.repository}/`;

// Initializing the mock in here because it needs prefix
const s3Mock = mockClient(S3Client);
s3Mock.on(ListObjectsV2Command).resolvesOnce({
IsTruncated: true,
Contents: [
Expand Down Expand Up @@ -121,8 +121,6 @@ describe('S3PublishedFileLister', () => {
site = model;
prefix = `demo/${site.owner}/${site.repository}/`;

// Initializing the mock in here because it needs prefix
const s3Mock = mockClient(S3Client);
s3Mock.on(ListObjectsV2Command).resolvesOnce({
IsTruncated: true,
Contents: [
Expand Down Expand Up @@ -166,8 +164,6 @@ describe('S3PublishedFileLister', () => {
site = model;
prefix = `preview/${site.owner}/${site.repository}/preview/`;

// Initializing the mock in here because it needs prefix
const s3Mock = mockClient(S3Client);
s3Mock.on(ListObjectsV2Command).resolvesOnce({
IsTruncated: true,
Contents: [
Expand Down Expand Up @@ -204,7 +200,6 @@ describe('S3PublishedFileLister', () => {
mockTokenRequest();
apiNocks.mockDefaultCredentials();

const s3Mock = mockClient(S3Client);
s3Mock.on(ListObjectsV2Command).rejects('Test error');

factory.site()
Expand Down
7 changes: 6 additions & 1 deletion test/api/unit/services/S3SiteRemover.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ function createServiceNocks(serviceName, guid, bucketName) {
});
}

const s3Mock = mockClient(S3Client);

describe('S3SiteRemover', () => {
after(() => s3Mock.restore());

beforeEach(() => {
s3Mock.on(HeadBucketCommand).resolves({});
createServiceNocks(s3ServiceName, s3ServiceGuid, awsBucketName);
});

Expand All @@ -60,7 +65,7 @@ describe('S3SiteRemover', () => {

let deletionBucket;
let deletedObjects;
const s3Mock = mockClient(S3Client);

s3Mock
.on(ListObjectsV2Command).resolves({
IsTruncated: false,
Expand Down

0 comments on commit 49cffe4

Please sign in to comment.