#!/usr/bin/env python3 """Walk multi-page history, verify no loss/dup/reorder, and limit change.""" import uuid from _bootstrap import TESTS_DIR # noqa: F401 from client import Client, arguments, check, run, timestamp def main(): args = arguments('Student pagination tests') c = Client(args.base_url, args.ca_file) alice, bob, eve, chat = c.bootstrap() path = '/api/v1/chats/' + chat['id'] # Create 25 messages with different Unicode payloads. created = [] for i in range(25): created.append(c.message(alice, chat, 'строка-%02d-😀' % i)['json']) created.sort(key=lambda m: (timestamp(m['created_at']), uuid.UUID(m['id']).int)) # Walk with limit=7 items = [] cursor = None pages = 0 while True: q = path + '/messages?limit=7' if cursor: from urllib.parse import quote q += '&cursor=' + quote(cursor, safe='') page = c.call('GET', q, actor=bob, schema='MessagePage')['json'] items.extend(page['items']) pages += 1 if page['next_cursor'] is None: break cursor = page['next_cursor'] check(pages < 20, 'too many pages') check(items == created, 'pagination lost/duplicated/reordered messages') check(pages == 4, 'expected 4 pages, got %d' % pages) # Change limit on continuation first = c.call('GET', path + '/messages?limit=3', actor=alice, schema='MessagePage')['json'] from urllib.parse import quote second = c.call('GET', path + '/messages?limit=10&cursor=' + quote(first['next_cursor'], safe=''), actor=alice, schema='MessagePage')['json'] combined = first['items'] + second['items'] check(combined == created[:13], 'limit change broke the cursor') # Cursor from another chat must be rejected with 400 other = c.call('POST', '/api/v1/chats', 201, actor=alice, body={'title': 'Second', 'member_ids': [alice['id']]}, schema='Chat')['json'] c.call('GET', '/api/v1/chats/%s/messages?limit=1' % other['id'], actor=alice, schema='MessagePage') # Take a cursor from the original chat and try it on `other` fake = c.call('GET', path + '/messages?limit=1', actor=alice, schema='MessagePage')['json']['next_cursor'] c.call('GET', '/api/v1/chats/%s/messages?limit=1&cursor=%s' % (other['id'], fake), 400, actor=alice) print('PASS: student pagination tests (4 pages, no loss, cursor scoped)') if __name__ == '__main__': run(main)