summaryrefslogtreecommitdiff
path: root/yarns/900-implements.yarn
blob: 7eea6d8ce29a85ee808a5cb7c00560faf932c9b1 (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
# 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|facade) requests POST (\S+), with form values (\S+)=(\S+) and (\S+)=(\S+)
    who = get_next_match()
    path = get_next_match()
    field1 = get_next_match()
    value1 = get_next_match()
    field2 = get_next_match()
    value2 = 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)

## 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 is (.+)
    header = get_next_match()
    value = expand_vars(get_next_match(), V)
    assertEqual(V['headers'].get(header), value)

    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 Content-Type is (\S+)
    wanted = get_next_match()
    headers = V['headers']
    assertEqual(headers['Content-Type'][:len(wanted)], 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)