summaryrefslogtreecommitdiff
path: root/yarns/900-implements.yarn
blob: d63472c3a25311208cd77cf888af6384769cb352 (plain)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# Step implementations

This chapter shows the scenario step implementations.

## API requests of various kinds

    IMPLEMENTS WHEN client requests GET (/.+) without token
    path = get_next_match()
    path = expand_vars(path, V)
    V['status_code'], V['headers'], V['body'] = get(V['API_URL'] + path)

    IMPLEMENTS WHEN client requests GET (/.+) using token
    path = get_next_match()
    path = expand_vars(path, V)
    headers = {
        'Authorization': 'Bearer {}'.format(V['token']),
    }
    V['status_code'], V['headers'], V['body'] = get(
        V['API_URL'] + path, headers)

    IMPLEMENTS WHEN client requests POST (/.+) with token and body (.+)
    path = get_next_match()
    body = get_next_match()
    headers = {
        'Authorization': 'Bearer {}'.format(V['token']),
        'Content-Type': 'application/json',
    }
    V['status_code'], V['headers'], V['body'] = post(
        V['API_URL'] + path, headers=headers, body=body)

    IMPLEMENTS WHEN client requests POST /token with client_id "(.+)", client_secret "(.+)", and scopes "(.+)"
    client_id = get_next_match()
    client_secret = get_next_match()
    scopes = get_next_match().split()
    V['status_code'], V['headers'], V['body'] = get_token(
        client_id, client_secret, scopes)

    IMPLEMENTS WHEN client requests PUT (/.+) with token and body (.+)
    path = get_next_match()
    path = expand_vars(path, V)
    body = get_next_match()
    body = expand_vars(body, V)
    headers = {
        'Authorization': 'Bearer {}'.format(V['token']),
        'Content-Type': 'application/json',
    }
    V['status_code'], V['headers'], V['body'] = put(
        V['API_URL'] + path, headers=headers, body=body)

    IMPLEMENTS WHEN client requests PUT (/[a-z0-9/${}]+) with token, revision (\S+), content-type (\S+), and empty body
    path = expand_vars(get_next_match(), V)
    revision = expand_vars(get_next_match(), V)
    ctype = expand_vars(get_next_match(), V)
    body = ''
    headers = {
        'Authorization': 'Bearer {}'.format(V['token']),
        'Revision': revision,
        'Content-Type': ctype,
    }
    V['status_code'], V['headers'], V['body'] = put(
        V['API_URL'] + path, headers=headers, body=body)

    IMPLEMENTS WHEN client requests PUT (/[a-z0-9/${}]+) with token, revision (\S+), content-type (\S+), and body "(.+)"
    path = expand_vars(get_next_match(), V)
    revision = expand_vars(get_next_match(), V)
    ctype = expand_vars(get_next_match(), V)
    body = unescape(expand_vars(get_next_match(), V))
    headers = {
        'Authorization': 'Bearer {}'.format(V['token']),
        'Revision': revision,
        'Content-Type': ctype,
    }
    V['status_code'], V['headers'], V['body'] = put(
        V['API_URL'] + path, headers=headers, body=body)

    IMPLEMENTS WHEN client requests DELETE (/.+) with token
    path = get_next_match()
    path = expand_vars(path, V)
    headers = {
        'Authorization': 'Bearer {}'.format(V['token']),
    }
    V['status_code'], V['headers'], V['body'] = delete(
        V['API_URL'] + path, headers=headers)

    IMPLEMENTS WHEN browser requests GET (\S+)
    path = get_next_match()
    V['status_code'], V['headers'], V['body'] = get(V['API_URL'] + path, {})

    IMPLEMENTS WHEN browser requests POST (\S+), with form values (\S+)=(\S+) and (\S+)=(\S+) and (\S+)=(\S+)
    path = get_next_match()
    field1 = get_next_match()
    value1 = expand_vars(get_next_match(), V)
    field2 = get_next_match()
    value2 = expand_vars(get_next_match(), V)
    field3 = get_next_match()
    value3 = expand_vars(get_next_match(), V)
    headers = {}
    body = {
        field1: value1,
        field2: value2,
        field3: value3,
    }
    V['status_code'], V['headers'], V['body'] = post(
        V['API_URL'] + path, headers=headers, body=body)

    IMPLEMENTS WHEN facade requests POST (\S+), with form values (\S+)=(\S+) and (\S+)=(\S+) using Basic Auth with username (\S+), password (\S+)
    path = get_next_match()
    field1 = get_next_match()
    value1 = expand_vars(get_next_match(), V)
    field2 = get_next_match()
    value2 = expand_vars(get_next_match(), V)
    username = get_next_match()
    password = get_next_match()
    headers = {}
    body = {
        field1: value1,
        field2: value2,
    }
    V['status_code'], V['headers'], V['body'] = post(
        V['API_URL'] + path, headers=headers, body=body,
        auth=(username, password))

## API access token creation

    IMPLEMENTS WHEN client gets an authorization token with scope "(.+)"
    scopes = get_next_match()
    print 'privkey', repr(V['privkey'])
    assert V['privkey']
    V['token'] = create_token(V['privkey'], V['iss'], V['aud'], scopes)

## UUID creation

    IMPLEMENTS GIVEN unique random identifier (\S+)
    import uuid
    name = get_next_match()
    V[name] = str(uuid.uuid4())

## API request result checking

    IMPLEMENTS THEN HTTP status code is (\d+) (.*)
    expected = int(get_next_match())
    assertEqual(V['status_code'], expected)

    IMPLEMENTS THEN HTTP (\S+) header starts with (.+)
    header = get_next_match()
    wanted = expand_vars(get_next_match(), V)
    actual = V['headers'].get(header)
    assertTrue(actual.startswith(wanted))

    IMPLEMENTS THEN HTTP (\S+) header is saved as (.+)
    header = get_next_match()
    name = get_next_match()
    V[name] = V['headers'].get(header)

    IMPLEMENTS THEN authorization code from (\S+) is saved as (\S+)
    import urlparse
    var1 = get_next_match()
    var2 = get_next_match()
    parts = urlparse.urlparse(V[var1])
    params = urlparse.parse_qs(parts.query)
    V[var2] = params['code'][0]

    IMPLEMENTS THEN remember HTTP (\S+) header as (.+)
    header = get_next_match()
    name = get_next_match()
    V[name] = V['headers'].get(header)

    IMPLEMENTS THEN resource id is (\S+)
    import json
    name = get_next_match()
    print 'body:', repr(V['body'])
    body = json.loads(V['body'])
    V[name] = body['id']

    IMPLEMENTS THEN revision is (\S+)
    import json
    name = get_next_match()
    body = json.loads(V['body'])
    V[name] = body['revision']

    IMPLEMENTS THEN revisions (\S+) and (\S+) are different
    rev1 = get_next_match()
    rev2 = get_next_match()
    assertNotEqual(V[rev1], V[rev2])

    IMPLEMENTS THEN revisions (\S+) and (\S+) match
    rev1 = get_next_match()
    rev2 = get_next_match()
    assertEqual(V[rev1], V[rev2])

    IMPLEMENTS THEN JSON body matches (.+)
    import json
    wanted = get_next_match()
    print 'wanted1', repr(wanted)
    wanted = expand_vars(wanted, V)
    print 'wanted2', repr(wanted)
    wanted = json.loads(wanted)
    actual = json.loads(V['body'])
    print 'actual ', repr(actual)
    print 'wanted3', repr(wanted)
    assertTrue(values_match(wanted, actual))

    IMPLEMENTS THEN body is "(.+)"
    wanted = unescape(expand_vars(get_next_match(), V))
    body = V['body']
    assertTrue(values_match(wanted, body))

    IMPLEMENTS THEN body has an HTML form with field (.+)
    field = get_next_match()
    body = V['body']
    pattern = '<input name="{}"'.format(field)
    assertTrue(pattern in body)

    IMPLEMENTS THEN HTML form field (.+) is saved as (\S+)
    import re
    field = get_next_match()
    name = get_next_match()
    body = V['body']
    pattern = '<input name="{}" value="([^"]+)"'.format(field)
    m = re.search(pattern, body, re.M)
    print('body', repr(body))
    print('pattern:', pattern)
    print('m:', m)
    print('m.groups():', m.groups())
    V[name] = m.groups(0)[0]

    IMPLEMENTS THEN Content-Type is (\S+)
    wanted = get_next_match()
    headers = V['headers']
    assertEqual(headers['Content-Type'][:len(wanted)], wanted)

    IMPLEMENTS THEN Location is (\S+)
    wanted = get_next_match()
    headers = V['headers']
    assertEqual(headers['Location'], wanted)

    IMPLEMENTS THEN body is a correctly signed JWT token
    resp = json.loads(V['body'])
    assertIn('access_token', resp)
    assertIn('token_type', resp)
    assertEqual(resp['token_type'], 'Bearer')
    token = resp['access_token']
    claims = token_decode(token, V['pubkey'])
    assertNotEqual(claims, None)
    V['claims'] = claims

    IMPLEMENTS THEN token has claim (\S+) as "(.*)"
    claim = get_next_match()
    value = get_next_match()
    claims = V['claims']
    assertEqual(claims[claim], value)

    IMPLEMENTS THEN token expires in an hour
    claims = V['claims']
    expires = claims['exp']
    remains = expires - time.time()
    assertTrue(3400 < remains < 3700)

    IMPLEMENTS THEN JSON body has field (\S+)$
    field = get_next_match()
    body = V['body']
    body = json.loads(body)
    assertTrue(field in body)

    IMPLEMENTS THEN JSON body has field (\S+), with value (\S+)
    field = get_next_match()
    value = get_next_match()
    body = V['body']
    body = json.loads(body)
    assertEqual(body.get(field), value)

    IMPLEMENTS THEN access token has a (\S+) field set to (\S+)
    field = get_next_match()
    value = get_next_match()
    body = V['body']
    body = json.loads(body)
    token = body['access_token']
    claims = token_decode(token, V['pubkey'])
    print('claims', claims)
    print('value', value)
    assertEqual(claims.get(field), value)

    IMPLEMENTS THEN access token has an (\S+) field that is not empty
    field = get_next_match()
    value = get_next_match()
    body = V['body']
    body = json.loads(body)
    token = body['access_token']
    claims = token_decode(token)
    tf = token.get(field)
    assertTrue(tf is not None)
    assertTrue(isinstance(tf, str))
    assertTrue(tf != "")