2015-08-02 11:59:25 +00:00
|
|
|
from redash import models
|
2016-11-28 10:16:32 +00:00
|
|
|
from tests import BaseTestCase
|
2015-08-02 11:59:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestUserListResourcePost(BaseTestCase):
|
|
|
|
def test_returns_403_for_non_admin(self):
|
2015-12-01 13:44:08 +00:00
|
|
|
rv = self.make_request('post', "/api/users")
|
|
|
|
self.assertEqual(rv.status_code, 403)
|
2015-08-02 11:59:25 +00:00
|
|
|
|
|
|
|
def test_returns_400_when_missing_fields(self):
|
2015-12-01 13:44:08 +00:00
|
|
|
admin = self.factory.create_admin()
|
2015-08-02 11:59:25 +00:00
|
|
|
|
2015-12-01 13:44:08 +00:00
|
|
|
rv = self.make_request('post', "/api/users", user=admin)
|
|
|
|
self.assertEqual(rv.status_code, 400)
|
2015-08-02 11:59:25 +00:00
|
|
|
|
2015-12-01 13:44:08 +00:00
|
|
|
rv = self.make_request('post', '/api/users', data={'name': 'User'}, user=admin)
|
|
|
|
self.assertEqual(rv.status_code, 400)
|
2015-08-02 11:59:25 +00:00
|
|
|
|
|
|
|
def test_creates_user(self):
|
2015-12-01 13:44:08 +00:00
|
|
|
admin = self.factory.create_admin()
|
|
|
|
|
|
|
|
test_user = {'name': 'User', 'email': 'user@example.com', 'password': 'test'}
|
|
|
|
rv = self.make_request('post', '/api/users', data=test_user, user=admin)
|
|
|
|
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
self.assertEqual(rv.json['name'], test_user['name'])
|
|
|
|
self.assertEqual(rv.json['email'], test_user['email'])
|
2015-08-02 11:59:25 +00:00
|
|
|
|
2016-11-30 14:24:59 +00:00
|
|
|
def test_returns_400_when_email_taken(self):
|
|
|
|
admin = self.factory.create_admin()
|
|
|
|
|
|
|
|
test_user = {'name': 'User', 'email': admin.email, 'password': 'test'}
|
|
|
|
rv = self.make_request('post', '/api/users', data=test_user, user=admin)
|
|
|
|
|
|
|
|
self.assertEqual(rv.status_code, 400)
|
|
|
|
|
2015-08-02 11:59:25 +00:00
|
|
|
|
2015-12-01 13:44:08 +00:00
|
|
|
class TestUserListGet(BaseTestCase):
|
|
|
|
def test_returns_users_for_given_org_only(self):
|
|
|
|
user1 = self.factory.user
|
|
|
|
user2 = self.factory.create_user()
|
|
|
|
org = self.factory.create_org()
|
|
|
|
user3 = self.factory.create_user(org=org)
|
|
|
|
|
|
|
|
rv = self.make_request('get', "/api/users")
|
|
|
|
user_ids = map(lambda u: u['id'], rv.json)
|
|
|
|
self.assertIn(user1.id, user_ids)
|
|
|
|
self.assertIn(user2.id, user_ids)
|
|
|
|
self.assertNotIn(user3.id, user_ids)
|
2015-08-02 11:59:25 +00:00
|
|
|
|
|
|
|
|
2015-08-06 13:24:39 +00:00
|
|
|
class TestUserResourceGet(BaseTestCase):
|
|
|
|
def test_returns_api_key_for_your_own_user(self):
|
2015-12-01 13:44:08 +00:00
|
|
|
rv = self.make_request('get', "/api/users/{}".format(self.factory.user.id))
|
|
|
|
self.assertIn('api_key', rv.json)
|
2015-08-06 13:24:39 +00:00
|
|
|
|
|
|
|
def test_returns_api_key_for_other_user_when_admin(self):
|
2015-12-01 13:44:08 +00:00
|
|
|
other_user = self.factory.user
|
|
|
|
admin = self.factory.create_admin()
|
|
|
|
|
|
|
|
rv = self.make_request('get', "/api/users/{}".format(other_user.id), user=admin)
|
|
|
|
self.assertIn('api_key', rv.json)
|
2015-08-06 13:24:39 +00:00
|
|
|
|
|
|
|
def test_doesnt_return_api_key_for_other_user(self):
|
2015-12-01 13:44:08 +00:00
|
|
|
other_user = self.factory.create_user()
|
|
|
|
|
|
|
|
rv = self.make_request('get', "/api/users/{}".format(other_user.id))
|
|
|
|
self.assertNotIn('api_key', rv.json)
|
|
|
|
|
|
|
|
def test_doesnt_return_user_from_different_org(self):
|
|
|
|
org = self.factory.create_org()
|
|
|
|
other_user = self.factory.create_user(org=org)
|
|
|
|
|
|
|
|
rv = self.make_request('get', "/api/users/{}".format(other_user.id))
|
|
|
|
self.assertEqual(rv.status_code, 404)
|
2015-08-06 13:24:39 +00:00
|
|
|
|
|
|
|
|
2015-08-02 11:59:25 +00:00
|
|
|
class TestUserResourcePost(BaseTestCase):
|
|
|
|
def test_returns_403_for_non_admin_changing_not_his_own(self):
|
2015-12-01 13:44:08 +00:00
|
|
|
other_user = self.factory.create_user()
|
|
|
|
|
|
|
|
rv = self.make_request('post', "/api/users/{}".format(other_user.id), data={"name": "New Name"})
|
|
|
|
self.assertEqual(rv.status_code, 403)
|
2015-08-02 11:59:25 +00:00
|
|
|
|
|
|
|
def test_returns_200_for_non_admin_changing_his_own(self):
|
2015-12-01 13:44:08 +00:00
|
|
|
rv = self.make_request('post', "/api/users/{}".format(self.factory.user.id), data={"name": "New Name"})
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
2015-08-02 11:59:25 +00:00
|
|
|
|
|
|
|
def test_returns_200_for_admin_changing_other_user(self):
|
2015-12-01 13:44:08 +00:00
|
|
|
admin = self.factory.create_admin()
|
2015-08-02 11:59:25 +00:00
|
|
|
|
2015-12-01 13:44:08 +00:00
|
|
|
rv = self.make_request('post', "/api/users/{}".format(self.factory.user.id), data={"name": "New Name"}, user=admin)
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
2015-08-02 11:59:25 +00:00
|
|
|
|
|
|
|
def test_fails_password_change_without_old_password(self):
|
2015-12-01 13:44:08 +00:00
|
|
|
rv = self.make_request('post', "/api/users/{}".format(self.factory.user.id), data={"password": "new password"})
|
|
|
|
self.assertEqual(rv.status_code, 403)
|
2015-08-02 11:59:25 +00:00
|
|
|
|
|
|
|
def test_fails_password_change_with_incorrect_old_password(self):
|
2015-12-01 13:44:08 +00:00
|
|
|
rv = self.make_request('post', "/api/users/{}".format(self.factory.user.id), data={"password": "new password", "old_password": "wrong"})
|
|
|
|
self.assertEqual(rv.status_code, 403)
|
2015-08-02 11:59:25 +00:00
|
|
|
|
|
|
|
def test_changes_password(self):
|
|
|
|
new_password = "new password"
|
|
|
|
old_password = "old password"
|
|
|
|
|
2015-12-01 13:44:08 +00:00
|
|
|
self.factory.user.hash_password(old_password)
|
2016-11-30 14:24:59 +00:00
|
|
|
models.db.session.add(self.factory.user)
|
2015-12-01 13:44:08 +00:00
|
|
|
|
|
|
|
rv = self.make_request('post', "/api/users/{}".format(self.factory.user.id), data={"password": new_password, "old_password": old_password})
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
2015-08-02 11:59:25 +00:00
|
|
|
|
2016-11-30 14:24:59 +00:00
|
|
|
user = models.User.query.get(self.factory.user.id)
|
2015-12-01 13:44:08 +00:00
|
|
|
self.assertTrue(user.verify_password(new_password))
|