1 | 'use strict';
|
2 |
|
3 | const S3Inventory = require('../functions/s3-inventory');
|
4 | const test = require('tape');
|
5 | const AWS = require('@mapbox/mock-aws-sdk-js');
|
6 |
|
7 | test('[s3-inventory] constructor', (assert) => {
|
8 | assert.throws(() => new S3Inventory(), /Missing Parameter/, 'throws with no bucket');
|
9 | assert.throws(() => new S3Inventory('bucket'), /Missing Parameter/, 'throws with no region');
|
10 | assert.throws(() => new S3Inventory('bucket', 'us-east-1'), /Missing Parameter/, 'throws with no id');
|
11 | assert.throws(() => new S3Inventory('bucket', 'us-east-1', 'id'), /Missing Parameter/, 'throws with no config');
|
12 |
|
13 | AWS.stub('S3', 'getObject');
|
14 |
|
15 | const newInv = new S3Inventory('bucket', 'us-east-1', 'id', {});
|
16 | const updateInv = new S3Inventory('bucket', 'us-east-1', 'id', {}, 'old');
|
17 | const updateSame = new S3Inventory('bucket', 'us-east-1', 'id', {}, 'id');
|
18 |
|
19 | assert.ok(AWS.S3.calledWith({ region: 'us-east-1' }), 'created s3 client in the right region');
|
20 | assert.deepEqual(newInv.params, {
|
21 | Bucket: 'bucket',
|
22 | Id: 'id',
|
23 | InventoryConfiguration: { IsEnabled: false }
|
24 | }, 'sets properties for create');
|
25 |
|
26 | assert.equal(updateInv.oldId, 'old', 'update sets oldId property when intention is to create a new inventory');
|
27 | assert.notOk(updateSame.oldId, 'no oldId set when updating an inventory in place');
|
28 |
|
29 | AWS.S3.restore();
|
30 | assert.end();
|
31 | });
|
32 |
|
33 | test('[s3-inventory] create', (assert) => {
|
34 | const put = AWS.stub('S3', 'putBucketInventoryConfiguration').yields();
|
35 | const newInv = new S3Inventory('bucket', 'us-east-1', 'id', {});
|
36 |
|
37 | newInv.create((err, id) => {
|
38 | assert.ifError(err, 'test failed');
|
39 |
|
40 | assert.equal(id, 'id', 'returns id');
|
41 |
|
42 | assert.ok(put.calledWith({
|
43 | Bucket: 'bucket', Id: 'id', InventoryConfiguration: { IsEnabled: false }
|
44 | }), 'called API with expected parameters');
|
45 |
|
46 | AWS.S3.restore();
|
47 | assert.end();
|
48 | });
|
49 | });
|
50 |
|
51 | test('[s3-inventory] update', (assert) => {
|
52 | const put = AWS.stub('S3', 'putBucketInventoryConfiguration').yields();
|
53 | const del = AWS.stub('S3', 'deleteBucketInventoryConfiguration').yields();
|
54 | const updateInv = new S3Inventory('bucket', 'us-east-1', 'id', {}, 'old');
|
55 |
|
56 | updateInv.update((err, id) => {
|
57 | assert.ifError(err, 'test failed');
|
58 |
|
59 | assert.equal(id, 'id', 'returns new id');
|
60 |
|
61 | assert.ok(put.calledWith({
|
62 | Bucket: 'bucket', Id: 'id', InventoryConfiguration: { IsEnabled: false }
|
63 | }), 'called put API with expected parameters');
|
64 |
|
65 | assert.ok(del.calledWith({
|
66 | Bucket: 'bucket', Id: 'old'
|
67 | }), 'called delete API with expected parameters');
|
68 |
|
69 | AWS.S3.restore();
|
70 | assert.end();
|
71 | });
|
72 | });
|
73 |
|
74 | test('[s3-inventory] delete', (assert) => {
|
75 | const del = AWS.stub('S3', 'deleteBucketInventoryConfiguration').yields();
|
76 | const delInv = new S3Inventory('bucket', 'us-east-1', 'id', {}, 'id');
|
77 |
|
78 | delInv.delete((err) => {
|
79 | assert.ifError(err, 'test failed');
|
80 |
|
81 | assert.ok(del.calledWith({
|
82 | Bucket: 'bucket', Id: 'id'
|
83 | }), 'called delete API with expected parameters');
|
84 |
|
85 | AWS.S3.restore();
|
86 | assert.end();
|
87 | });
|
88 | });
|