Skip to content

Commit

Permalink
Fix some not-synchronous issues with download_file
Browse files Browse the repository at this point in the history
  • Loading branch information
marcdubs committed Nov 19, 2018
1 parent c0ab889 commit a988514
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`download_file when it returns an error rejects with the error 1`] = `"It's a lot of bad things That they wishin' and wishin' and wishin' and wishin' They wishin' on me Yuh, ayy, ayy"`;
exports[`download_file when fs.writeFile returns an error rejects with the error 1`] = `"It's a lot of bad things That they wishin' and wishin' and wishin' and wishin' They wishin' on me Yuh, ayy, ayy"`;

exports[`download_file when getObject returns an error rejects with the error 1`] = `"You Done Messed Up A-Aron!"`;
69 changes: 38 additions & 31 deletions lib/integrations/S3/functions/__tests__/download_file_spec.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
describe("download_file", () => {
describe("when the file is downloaded successfully", () => {
const expected_dest = "some_file_dest.extension",
expected_file_id = "1234",
expected_output = "some_file_data";
let func, data, stream, fs, result, createReadStream;
expected_data = "some_file_data",
expected_file_id = "1234";
let func, data, fs, result;
beforeEach(async done => {
fs = require("fs");
fs.createWriteStream = jest.fn().mockImplementation(() => {
return expected_output;
});
stream = {
pipe: jest.fn()
};

createReadStream = jest.fn().mockImplementation(() => {
return stream;
fs.writeFile = jest.fn().mockImplementation((dest, body, callback) => {
callback(null);
});

data = {
s3: {
getObject: jest.fn().mockImplementation(() => {
return { createReadStream: createReadStream };
getObject: jest.fn().mockImplementation((params, callback) => {
callback(null, { data: expected_data });
})
}
};
Expand All @@ -29,35 +22,49 @@ describe("download_file", () => {
done();
});

it("calls fs.createWriteStream with dest", () => {
expect(fs.createWriteStream).toHaveBeenCalledWith(expected_dest);
});

it("calls stream.pipe with what createWriteStream returned", () => {
expect(stream.pipe).toHaveBeenCalledWith(expected_output);
it("calls fs.writeFile with dest and data", () => {
expect(fs.writeFile).toHaveBeenCalledWith(
expected_dest,
expected_data,
expect.any(Function)
);
});

it("returns undefined", () => {
expect(result).toBeUndefined();
});
});

describe("when it returns an error", () => {
describe("when fs.writeFile returns an error", () => {
const expected_error =
"It's a lot of bad things That they wishin' and wishin' and wishin' and wishin' They wishin' on me Yuh, ayy, ayy";
beforeEach(() => {
fs = require("fs");
fs.writeFile = jest.fn().mockImplementation((dest, body, callback) => {
callback(new Error(expected_error));
});
data = {
s3: {
getObject: jest.fn().mockImplementation((params, callback) => {
callback(null, { data: null });
})
}
};
func = require("../download_file")(data);
});

it("rejects with the error", async () => {
await expect(func()).rejects.toThrowErrorMatchingSnapshot();
});
});

describe("when getObject returns an error", () => {
const expected_error = "You Done Messed Up A-Aron!";
beforeEach(() => {
data = {
s3: {
getObject: jest.fn().mockImplementation(() => {
return {
createReadStream: jest.fn().mockImplementation(() => {
return {
pipe: jest.fn().mockImplementation(() => {
throw new Error(expected_error);
})
};
})
};
getObject: jest.fn().mockImplementation((params, callback) => {
callback(new Error(expected_error));
})
}
};
Expand Down
23 changes: 13 additions & 10 deletions lib/integrations/S3/functions/download_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ module.exports = data => {
Key: fileID,
Bucket: data.bucket
};
try {
var output = fs.createWriteStream(dest);
await data.s3
.getObject(params)
.createReadStream()
.pipe(output);
resolve();
} catch (err) {
reject(err);
}
data.s3.getObject(params, (err, body) => {
if (err) {
reject(err);
} else {
fs.writeFile(dest, body.data, err2 => {
if (err2) {
reject(err2);
return;
}
resolve();
});
}
});
});
};
};
3 changes: 2 additions & 1 deletion lib/integrations/S3/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module.exports = props => {
data.s3 = new S3({
apiVersion: "2006-03-01",
accessKeyId: data.accessKeyId,
secretAccessKey: data.secretAccessKey
secretAccessKey: data.secretAccessKey,
region: data.region || "us-east-2"
});

data.bucket = props.bucket;
Expand Down

0 comments on commit a988514

Please sign in to comment.