Skip to content

Commit 6e4f747

Browse files
d2lamPranav Ravichandran
authored and
Pranav Ravichandran
committed
fix: remove checksum, add tests (#58)
1 parent c4966b9 commit 6e4f747

File tree

4 files changed

+25
-165
lines changed

4 files changed

+25
-165
lines changed

helpers/aws.js

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const AWS = require('aws-sdk');
4-
const crypto = require('crypto');
54

65
class AwsClient {
76
/**
@@ -59,34 +58,6 @@ class AwsClient {
5958
});
6059
});
6160
}
62-
63-
/**
64-
* Compare the checksum of local cache with the one in S3
65-
* @method compareChecksum
66-
* @param {String} localCache content of localFile
67-
* @param {String} cacheKey cache key
68-
* @param {Function} callback callback function
69-
*/
70-
compareChecksum(localCache, cacheKey, callback) {
71-
const params = {
72-
Bucket: this.bucket,
73-
Key: cacheKey
74-
};
75-
const localmd5 = crypto.createHash('md5').update(localCache).digest('hex');
76-
77-
return this.client.headObject(params, (err, data) => {
78-
if (err) {
79-
return callback(err);
80-
}
81-
const remotemd5 = data.Metadata.md5;
82-
83-
if (localmd5 !== remotemd5) {
84-
return callback(null, false);
85-
}
86-
87-
return callback(null, true);
88-
});
89-
}
9061
}
9162

9263
module.exports = AwsClient;

plugins/caches.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -143,22 +143,7 @@ exports.plugin = {
143143
+ `headers ${JSON.stringify(contents.h)}`);
144144

145145
try {
146-
if (!awsClient) {
147-
await cache.set(cacheKey, value, 0);
148-
} else {
149-
await awsClient.compareChecksum(value.c,
150-
`caches/${cacheKey}`, async (err, areEqual) => {
151-
if (err) {
152-
console.log('Failed to compare checksums: ', err);
153-
}
154-
155-
if (!areEqual) {
156-
await cache.set(cacheKey, value, 0);
157-
} else {
158-
console.log('Cache has not changed, not setting cache.');
159-
}
160-
});
161-
}
146+
await cache.set(cacheKey, value, 0);
162147
} catch (err) {
163148
request.log([cacheName, 'error'], `Failed to store in cache: ${err}`);
164149

test/helpers/aws.test.js

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -105,45 +105,4 @@ describe('aws helper test', () => {
105105
done();
106106
});
107107
});
108-
109-
it('returns true if checksum are the same', (done) => {
110-
const headParam = {
111-
Bucket: testBucket,
112-
Key: cacheKey
113-
};
114-
115-
return awsClient.compareChecksum(localCache, cacheKey, (err, checksum) => {
116-
assert.calledWith(clientMock.prototype.headObject, headParam);
117-
assert.isNull(err);
118-
assert.isTrue(checksum);
119-
done();
120-
});
121-
});
122-
123-
it('returns false if checksum are not the same', (done) => {
124-
const headParam = {
125-
Bucket: testBucket,
126-
Key: cacheKey
127-
};
128-
129-
const newLocalCache = 'A DIFFERENT FILE';
130-
131-
return awsClient.compareChecksum(newLocalCache, cacheKey, (err, checksum) => {
132-
assert.calledWith(clientMock.prototype.headObject, headParam);
133-
assert.isNull(err);
134-
assert.isFalse(checksum);
135-
done();
136-
});
137-
});
138-
139-
it('returns err if fails to get headObject', (done) => {
140-
const err = new Error('failed to get headObject');
141-
142-
clientMock.prototype.headObject = sinon.stub().yieldsAsync(err);
143-
144-
return awsClient.compareChecksum(localCache, cacheKey, (e) => {
145-
assert.deepEqual(e, err);
146-
done();
147-
});
148-
});
149108
});

test/plugins/caches.test.js

Lines changed: 24 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ describe('events plugin test', () => {
3030
};
3131

3232
awsClientMock = sinon.stub().returns({
33-
updateLastModified: sinon.stub().yields(null),
34-
compareChecksum: sinon.stub().yields(null, false)
33+
updateLastModified: sinon.stub().yields(null)
3534
});
3635

3736
mockery.registerMock('../helpers/aws', awsClientMock);
@@ -95,6 +94,21 @@ describe('events plugin test', () => {
9594
})
9695
));
9796

97+
it('returns 403 if credentials is not valid', () => (
98+
server.inject({
99+
headers: {
100+
'x-foo': 'bar'
101+
},
102+
credentials: {
103+
eventId: 5555,
104+
scope: ['build']
105+
},
106+
url: `/caches/events/${mockEventID}/foo`
107+
}).then((response) => {
108+
assert.equal(response.statusCode, 403);
109+
})
110+
));
111+
98112
describe('caching is not setup right', () => {
99113
let badServer;
100114

@@ -251,83 +265,6 @@ describe('events plugin test', () => {
251265
assert.equal(getResponse.result, 'THIS IS A TEST');
252266
});
253267
});
254-
255-
describe('local file and remote file are the same', () => {
256-
let cacheConfigMock;
257-
let cacheAwsClientMock;
258-
let cachePlugin;
259-
let cacheServer;
260-
261-
beforeEach(() => {
262-
mockery.deregisterAll();
263-
mockery.resetCache();
264-
265-
cacheConfigMock = {
266-
get: sinon.stub().returns({
267-
plugin: 's3'
268-
})
269-
};
270-
271-
cacheAwsClientMock = sinon.stub().returns({
272-
updateLastModified: sinon.stub().yields(null),
273-
compareChecksum: sinon.stub().yields(null, true)
274-
});
275-
276-
mockery.registerMock('../helpers/aws', cacheAwsClientMock);
277-
mockery.registerMock('config', cacheConfigMock);
278-
279-
// eslint-disable-next-line global-require
280-
cachePlugin = require('../../plugins/caches');
281-
282-
cacheServer = Hapi.server({
283-
cache: {
284-
engine: catmemory,
285-
maxByteSize: 512,
286-
allowMixedContent: true
287-
},
288-
port: 1235
289-
});
290-
291-
cacheServer.auth.scheme('custom', () => ({
292-
authenticate: (request, h) => h.authenticated()
293-
}));
294-
cacheServer.auth.strategy('token', 'custom');
295-
cacheServer.auth.strategy('session', 'custom');
296-
297-
return cacheServer.register({
298-
plugin: cachePlugin,
299-
options: {
300-
expiresInSec: '100',
301-
maxByteSize: '5368709120'
302-
} })
303-
.then(() => cacheServer.start());
304-
});
305-
306-
afterEach(() => {
307-
cacheServer = null;
308-
mockery.deregisterAll();
309-
mockery.resetCache();
310-
});
311-
312-
it('does not save cache if checksums are equal', async () => {
313-
options.url = `/caches/events/${mockEventID}/foo`;
314-
315-
options.headers['content-type'] = 'application/x-ndjson';
316-
const putResponse = await cacheServer.inject(options);
317-
318-
assert.equal(putResponse.statusCode, 202);
319-
320-
return cacheServer.inject({
321-
url: `/caches/events/${mockEventID}/foo`,
322-
credentials: {
323-
eventId: mockEventID,
324-
scope: ['build']
325-
}
326-
}).then((getResponse) => {
327-
assert.equal(getResponse.statusCode, 404);
328-
});
329-
});
330-
});
331268
});
332269

333270
describe('DELETE /caches/events/:id/:cacheName', () => {
@@ -383,6 +320,14 @@ describe('events plugin test', () => {
383320
});
384321
}));
385322

323+
it('returns 403 if credentials is not valid', () => {
324+
deleteOptions.credentials.eventId = 5555;
325+
326+
return server.inject(deleteOptions).then((response) => {
327+
assert.equal(response.statusCode, 403);
328+
});
329+
});
330+
386331
it('deletes an event cache', () => server.inject(putOptions).then((postResponse) => {
387332
assert.equal(postResponse.statusCode, 202);
388333

0 commit comments

Comments
 (0)