def setUp(self): self.url = 'http://not.a.real.url/api/' self.name = 'test_user' self.password = '' with patch('requests.Session.post') as post: with patch('requests.Session.get') as get: post.return_value = build() get.return_value = build(update={'value': 'dev'}) self.settings = Settings(self.url, self.name, self.password)
def setUp(self): self.url = 'http://not.a.real.url/api/' self.name = 'test_user' self.password = '' self.value = 'some value' self.protect = True self.get_value = build(update={ 'value': self.value, 'protect': self.protect }) self.get_children = build( update={ "path": "/abc/", "children": [{ "override": "", "path": "/abc/val1/", "id": 9, "value": "1000", "protect": False, "name": "val1" }, { "override": "test_user", "path": "/abc/val1/", "id": 12, "value": "50", "protect": False, "name": "val1" }] }) self.get_history = build( update={ "History": [{ "active": False, "override": "", "id": 12, "value": "1000", "datetime": datetime.now(), "protect": False, "name": "abc", "author": "cityhall" }, { "active": True, "override": "", "id": 12, "value": "50", "datetime": datetime.now(), "protect": False, "name": "abc", "author": "test_dev" }] }) with patch('requests.Session.post') as post: with patch('requests.Session.get') as get: post.return_value = build() get.return_value = build(update={'value': 'dev'}) self.settings = Settings(self.url, self.name, self.password)
def test_logging_out(self, post, get, delete): """ Logging out hits the correct url """ post.return_value = build() get.return_value = build(update={'value': 'dev'}) delete.return_value = build() settings = Settings(self.url, self.username, '') settings.log_out() delete.assert_called_once_with(self.url + 'auth/') self.assertFalse(settings.logged_in)
def test_password_is_hashed(self, post, get): """ A password is passed in as plaintext and is hashed before the post """ post.return_value = build() get.return_value = build(update={'value': 'dev'}) Settings(self.url, self.username, 'abc') auth_url = self.url + 'auth/' post.assert_called_once_with(auth_url, data={ 'username': self.username, 'passhash': _hash_password('abc') })
def test_default_env_is_retrieved(self, post, get): """ Upon logging in, the default environment is retrieved """ post.return_value = build() get.return_value = build(update={'value': 'dev'}) settings = Settings(self.url, self.username, 'abc') get.asssert_called_once_with(self.url + 'auth/user/' + self.username + '/default/') self.assertEqual('dev', settings.default_env) self.failed_call_honored(settings.get_default_env, 'requests.Session.get') self.logout_honored(settings.get_default_env, settings)
def test_no_password_is_honored(self, post, get): """ By convention, no password passes an empty string """ post.return_value = build() get.return_value = build(update={'value': 'dev'}) Settings(self.url, self.username, '') auth_url = self.url + 'auth/' post.assert_called_once_with(auth_url, data={ 'username': self.username, 'passhash': '' })
def test_set_protect_override(self, post): post.return_value = build() self.settings.set_protect('dev', '/abc', self.name, True) payload = {'protect': True} post_url = self.url + 'env/dev/abc/' params = {'override': self.name} post.assert_called_once_with(post_url, data=payload, params=params)
def test_setting_default_env(self, get, post): """ A user should be able to set the environment, also. This test is here for completeness sake, since the call to get the default environment is part creating the Settings() class """ get.return_value = build(update={'value': 'dev'}) post.return_value = build(message='Updated default env') settings = Settings(self.url, self.username, '') settings.set_default_env('abc') post.assert_called_with(self.url + 'auth/user/' + self.username + '/default/', data={'env': 'abc'}) self.assertEqual('abc', settings.default_env) call = lambda: settings.set_default_env('abc') self.failed_call_honored(call, 'requests.Session.post') self.logout_honored(call, settings)
def test_can_create_environment(self, post): """ A user is able to create an environment """ post.return_value = build() self.settings.create_env('dev2') post.assert_called_once_with(self.url + 'auth/env/dev2/') call = lambda: self.settings.create_env('dev') self.failed_call_honored(call, 'requests.Session.post') self.logout_honored(call, self.settings)
def test_set_protect(self, post): post.return_value = build() self.settings.set_protect('dev', '/abc', '', True) payload = {'protect': True} params = {'override': ''} post_url = self.url + 'env/dev/abc/' post.assert_called_once_with(post_url, data=payload, params=params) call = lambda: self.settings.set_protect('dev', '/abc', '', True) self.failed_call_honored(call, 'requests.Session.post') self.logout_honored(call, self.settings)
def test_delete_user(self, delete): """ Delete a user """ delete.return_value = build() self.settings.delete_user('other_user') auth_url = self.url + 'auth/user/other_user/' delete.assert_called_once_with(auth_url) call = lambda: self.settings.delete_user('other_user') self.failed_call_honored(call, 'requests.Session.delete') self.logout_honored(call, self.settings)
def test_grant_rights(self, post): post.return_value = build() self.settings.grant_rights('dev', 'abc', 2) post.assert_called_once_with(self.url + 'auth/grant/', data={ 'env': 'dev', 'user': '******', 'rights': 2 }) call = lambda: self.settings.grant_rights('dev', 'abc', 2) self.failed_call_honored(call, 'requests.Session.post') self.logout_honored(call, self.settings)
def test_create_user(self, post): """ Create a user """ post.return_value = build() self.settings.create_user('user2', '') auth_url = self.url + 'auth/user/user2/' post.assert_called_with(auth_url, data={'passhash': ''}) self.settings.create_user('user2', 'abc') abc_hash = _hash_password('abc') post.assert_called_with(auth_url, data={'passhash': abc_hash}) call = lambda: self.settings.create_user('user2', 'abc') self.failed_call_honored(call, 'requests.Session.post') self.logout_honored(call, self.settings)
def test_update_password(self, put): """ Update your own password """ put.return_value = build() self.settings.update_password('') auth_url = self.url + 'auth/user/' + self.name + '/' put.assert_called_with(auth_url, data={'passhash': ''}) self.settings.update_password('abc') abc_hash = _hash_password('abc') put.assert_called_with(auth_url, data={'passhash': abc_hash}) call = lambda: self.settings.update_password('abc') self.failed_call_honored(call, 'requests.Session.put') self.logout_honored(call, self.settings)
def test_can_get_environment(self, get): """ A user is able to get details for an environment """ get.return_value = build( update={'Users': { 'test_user': 4, 'user2': 1, 'guest': 1 }}) env_users = self.settings.get_env('dev') self.assertEqual(3, len(env_users)) self.assertIn('test_user', env_users) self.assertEqual(4, env_users['test_user']) call = lambda: self.settings.get_env('dev') self.failed_call_honored(call, 'requests.Session.get') self.logout_honored(call, self.settings)
def test_get_user(self, get): """ Get information about a user, his permissions on all environments """ get.return_value = build( update={'Environments': { 'dev': 4, 'auto': 1, 'users': 1 }}) rights = self.settings.get_user('test_user') self.assertEqual(3, len(rights)) self.assertIn('dev', rights) self.assertEqual(4, rights['dev']) get.asssert_called_once_with(self.url + 'auth/user/test_user/') call = lambda: self.settings.get_user('test_user') self.failed_call_honored(call, 'requests.Session.get') self.logout_honored(call, self.settings)
def test_ensure_okay(self): self.assertIsInstance(_ensure_okay(build()), dict) with self.assertRaises(FailedCall): _ensure_okay(build(status_code=400)) with self.assertRaises(FailedCall): _ensure_okay(build(reply='Failure', message='abc'))
def setUp(self): self.url = 'http://not.a.real.url/api/' self.name = 'test_user' self.password = '' self.value = 'some value' self.protect = True self.get_value = build( update={'value': self.value, 'protect': self.protect} ) self.get_children = build( update={ "path": "/abc/", "children": [ { "override": "", "path": "/abc/val1/", "id": 9, "value": "1000", "protect": False, "name": "val1" }, { "override": "test_user", "path": "/abc/val1/", "id": 12, "value": "50", "protect": False, "name": "val1" } ] } ) self.get_history = build( update={ "History": [ { "active": False, "override": "", "id": 12, "value": "1000", "datetime": datetime.now(), "protect": False, "name": "abc", "author": "cityhall" }, { "active": True, "override": "", "id": 12, "value": "50", "datetime": datetime.now(), "protect": False, "name": "abc", "author": "test_dev" } ] } ) with patch('requests.Session.post') as post: with patch('requests.Session.get') as get: post.return_value = build() get.return_value = build(update={'value': 'dev'}) self.settings = Settings(self.url, self.name, self.password)