Skip to content

Commit

Permalink
test: updates env test file
Browse files Browse the repository at this point in the history
  • Loading branch information
gunar committed Aug 17, 2022
1 parent 49aa64d commit 742c53c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
1 change: 1 addition & 0 deletions api.planx.uk/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ GOVUK_NOTIFY_EXPIRY_EMAIL_TEMPLATE_ID=9619f89d-5d33-4cb0-a365-42c431ea9db3
GOVUK_NOTIFY_API_KEY_TEAM=Test

HASURA_PLANX_API_KEY=testtesttest
FILE_API_KEY=test
40 changes: 14 additions & 26 deletions api.planx.uk/server.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const nock = require("nock");
const supertest = require("supertest");
const { generateFileHash } = require("./s3");
const loadOrRecordNockRequests = require("./tests/loadOrRecordNockRequests");

const { queryMock } = require("./tests/graphqlQueryMock");
Expand Down Expand Up @@ -335,7 +334,6 @@ describe("File upload", () => {
expect(res.body).toEqual({
file_type: 'text/plain',
key: expect.stringContaining('some_file.txt'),
file_hash: expect.any(String),
});
expect(mockPutObject).toHaveBeenCalledTimes(1);
});
Expand Down Expand Up @@ -398,64 +396,54 @@ describe("File download", () => {
});

it("file/public - should not download private files", async () => {
const filePath = 'somekey/file_name.txt'
getObjectResponse = {
...getObjectResponse,
Metadata: {
file_hash: '123'
is_private: 'true'
}
}
await supertest(app)
.get("/file/public/somekey/file_name.txt")
.expect(400)
.then(res => {
expect(mockGetObject).toHaveBeenCalledTimes(1);
expect(res.body.error).toBe('bad request')
})
});

it("file/private - should not download with missing file hash", async () => {
await supertest(app)
.get("/file/private/somekey/file_name.txt")
.get(`/file/public/${filePath}`)
.expect(400)
.then(res => {
expect(mockGetObject).not.toHaveBeenCalled();
expect(res.body.error).toBe('wrong or missing headers')
})
expect(mockGetObject).toHaveBeenCalledTimes(1);
expect(res.body.error).toBe("bad request")
});
});

it("file/private - should not download if file has different hash", async () => {
it("file/private - should not download if file is private", async () => {
const filePath = 'somekey/file_name.txt'
const hash = generateFileHash(filePath);
getObjectResponse = {
...getObjectResponse,
Metadata: {
file_hash: 'somehash'
is_private: 'true'
}
}

await supertest(app)
.get(`/file/private/${filePath}`)
.set({ 'file-hash': hash })
.expect(403)
.get(`/file/public/${filePath}`)
.expect(400)
.then(res => {
expect(mockGetObject).toHaveBeenCalledTimes(1);
expect(res.body.error).toBe('unauthorized')
expect(res.body.error).toBe("bad request")
});
});

it("file/private - should download file", async () => {
const filePath = 'somekey/file_name.txt'
const hash = generateFileHash(filePath);

getObjectResponse = {
...getObjectResponse,
Metadata: {
file_hash: hash
is_private: 'true'
}
}

await supertest(app)
.get(`/file/private/${filePath}`)
.set({ 'file-hash': hash })
.set({ 'api-key': 'test' })
.expect(200)
.then(() => {
expect(mockGetObject).toHaveBeenCalledTimes(1);
Expand Down

0 comments on commit 742c53c

Please sign in to comment.