def search(cls, session, text): resp = call_api(session, 'GET', '/{}?search_text={}'.format(cls._api_path, text)) if resp.status_code == 200: result = [] resp_list = resp.json() for item in resp_list: result.append(cls(**item)) return True, result return False, resp
def save_new(self, session): resp = call_api(session, 'POST', '/{}'.format(self._api_path), self.toJSON(True)) if resp.status_code == 201: # If id_fields is not list, we get a new ID back from create # otherwise, we don't need a value from the response if not isinstance(self._id_fields, set): self.set_ids(**resp.json()) return True, None return False, resp
def get(cls, session, ids): # Second argument can be a string if child class has only one ID field, # should be a list otherwise. We'll convert to a list so that we can join # it around the API path separator character regardless ids_arg = [ids] if type(ids) == str else ids resp = call_api(session, 'GET', '/{}/{}'.format(cls._api_path, '/'.join(ids_arg))) if resp.status_code == 200: return True, cls(**resp.json()) return False, resp
def test_login(): """LoginAPI: Test login API""" resp = call_api(TEST_SESSION, 'GET', '/us/login', None, True) log_response_error(resp) assert resp.status_code == 200 json = resp.json() assert len(json['Groups']) == 0 assert len(json['Memberships']) == 0 assert len(json['Users']) >= 1
def test_reset_finish_email_mismatch(): """--> Test reset finish fail due to email mismatch""" session = get_session() user = session.query(User).filter(User.email == '*****@*****.**').one_or_none() assert user is not None reset_json = { 'email': '*****@*****.**', 'password': '******', 'reset_code': user.reset_code } resp = call_api(TEST_SESSION, 'PUT', '/us/pw_reset', reset_json, True) log_response_error(resp, True) assert resp.status_code == 400
def test_refresh_finish_expired_code(): """--> Test reset API failure due to expired code""" # Look up the user in the database and set the reset_expires to now session = get_session() user = session.query(User).filter(User.email == '*****@*****.**').one_or_none() assert user user.reset_expires = datetime.datetime.now() - datetime.timedelta(days=5) session.add(user) session.commit() reset_json = { 'email': '*****@*****.**', 'password': '******', 'reset_code': user.reset_code } session.close() resp = call_api(TEST_SESSION, 'PUT', '/us/pw_reset', reset_json, True) log_response_error(resp, True) assert resp.status_code == 400
def test_reset_fails_email(): """--> Test that reset fails due to email difference""" resp = call_api(None, 'POST', '/us/pw_reset', {'email': '*****@*****.**'}) log_response_error(resp, True) assert resp.status_code == 400 or resp.status_code == 404
def test_reset_invalid_email(): """PasswordResetAPIs: Test reset API failure due to invalid email""" resp = call_api(None, 'POST', '/us/pw_reset', {'email': '*****@*****.**'}) log_response_error(resp, True) assert resp.status_code == 404
def test_rest_start_fails_existing_code(): """--> Test getting a second reset code when existing one has not expired fails""" resp = call_api(TEST_SESSION, 'POST', '/us/pw_reset', {'email': '*****@*****.**'}, True) log_response_error(resp, True) assert resp.status_code == 400
def test_group_add_no_json(): """GroupAPIs Extended: Test add API failure due to no json""" resp = call_api(TEST_SESSION, 'POST', '/groups') log_response_error(resp, True) assert resp.status_code == 400