async def test_key_lifecycle(loop, wifi_keys_tempdir): with tempfile.TemporaryDirectory() as source_td: keys = list(wifi.list_keys()) assert keys == [] results = {} # We should be able to add multiple keys for fn in ['test1.pem', 'test2.pem', 'test3.pem']: path = os.path.join(source_td, fn) with open(path, 'w') as f: f.write(str(random.getrandbits(2048))) with open(path, 'rb') as f: add_response = wifi.add_key(fn, f.read()) assert add_response.created is True assert add_response.key.file == fn results[fn] = add_response # We should not be able to upload a duplicate with open(os.path.join(source_td, 'test1.pem'), 'rb') as f: add_response = wifi.add_key('test1.pem', f.read()) assert add_response.created is False # We should be able to see them all list_resp = list(wifi.list_keys()) assert len(list_resp) == 3 for elem in list_resp: assert elem.directory in { r.key.directory for r in results.values() } for fn, data in results.items(): del_resp = wifi.remove_key(data.key.directory) assert del_resp == fn del_list_resp = list(wifi.list_keys()) assert data.key.directory not in { k.directory for k in del_list_resp } dup_del_resp = wifi.remove_key(results['test1.pem'].key.directory) assert dup_del_resp is None
async def get_wifi_keys(): keys = [ WifiKeyFile(uri=f'/wifi/keys/{key.directory}', id=key.directory, name=os.path.basename(key.file)) for key in wifi.list_keys() ] # Why not create a WifiKeyFiles? Because validation fails when there's a # pydantic model with attribute named keys. Deep in the guts of pydantic # there's a call to `dict(model)` which raises an exception because `keys` # is not callable, like the `keys` member of dict. # A problem for another time. return {"keys": keys}
async def test_list_keys(loop, wifi_keys_tempdir): dummy_names = ['ad12d1df199bc912', 'cbdda8124128cf', '812410990c5412'] for dn in dummy_names: os.mkdir(os.path.join(wifi_keys_tempdir, dn)) open(os.path.join(wifi_keys_tempdir, dn, 'test.pem'), 'w').write('hi') keys = list(wifi.list_keys()) assert len(keys) == 3 for dn in dummy_names: for keyfile in keys: if keyfile.directory == dn: assert keyfile.file == 'test.pem' break else: raise KeyError(dn)
async def list_keys(request: web.Request) -> web.Response: """ List the key files installed in the system. This responds with a list of the same objects as key: ``` GET /wifi/keys -> 200 OK { keys: [ { uri: '/wifi/keys/some-hex-digest', id: 'some-hex-digest', name: 'keyfile.pem' }, ... ] } ``` """ keys = [ {'uri': '/wifi/keys/{}'.format(key.directory), 'id': key.directory, 'name': os.path.basename(key.file)} for key in wifi.list_keys() ] return web.json_response({'keys': keys}, status=200)