def setUp(self): super().setUp() self.fixture = User() self.fixture.set_first_name("First") self.fixture.set_last_name("Last") self.fixture.set_email("email") self.fixture.set_password("password") self.fixture.set_role(Role.USER) self.db.add(self.fixture) self.db.commit()
def test_existing_user(self): user = User(first_name="John", last_name="Smith", email="*****@*****.**", role=Role.USER) user.set_password("test") self.db.add(user) self.fixture.add_user(user) count = self.fixture.users.count() self.fixture.add_user(user) self.assertEqual(count, self.fixture.users.count(), "counts not same")
def setUp(self): super().setUp() self.user = User() self.user.set_first_name("Foo") self.user.set_last_name("Bar") self.user.set_role(Role.USER) self.user.set_email("*****@*****.**") self.user.set_password("password") print("app:", self.server._app) print("app.session", self.server._app.session) self.server._app.session.add(self.user) self.server._app.session.commit()
def test_create_from_json(self): expected = { "id": 2, "first_name": "First", "last_name": "Last", "email": "other", "password": "******", "role": str(Role.USER) } self.fixture = User(expected) self.db.commit() self.assertEqual(expected["first_name"], self.fixture.get_first_name()) self.assertEqual(expected["last_name"], self.fixture.get_last_name()) self.assertEqual(expected["email"], self.fixture.get_email()) self.assertEqual(Role.from_str(expected["role"]), self.fixture.get_role()) self.assertTrue(self.fixture.check_password(expected['password']))
def test_users(self): u1 = User(first_name="John", last_name="Smith", email="*****@*****.**", role=Role.USER) u1.set_password("test") u2 = User(first_name="Jane", last_name="Smith", email="*****@*****.**", role=Role.USER) u2.set_password("test") self.db.add(u1) self.db.add(u2) self.fixture.add_user(u1) self.fixture.add_user(u2) self.db.commit() self.assertIn(u1, self.fixture.users) self.assertIn(u2, self.fixture.users)
def create_user(): if not request.is_json: return jsonify({"msg": "Missing JSON in request"}), 400 response_object = {'status': 'success'} post_data = request.get_json() if post_data is None or post_data == {}: response_object['message'] = 'Missing data' response_object['status'] = 'failure' return jsonify(response_object), 400 else: print("Post Data: " + str(post_data)) user = User(post_data) current_app.session.add(user) current_app.session.commit() response_object['message'] = 'User added!' return jsonify(response_object), 200
def login(): if not request.is_json: return jsonify({"msg": "Missing JSON in request"}), 400 data = request.get_json() if not data.get('email', None): return jsonify({'msg': 'Missing email parameter'}), 400 if not data.get('password', None): return jsonify({'msg': 'Missing password parameter'}), 400 user = User.authenticate(data) if not user: return jsonify({ 'message': 'Invalid credentials', 'authenticated': False }), 401 token = create_access_token(identity=user.get_email()) dct = user.to_dict() dct['token'] = token return jsonify(dct), 200
class UserTest(BaseTest): def setUp(self): super().setUp() self.fixture = User() self.fixture.set_first_name("First") self.fixture.set_last_name("Last") self.fixture.set_email("email") self.fixture.set_password("password") self.fixture.set_role(Role.USER) self.db.add(self.fixture) self.db.commit() def test_id(self): self.assertEqual(self.fixture.get_id(), 2, "id's do not match") def test_first_name(self): self.assertEqual(self.fixture.get_first_name(), "First", "names do not match") def test_null_first_name(self): with self.assertRaises(Exception): self.fixture.set_first_name(None) def test_empty_first_name(self): with self.assertRaises(Exception): self.fixture.set_first_name("") def test_last_name(self): self.assertEqual(self.fixture.get_last_name(), "Last", "names do not match") def test_null_last_name(self): with self.assertRaises(Exception): self.fixture.set_last_name(None) def test_empty_last_name(self): with self.assertRaises(Exception): self.fixture.set_last_name("") def test_email(self): self.assertEqual(self.fixture.get_email(), "email", "names do not match") def test_null_email(self): with self.assertRaises(Exception): self.fixture.set_email(None) def test_empty_email(self): with self.assertRaises(Exception): self.fixture.set_email("") def test_role(self): self.assertEqual(self.fixture.get_role(), Role.USER, "roles do not match") def test_null_role(self): with self.assertRaises(Exception): self.fixture.set_role(None) def test_password_hashing(self): self.fixture.set_password('cat') self.assertFalse(self.fixture.check_password('dog')) self.assertTrue(self.fixture.check_password('cat')) def test_api_key(self): key = ApiKey() key.set_key("testkey") key.set_description("Test Key") self.db.add(key) self.fixture.add_api_key(key) self.db.commit() self.assertEqual(1, self.fixture.api_keys.count(), "counts are same") self.assertIn(key, self.fixture.api_keys, "key not found") def test_null_api_key(self): self.fixture.add_api_key(None) self.assertEqual(0, self.fixture.api_keys.count(), "counts are same") def test_existing_api_key(self): key = ApiKey() key.set_key("testkey") key.set_description("Test Key") self.db.add(key) self.fixture.add_api_key(key) self.fixture.add_api_key(key) self.db.commit() self.assertEqual(1, self.fixture.api_keys.count(), "counts are same") def test_device(self): sc = Device(name="SC_Test") self.db.add(sc) sc.add_user(self.fixture) self.db.commit() self.assertIn(self.fixture, sc.users, "user not added") self.assertEqual(self.fixture.device_id, sc.get_id(), "id mismatch") def test_to_dict(self): expected = { "id": 2, "first_name": "First", "last_name": "Last", "email": "email", "role": str(Role.USER) } result = self.fixture.to_dict() self.assertDictEqual(expected, result, "dicts not the same") def test_update(self): expected = { "first_name": "First2", "last_name": "Last2", "email": "email2", "password": "******", "role": str(Role.ADMIN) } self.fixture.update(expected) self.assertEqual(expected["first_name"], self.fixture.get_first_name()) self.assertEqual(expected["last_name"], self.fixture.get_last_name()) self.assertEqual(expected["email"], self.fixture.get_email()) self.assertEqual(Role.from_str(expected["role"]), self.fixture.get_role()) self.assertTrue(self.fixture.check_password(expected['password'])) def test_create_from_json(self): expected = { "id": 2, "first_name": "First", "last_name": "Last", "email": "other", "password": "******", "role": str(Role.USER) } self.fixture = User(expected) self.db.commit() self.assertEqual(expected["first_name"], self.fixture.get_first_name()) self.assertEqual(expected["last_name"], self.fixture.get_last_name()) self.assertEqual(expected["email"], self.fixture.get_email()) self.assertEqual(Role.from_str(expected["role"]), self.fixture.get_role()) self.assertTrue(self.fixture.check_password(expected['password'])) def test_repr(self): self.assertEqual("User: email", self.fixture.__repr__(), "repr not same") def test_str(self): self.assertEqual("User: email", str(self.fixture), "string representation not same") def test_authenticate(self): data = dict(email="admin", password="******") result = User.authenticate(data) self.assertIsNotNone(result) def test_authenticate_none_email(self): data = dict(email=None, password="******") result = User.authenticate(data) self.assertIsNone(result) def test_authenticate_none_pass(self): data = dict(email="admin", password=None) result = User.authenticate(data) self.assertIsNone(result) def test_authenticate_unknown_user(self): data = dict(email="bob", password="******") result = User.authenticate(data) self.assertIsNone(result) def test_authenticate_basd_pass(self): data = dict(email="admin", password="******") result = User.authenticate(data) self.assertIsNone(result) def test_find_user(self): email = "admin" result = User.find_user(email) self.assertTrue(result) def test_find_user_unknown(self): email = "*****@*****.**" result = User.find_user(email) self.assertFalse(result) def test_find_user_none(self): with self.assertRaises(Exception): User.find_user(None) def test_find_user_empty(self): with self.assertRaises(Exception): User.find_user("")
def test_parent_user(self): user = User() user.set_first_name("First") user.set_last_name("Last") user.set_email("email") user.set_password("password") user.set_role(Role.USER) self.db.add(user) self.db.commit() user.add_api_key(self.fixture) self.assertEqual(user, self.fixture.user, "users are not the same")
def add_claims_to_access_token(identity): user = User.find_user(identity) return {'role': str(user.get_role())}
class UserTest(BaseApiTest): def setUp(self): super().setUp() self.user = User() self.user.set_first_name("Foo") self.user.set_last_name("Bar") self.user.set_role(Role.USER) self.user.set_email("*****@*****.**") self.user.set_password("password") print("app:", self.server._app) print("app.session", self.server._app.session) self.server._app.session.add(self.user) self.server._app.session.commit() def test_create_user(self): json = dict( first_name="FooFoo", last_name="BarBar", role="USER", email="*****@*****.**", password="******", ) rv = BaseApiTest.auth_post(self.client, "admin", "admin", route="/api/users", json=json) self.assertEqual("User added!", rv.get_json().get('message')) self.assertEqual("success", rv.get_json().get('status')) def test_create_user_noauth(self): json = dict( first_name="FooFoo", last_name="BarBar", role="USER", email="*****@*****.**", password="******", ) rv = self.client.post('/api/users', json=json) self.assertEqual('Missing Authorization Header', rv.get_json().get('msg')) def test_create_user_auth_nodata(self): json = dict() rv = BaseApiTest.auth_post(self.client, "admin", "admin", route="/api/users", json=json) self.assertEqual("Missing data", rv.get_json().get('message')) self.assertEqual('failure', rv.get_json().get('status')) self.assertEqual("400 BAD REQUEST", rv.status) def test_create_user_auth_nonjson(self): json = dict() rv = self.client.post('/api/users', data=json, headers=BaseApiTest._create_auth_header( self.client, "admin", "admin")) self.assertEqual("Missing JSON in request", rv.get_json().get('msg')) def test_read_all_users(self): rv = BaseApiTest.auth_get(self.client, "admin", "admin", route="/api/users") self.assertEqual(2, len(rv.get_json())) def test_read_all_users_noauth(self): rv = self.client.get('/api/users') self.assertEqual('Missing Authorization Header', rv.get_json().get('msg')) def test_read_one_user(self): rv = BaseApiTest.auth_get(self.client, "admin", "admin", route=f"/api/users/{self.user.get_id()}") self.assertEqual(self.user.get_id(), rv.get_json().get("id")) self.assertEqual(self.user.get_email(), rv.get_json().get("email")) def test_read_one_user_noauth(self): rv = self.client.get(f'/api/users/{self.user.get_id()}') self.assertEqual('Missing Authorization Header', rv.get_json().get('msg')) # def test_read_one_user_notself_notadmin(self): # rv = BaseApiTest.auth_get(self.client, "*****@*****.**", "password", route=f"/api/users/{self.user.get_id()}") # self.fail() def test_read_one_user_self_notadmin(self): rv = BaseApiTest.auth_get(self.client, "*****@*****.**", "password", route=f"/api/users/{self.user.get_id()}") self.assertEqual(self.user.get_id(), rv.get_json().get("id")) self.assertEqual(self.user.get_email(), rv.get_json().get("email")) def test_read_one_user_self_admin(self): rv = BaseApiTest.auth_get(self.client, "admin", "admin", route="/api/users/1") self.assertEqual(1, rv.get_json().get("id")) self.assertEqual("admin", rv.get_json().get("email"))
def test_find_user_none(self): with self.assertRaises(Exception): User.find_user(None)
def test_find_user_empty(self): with self.assertRaises(Exception): User.find_user("")
def test_find_user_unknown(self): email = "*****@*****.**" result = User.find_user(email) self.assertFalse(result)
def test_find_user(self): email = "admin" result = User.find_user(email) self.assertTrue(result)
def test_authenticate_basd_pass(self): data = dict(email="admin", password="******") result = User.authenticate(data) self.assertIsNone(result)
def test_authenticate_unknown_user(self): data = dict(email="bob", password="******") result = User.authenticate(data) self.assertIsNone(result)
def test_authenticate_none_email(self): data = dict(email=None, password="******") result = User.authenticate(data) self.assertIsNone(result)