/
validateS3BucketName.test.js
114 lines (98 loc) · 3.93 KB
/
validateS3BucketName.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
'use strict';
const expect = require('chai').expect;
const Serverless = require('../../../Serverless');
const validateS3BucketName = require('./validateS3BucketName');
describe('#validateS3BucketName()', () => {
const serverless = new Serverless();
const awsPlugin = {};
beforeEach(() => {
awsPlugin.serverless = serverless;
awsPlugin.options = {
stage: 'dev',
region: 'us-east-1',
};
awsPlugin.serverless.config.servicePath = true;
Object.assign(awsPlugin, validateS3BucketName);
});
describe('#validateS3BucketName()', () => {
it('should reject an ip address as a name', () =>
awsPlugin
.validateS3BucketName('127.0.0.1')
.then(() => {
throw new Error('Should not get here');
})
.catch(err => expect(err.message).to.contain('cannot look like an IPv4 address')));
it('should reject names that are too long', () => {
const bucketName = Array.from({ length: 64 }, () => 'j').join('');
return awsPlugin
.validateS3BucketName(bucketName)
.then(() => {
throw new Error('Should not get here');
})
.catch(err => expect(err.message).to.contain('longer than 63 characters'));
});
it('should reject names that are too short', () =>
awsPlugin
.validateS3BucketName('12')
.then(() => {
throw new Error('Should not get here');
})
.catch(err => expect(err.message).to.contain('shorter than 3 characters')));
it('should reject names that contain invalid characters', () =>
awsPlugin
.validateS3BucketName('this has b@d characters')
.then(() => {
throw new Error('Should not get here');
})
.catch(err => expect(err.message).to.contain('contains invalid characters')));
it('should reject names that have consecutive periods', () =>
awsPlugin
.validateS3BucketName('otherwise..valid.name')
.then(() => {
throw new Error('Should not get here');
})
.catch(err => expect(err.message).to.contain('cannot contain consecutive periods')));
it('should reject names that start with a dash', () =>
awsPlugin
.validateS3BucketName('-invalid.name')
.then(() => {
throw new Error('Should not get here');
})
.catch(err => expect(err.message).to.contain('start with a letter or number')));
it('should reject names that start with a period', () =>
awsPlugin
.validateS3BucketName('.invalid.name')
.then(() => {
throw new Error('Should not get here');
})
.catch(err => expect(err.message).to.contain('start with a letter or number')));
it('should reject names that end with a dash', () =>
awsPlugin
.validateS3BucketName('invalid.name-')
.then(() => {
throw new Error('Should not get here');
})
.catch(err => expect(err.message).to.contain('end with a letter or number')));
it('should reject names that end with a period', () =>
awsPlugin
.validateS3BucketName('invalid.name.')
.then(() => {
throw new Error('Should not get here');
})
.catch(err => expect(err.message).to.contain('end with a letter or number')));
it('should reject names that contain uppercase letters', () =>
awsPlugin
.validateS3BucketName('otherwise.Valid.name')
.then(() => {
throw new Error('Should not get here');
})
.catch(err => expect(err.message).to.contain('cannot contain uppercase letters')));
it('should accept valid names', () =>
awsPlugin
.validateS3BucketName('1.this.is.valid.2')
.then(() => awsPlugin.validateS3BucketName('another.valid.name'))
.then(() => awsPlugin.validateS3BucketName('1-2-3'))
.then(() => awsPlugin.validateS3BucketName('123'))
.then(() => awsPlugin.validateS3BucketName('should.be.allowed-to-mix')));
});
});