60 lines
3.0 KiB
Python
60 lines
3.0 KiB
Python
#!/usr/bin/env python3
|
|
"""HTTP regressions: exact JSON fields and explicitly empty pagination values."""
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / 'tests'))
|
|
from client import Client, arguments, check
|
|
|
|
|
|
def main():
|
|
args = arguments('Strict JSON and pagination regression checks')
|
|
c = Client(args.base_url, args.ca_file)
|
|
alice, bob, eve, chat = c.bootstrap()
|
|
path = '/api/v1/chats/'+chat['id']+'/messages'
|
|
cases = []
|
|
|
|
def invalid(method, url, **kwargs):
|
|
response = c.call(method, url, 400, actor=alice, **kwargs)
|
|
check(response['json']['error']['code']=='invalid_request', 'expected invalid_request')
|
|
cases.append({'method': method, 'path': url, 'status': 400, 'code': 'invalid_request'})
|
|
|
|
c.message(alice, chat, 'confirmed before invalid requests')
|
|
before = c.call('GET', path, actor=bob, schema='MessagePage')['json']
|
|
for body in [
|
|
{'text': 'valid', 'TEXT': 'unexpected-property'},
|
|
{'TEXT': 'unexpected-property', 'text': 'valid'},
|
|
{'Text': 'unexpected-property'},
|
|
]:
|
|
invalid('POST', path, body=body)
|
|
invalid('POST', path, raw=b'{"text":"valid","text":"overwrite"}',
|
|
headers={'Content-Type': 'application/json'})
|
|
after = c.call('GET', path, actor=bob, schema='MessagePage')['json']
|
|
check(after==before, 'rejected JSON modified message history')
|
|
|
|
invalid('POST', '/api/v1/users', body=dict(alice['registration'], PASSWORD='unexpected-property'))
|
|
invalid('POST', '/api/v1/chats', body={'title':'valid', 'TITLE':'unexpected-property', 'member_ids':[alice['id']]})
|
|
for resource in [path, '/api/v1/chats']:
|
|
for query in ['limit=', 'cursor=', 'limit', 'cursor', 'limit=1&cursor=', 'limit=1&limit=2', 'cursor=&cursor=']:
|
|
invalid('GET', resource+'?'+query)
|
|
# Omitting both parameters and setting a valid limit must still work.
|
|
for query in ['', '?limit=1']:
|
|
page = c.call('GET', path+query, actor=alice, schema='MessagePage')['json']
|
|
check(len(page['items'])==1, 'valid pagination lost confirmed message')
|
|
c.message(alice, chat, 'second confirmed message')
|
|
first = c.call('GET', path+'?limit=1', actor=alice, schema='MessagePage')['json']
|
|
check(first['next_cursor'] is not None, 'expected continuation cursor')
|
|
second = c.call('GET', path+'?limit=1&cursor='+first['next_cursor'], actor=bob, schema='MessagePage')['json']
|
|
check(len(second['items'])==1 and second['next_cursor'] is None, 'valid continuation failed')
|
|
check(first['items'][0]['id']!=second['items'][0]['id'], 'duplicate page item')
|
|
c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=alice)
|
|
result = {'cases':cases, 'rejected_posts_leave_history_unchanged':True, 'valid_pagination':'PASS'}
|
|
(ROOT/'evidence'/'regression.json').write_text(json.dumps(result,ensure_ascii=False,indent=2)+'\n',encoding='utf-8')
|
|
print('PASS: exact JSON fields, no rejected writes, empty/repeated pagination rejected, valid pages preserved')
|
|
|
|
|
|
if __name__=='__main__':
|
|
main()
|