import base64, hashlib, json, time, urllib.request, uuid from pathlib import Path BASE='http://127.0.0.1:8085' ROOT=Path('.') content=(ROOT/'tests/fixtures/sample.png').read_bytes() def call(method, path, expected=None, body=None, token=None, raw=None, headers=None): h={'Accept':'application/json'} if token: h['Authorization']='Bearer '+token data=None if body is not None: data=json.dumps(body).encode(); h['Content-Type']='application/json' if raw is not None: data=raw if headers: h.update(headers) req=urllib.request.Request(BASE+path, data=data, headers=h, method=method) try: resp=urllib.request.urlopen(req, timeout=30) except urllib.error.HTTPError as e: resp=e status=resp.code; payload=resp.read(); ctype=resp.headers.get_content_type() js=json.loads(payload) if ctype=='application/json' and payload else None if expected is not None and status!=expected: raise SystemExit(f'{method} {path}: expected {expected} got {status} body={payload[:300]!r}') return status, js, payload, resp.headers # stop worker import subprocess subprocess.check_call(['docker','compose','--env-file','.env','stop','worker'], cwd=str(ROOT)) print('worker stopped') # register+login uname='u_demo_'+uuid.uuid4().hex[:10] pwd='T3st-'+uuid.uuid4().hex _, user, _, _ = call('POST','/api/v1/users',201,body={'username':uname,'display_name':'Demo','password':pwd}) cred=base64.b64encode((uname+':'+pwd).encode()).decode() _, sess, _, _ = call('POST','/api/v1/auth/sessions',201,headers={'Authorization':'Basic '+cred}) token=sess['token'] _, chat, _, _ = call('POST','/api/v1/chats',201,token=token,body={'title':'demo', 'member_ids':[user['id']]}) status, att, _, _ = call('POST',f"/api/v1/chats/{chat['id']}/attachments?filename=sample.png",202,token=token,raw=content,headers={'Content-Type':'image/png'}) print('upload', status, att['status'], att['id']) assert att['status']=='queued' # content must be 409 st,_,_,_ = call('GET',f"/api/v1/attachments/{att['id']}/content",409,token=token) print('content while queued', st) # message with queued attachment call('POST',f"/api/v1/chats/{chat['id']}/messages",201,token=token,body={'text':'','attachment_ids':[att['id']]},headers={'Idempotency-Key':uuid.uuid4().hex}) print('message linked while queued') # restart api subprocess.check_call(['docker','compose','--env-file','.env','restart','api1','api2'], cwd=str(ROOT)) # wait ready for _ in range(60): try: st,js,_,_=call('GET','/health/ready') if st==200: break except Exception: pass time.sleep(1) else: raise SystemExit('api not ready') print('api restarted and ready') # start worker subprocess.check_call(['docker','compose','--env-file','.env','start','worker'], cwd=str(ROOT)) print('worker started') # wait ready attachment deadline=time.time()+60 ready=None while time.time()