|
| 1 | +var helpers = require('./helpers') |
| 2 | + |
| 3 | +var target = 'UpdateTimeToLive', |
| 4 | + request = helpers.request, |
| 5 | + opts = helpers.opts.bind(null, target), |
| 6 | + assertType = helpers.assertType.bind(null, target), |
| 7 | + assertValidation = helpers.assertValidation.bind(null, target), |
| 8 | + assertNotFound = helpers.assertNotFound.bind(null, target) |
| 9 | + |
| 10 | +describe('updateTimeToLive', function() { |
| 11 | + |
| 12 | + describe('serializations', function() { |
| 13 | + |
| 14 | + it('should return SerializationException when TableName is not a string', function(done) { |
| 15 | + assertType('TableName', 'String', done) |
| 16 | + }) |
| 17 | + |
| 18 | + it('should return SerializationException when TimeToLiveSpecification is not a struct', function(done) { |
| 19 | + assertType('TimeToLiveSpecification', 'FieldStruct<TimeToLiveSpecification>', done) |
| 20 | + }) |
| 21 | + |
| 22 | + it('should return SerializationException when TimeToLiveSpecification.AttributeName is not a string', function(done) { |
| 23 | + assertType('TimeToLiveSpecification.AttributeName', 'String', done) |
| 24 | + }) |
| 25 | + |
| 26 | + it('should return SerializationException when TimeToLiveSpecification.Enabled is not a boolean', function(done) { |
| 27 | + assertType('TimeToLiveSpecification.Enabled', 'Boolean', done) |
| 28 | + }) |
| 29 | + |
| 30 | + }) |
| 31 | + |
| 32 | + describe('validations', function() { |
| 33 | + |
| 34 | + it('should return ValidationException for no TableName', function(done) { |
| 35 | + assertValidation({}, |
| 36 | + 'The parameter \'TableName\' is required but was not present in the request', done) |
| 37 | + }) |
| 38 | + |
| 39 | + it('should return ValidationException for empty TableName', function(done) { |
| 40 | + assertValidation({TableName: ''}, |
| 41 | + 'TableName must be at least 3 characters long and at most 255 characters long', done) |
| 42 | + }) |
| 43 | + |
| 44 | + it('should return ValidationException for short TableName', function(done) { |
| 45 | + assertValidation({TableName: 'a;'}, |
| 46 | + 'TableName must be at least 3 characters long and at most 255 characters long', done) |
| 47 | + }) |
| 48 | + |
| 49 | + it('should return ValidationException for long TableName', function(done) { |
| 50 | + var name = new Array(256 + 1).join('a') |
| 51 | + assertValidation({TableName: name}, |
| 52 | + 'TableName must be at least 3 characters long and at most 255 characters long', done) |
| 53 | + }) |
| 54 | + |
| 55 | + it('should return ValidationException for invalid chars', function(done) { |
| 56 | + assertValidation({TableName: 'abc;'}, |
| 57 | + '1 validation error detected: ' + |
| 58 | + 'Value \'abc;\' at \'tableName\' failed to satisfy constraint: ' + |
| 59 | + 'Member must satisfy regular expression pattern: [a-zA-Z0-9_.-]+', done) |
| 60 | + }) |
| 61 | + |
| 62 | + it('should return ValidationException for empty TimeToLiveSpecification', function(done) { |
| 63 | + assertValidation({TableName: 'abc', TimeToLiveSpecification: {}}, [ |
| 64 | + 'Value null at \'timeToLiveSpecification.enabled\' failed to satisfy constraint: ' + |
| 65 | + 'Member must not be null', |
| 66 | + 'Value null at \'timeToLiveSpecification.attributeName\' failed to satisfy constraint: ' + |
| 67 | + 'Member must not be null', |
| 68 | + ], done) |
| 69 | + }) |
| 70 | + |
| 71 | + it('should return ValidationException for null members in TimeToLiveSpecification', function(done) { |
| 72 | + assertValidation({TableName: 'abc', TimeToLiveSpecification: {AttributeName: null, Enabled: null}}, [ |
| 73 | + 'Value null at \'timeToLiveSpecification.attributeName\' failed to satisfy constraint: ' + |
| 74 | + 'Member must not be null', |
| 75 | + 'Value null at \'timeToLiveSpecification.enabled\' failed to satisfy constraint: ' + |
| 76 | + 'Member must not be null', |
| 77 | + ], done) |
| 78 | + }) |
| 79 | + |
| 80 | + it('should return ValidationException for empty TimeToLiveSpecification.AttributeName', function(done) { |
| 81 | + assertValidation({TableName: 'abc', |
| 82 | + TimeToLiveSpecification: {AttributeName: "", Enabled: true}}, |
| 83 | + 'TimeToLiveSpecification.AttributeName must be non empty', done) |
| 84 | + }) |
| 85 | + |
| 86 | + it('should return ResourceNotFoundException if table does not exist', function(done) { |
| 87 | + var name = helpers.randomString() |
| 88 | + assertNotFound({TableName: name, |
| 89 | + TimeToLiveSpecification: {AttributeName: "id", Enabled: true}}, |
| 90 | + 'Requested resource not found: Table: ' + name + ' not found', done) |
| 91 | + }) |
| 92 | + |
| 93 | + it('should return ValidationException for false TimeToLiveSpecification.Enabled when already disabled', function(done) { |
| 94 | + assertValidation({TableName: helpers.testHashTable, |
| 95 | + TimeToLiveSpecification: {AttributeName: "a", Enabled: false}}, |
| 96 | + 'TimeToLive is already disabled', done) |
| 97 | + }) |
| 98 | + |
| 99 | + it('should return ValidationException for true TimeToLiveSpecification.Enabled when already enabled', function(done) { |
| 100 | + request(opts({TableName: helpers.testHashTable, TimeToLiveSpecification: {AttributeName: "a", Enabled: true}}), function(err, res) { |
| 101 | + if (err) return done(err) |
| 102 | + res.statusCode.should.equal(200) |
| 103 | + |
| 104 | + assertValidation({TableName: helpers.testHashTable, |
| 105 | + TimeToLiveSpecification: {AttributeName: "a", Enabled: true}}, |
| 106 | + 'TimeToLive is already enabled', function(err){ |
| 107 | + if (err) return done(err) |
| 108 | + // teardown |
| 109 | + request(opts({TableName: helpers.testHashTable, TimeToLiveSpecification: {AttributeName: "a", Enabled: false}}), function(err, res) { |
| 110 | + if (err) return done(err) |
| 111 | + res.statusCode.should.equal(200) |
| 112 | + done() |
| 113 | + }) |
| 114 | + }) |
| 115 | + }) |
| 116 | + }) |
| 117 | + }) |
| 118 | + |
| 119 | + describe('functionality', function() { |
| 120 | + it('should enable when disabled', function(done) { |
| 121 | + request(opts({TableName: helpers.testHashTable, TimeToLiveSpecification: {AttributeName: "a", Enabled: true}}), function(err, res) { |
| 122 | + if (err) return done(err) |
| 123 | + res.statusCode.should.equal(200) |
| 124 | + res.body.should.eql({TimeToLiveSpecification: {AttributeName: "a", Enabled: true}}) |
| 125 | + |
| 126 | + request(helpers.opts('DescribeTimeToLive', {TableName: helpers.testHashTable}), function(err, res) { |
| 127 | + if (err) return done(err) |
| 128 | + res.statusCode.should.equal(200) |
| 129 | + res.body.should.eql({TimeToLiveDescription: {TimeToLiveStatus: "ENABLED", AttributeName: "a"}}) |
| 130 | + |
| 131 | + // teardown |
| 132 | + request(opts({TableName: helpers.testHashTable, TimeToLiveSpecification: {AttributeName: "a", Enabled: false}}), function(err, res) { |
| 133 | + if (err) return done(err) |
| 134 | + res.statusCode.should.equal(200) |
| 135 | + done() |
| 136 | + }) |
| 137 | + }) |
| 138 | + }) |
| 139 | + }) |
| 140 | + |
| 141 | + it('should disable when enabled', function(done) { |
| 142 | + request(opts({TableName: helpers.testHashTable, TimeToLiveSpecification: {AttributeName: "a", Enabled: true}}), function(err, res) { |
| 143 | + if (err) return done(err) |
| 144 | + res.statusCode.should.equal(200) |
| 145 | + res.body.should.eql({TimeToLiveSpecification: {AttributeName: "a", Enabled: true}}) |
| 146 | + |
| 147 | + request(opts({TableName: helpers.testHashTable, TimeToLiveSpecification: {AttributeName: "a", Enabled: false}}), function(err, res) { |
| 148 | + if (err) return done(err) |
| 149 | + res.statusCode.should.equal(200) |
| 150 | + res.body.should.eql({TimeToLiveSpecification: {Enabled: false}}) |
| 151 | + |
| 152 | + |
| 153 | + request(helpers.opts('DescribeTimeToLive', {TableName: helpers.testHashTable}), function(err, res) { |
| 154 | + if (err) return done(err) |
| 155 | + res.statusCode.should.equal(200) |
| 156 | + res.body.should.eql({TimeToLiveDescription: {TimeToLiveStatus: "DISABLED"}}) |
| 157 | + done() |
| 158 | + }) |
| 159 | + }) |
| 160 | + }) |
| 161 | + }) |
| 162 | + |
| 163 | + it('should delete the expired items when TTL is enabled', function(done) { |
| 164 | + request(opts({TableName: helpers.testHashTable, TimeToLiveSpecification: {AttributeName: "TTL", Enabled: true}}), function(err, res) { |
| 165 | + if (err) return done(err) |
| 166 | + res.statusCode.should.equal(200) |
| 167 | + res.body.should.eql({TimeToLiveSpecification: {AttributeName: "TTL", Enabled: true}}) |
| 168 | + |
| 169 | + var timestampOneSecondLater = Math.round(Date.now() / 1000) + 1; |
| 170 | + var item = { |
| 171 | + a: {S: helpers.randomString()}, |
| 172 | + TTL: {N: timestampOneSecondLater.toString()}, |
| 173 | + } |
| 174 | + |
| 175 | + request(helpers.opts('PutItem', {TableName: helpers.testHashTable, Item: item}), function(err, res) { |
| 176 | + if (err) return done(err) |
| 177 | + res.statusCode.should.equal(200) |
| 178 | + |
| 179 | + setTimeout(function(){ |
| 180 | + request(helpers.opts('GetItem', {TableName: helpers.testHashTable, Key: { a: item.a }}), function(err, res) { |
| 181 | + if (err) return done(err) |
| 182 | + res.statusCode.should.equal(200) |
| 183 | + // Item should be deleted |
| 184 | + res.body.should.eql({}) |
| 185 | + |
| 186 | + // teardown |
| 187 | + request(opts({TableName: helpers.testHashTable, TimeToLiveSpecification: {AttributeName: "TTL", Enabled: false}}), function(err, res) { |
| 188 | + if (err) return done(err) |
| 189 | + res.statusCode.should.equal(200) |
| 190 | + done() |
| 191 | + }) |
| 192 | + }) |
| 193 | + }, 3000) |
| 194 | + }) |
| 195 | + }) |
| 196 | + }) |
| 197 | + }) |
| 198 | +}) |
0 commit comments