-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathvalidate.test.js
173 lines (142 loc) · 5.42 KB
/
validate.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict';
const sinon = require('sinon');
const BbPromise = require('bluebird');
const validate = require('./validate');
const GoogleProvider = require('../provider/googleProvider');
const Serverless = require('../test/serverless');
const GoogleCommand = require('../test/googleCommand');
describe('Validate', () => {
let serverless;
let googleCommand;
beforeEach(() => {
serverless = new Serverless();
serverless.config = {
servicePath: true,
};
serverless.service = {
service: 'some-default-service',
};
serverless.setProvider('google', new GoogleProvider(serverless));
googleCommand = new GoogleCommand(serverless, {}, validate);
});
describe('#validate()', () => {
let validateServicePathStub;
let validateServiceNameStub;
let validateHandlersStub;
beforeEach(() => {
validateServicePathStub = sinon
.stub(googleCommand, 'validateServicePath')
.returns(BbPromise.resolve());
validateServiceNameStub = sinon
.stub(googleCommand, 'validateServiceName')
.returns(BbPromise.resolve());
validateHandlersStub = sinon
.stub(googleCommand, 'validateHandlers')
.returns(BbPromise.resolve());
});
afterEach(() => {
googleCommand.validateServicePath.restore();
googleCommand.validateServiceName.restore();
googleCommand.validateHandlers.restore();
});
it('should run promise chain', () =>
googleCommand.validate().then(() => {
expect(validateServicePathStub.calledOnce).toEqual(true);
expect(validateServiceNameStub.calledAfter(validateServicePathStub));
expect(validateHandlersStub.calledAfter(validateServiceNameStub));
}));
});
describe('#validateServicePath()', () => {
it('should throw an error if not inside service', () => {
serverless.config.servicePath = false;
expect(() => googleCommand.validateServicePath()).toThrow(Error);
});
it('should not throw an error if inside service', () => {
serverless.config.servicePath = true;
expect(() => googleCommand.validateServicePath()).not.toThrow(Error);
});
});
describe('#validateServiceName()', () => {
it('should throw an error if the service name contains the string "goog"', () => {
serverless.service.service = 'service-name-with-goog-in-it';
expect(() => googleCommand.validateServiceName()).toThrow(Error);
});
it('should throw an error if the service name contains underscores', () => {
serverless.service.service = 'service_name';
expect(() => googleCommand.validateServiceName()).toThrow(Error);
});
it('should not throw an error if the service name is valid', () => {
serverless.service.service = 'service-name';
expect(() => googleCommand.validateServiceName()).not.toThrow(Error);
});
});
describe('#validateHandlers()', () => {
it('should throw an error if the handler name contains an invalid character', () => {
googleCommand.serverless.service.functions = {
foo: {
handler: 'invalid.handler',
},
bar: {
handler: 'invalid/handler',
},
};
expect(() => googleCommand.validateHandlers()).toThrow(Error);
});
it('should not throw an error if the function handler is valid', () => {
googleCommand.serverless.service.functions = {
foo: {
handler: 'validHandler',
},
};
expect(() => googleCommand.validateHandlers()).not.toThrow(Error);
});
});
describe('#validateEventsProperty()', () => {
const functionName = 'functionName';
const eventEvent = {
event: {},
};
const httpEvent = {
http: {},
};
const unknownEvent = {
unknown: {},
};
it('should throw if the configuration of function has no events', () => {
expect(() => validate.validateEventsProperty({}, functionName)).toThrow();
});
it('should throw if the configuration of function has an empty events array', () => {
expect(() => validate.validateEventsProperty({ events: [] }, functionName)).toThrow();
});
it('should throw if the configuration of function has more than one events', () => {
expect(() =>
validate.validateEventsProperty({ events: [eventEvent, httpEvent] }, functionName)
).toThrow();
});
it('should throw if the configuration of function has an unknown event', () => {
expect(() =>
validate.validateEventsProperty({ events: [unknownEvent] }, functionName)
).toThrow();
});
it('should pass if the configuration of function has an http event', () => {
expect(() =>
validate.validateEventsProperty({ events: [httpEvent] }, functionName)
).not.toThrow();
});
it('should pass if the configuration of function has an event event', () => {
expect(() =>
validate.validateEventsProperty({ events: [eventEvent] }, functionName)
).not.toThrow();
});
it('should throw if the configuration of function has an http event but http event is not supported', () => {
expect(() =>
validate.validateEventsProperty({ events: [unknownEvent] }, functionName, ['event'])
).toThrow();
});
it('should pass if the configuration of function has an event event and event is supported', () => {
expect(() =>
validate.validateEventsProperty({ events: [eventEvent] }, functionName, ['event'])
).not.toThrow();
});
});
});