def test_save_keys_for_user_does_not_create_duplicate_keysource(self): user = factory.make_User() protocol = random.choice( [KEYS_PROTOCOL_TYPE.LP, KEYS_PROTOCOL_TYPE.GH]) auth_id = factory.make_name("auth_id") factory.make_KeySource(protocol=protocol, auth_id=auth_id) mock_import_keys = self.patch(KeySource, "import_keys") KeySource.objects.save_keys_for_user(user, protocol, auth_id) self.expectThat(mock_import_keys, MockCalledOnceWith(user)) self.expectThat(KeySource.objects.count(), Equals(1))
def test_sshkey_same_key_can_be_used_by_different_sources(self): auth_id = factory.make_name('auth_id') keysource1 = factory.make_KeySource(protocol=KEYS_PROTOCOL_TYPE.LP, auth_id=auth_id) keysource2 = factory.make_KeySource(protocol=KEYS_PROTOCOL_TYPE.GH, auth_id=auth_id) key_string = get_data('data/test_rsa0.pub') user = factory.make_User() key1 = SSHKey(key=key_string, user=user, keysource=keysource1) key1.save() key2 = SSHKey(key=key_string, user=user, keysource=keysource2) key2.save() self.assertIsNone(key2.full_clean())
def test_do_not_delete_keysource_when_keys(self): user = factory.make_User() protocol = random.choice( [KEYS_PROTOCOL_TYPE.LP, KEYS_PROTOCOL_TYPE.GH]) auth_id = factory.make_name("auth_id") keysource = factory.make_KeySource(protocol=protocol, auth_id=auth_id) factory.make_SSHKey(user=user, keysource=keysource) self.assertIsNotNone(reload_object(keysource))
def test_delete_keysource_deleted_when_no_more_keys(self): user = factory.make_User() protocol = random.choice( [KEYS_PROTOCOL_TYPE.LP, KEYS_PROTOCOL_TYPE.GH]) auth_id = factory.make_name('auth_id') keysource = factory.make_KeySource(protocol=protocol, auth_id=auth_id) sshkey = factory.make_SSHKey(user=user, keysource=keysource) sshkey.delete() self.assertIsNone(reload_object(keysource))
def test_sshkey_user_and_key_unique_together(self): protocol = random.choice( [KEYS_PROTOCOL_TYPE.LP, KEYS_PROTOCOL_TYPE.GH]) auth_id = factory.make_name('auth_id') keysource = factory.make_KeySource(protocol=protocol, auth_id=auth_id) key_string = get_data('data/test_rsa0.pub') user = factory.make_User() key = SSHKey(key=key_string, user=user, keysource=keysource) key.save() key2 = SSHKey(key=key_string, user=user, keysource=keysource) self.assertRaises(ValidationError, key2.full_clean)
def test_import_keys_with_no_keys(self): user = factory.make_User() protocol = random.choice( [KEYS_PROTOCOL_TYPE.LP, KEYS_PROTOCOL_TYPE.GH]) auth_id = factory.make_name("auth_id") keysource = factory.make_KeySource(protocol, auth_id) mock_get_protocol_keys = self.patch(keysource_module, "get_protocol_keys") mock_get_protocol_keys.return_value = [] keysource.import_keys(user) self.expectThat(mock_get_protocol_keys, MockCalledOnceWith(protocol, auth_id)) self.expectThat(SSHKey.objects.count(), Equals(0))
def test_sshkey_user_and_key_unique_together_change_key(self): protocol = random.choice( [KEYS_PROTOCOL_TYPE.LP, KEYS_PROTOCOL_TYPE.GH] ) auth_id = factory.make_name("auth_id") keysource = factory.make_KeySource(protocol=protocol, auth_id=auth_id) key_string1 = get_data("data/test_rsa1.pub") key_string2 = get_data("data/test_rsa2.pub") user = factory.make_User() key1 = SSHKey(key=key_string1, user=user, keysource=keysource) key1.save() key2 = SSHKey(key=key_string2, user=user, keysource=keysource) key2.save() key2.key = key1.key self.assertRaises(ValidationError, key2.full_clean)
def test_import_keys_with_keys(self): user = factory.make_User() protocol = random.choice( [KEYS_PROTOCOL_TYPE.LP, KEYS_PROTOCOL_TYPE.GH]) auth_id = factory.make_name("auth_id") keysource = factory.make_KeySource(protocol, auth_id) keys = get_data("data/test_rsa0.pub") + get_data("data/test_rsa1.pub") mock_get_protocol_keys = self.patch(keysource_module, "get_protocol_keys") mock_get_protocol_keys.return_value = keys.strip().split("\n") returned_sshkeys = keysource.import_keys(user) self.expectThat(mock_get_protocol_keys, MockCalledOnceWith(protocol, auth_id)) self.expectThat(SSHKey.objects.count(), Equals(2)) self.assertItemsEqual(returned_sshkeys, SSHKey.objects.filter(keysource=keysource))
def test_import_keys_source_exists_adds_new_keys(self): user = factory.make_User() protocol = random.choice( [KEYS_PROTOCOL_TYPE.LP, KEYS_PROTOCOL_TYPE.GH]) auth_id = factory.make_name("auth_id") keysource = factory.make_KeySource(protocol, auth_id) keys = get_data("data/test_rsa0.pub") + get_data("data/test_rsa1.pub") mock_get_protocol_keys = self.patch(keysource_module, "get_protocol_keys") mock_get_protocol_keys.return_value = keys.strip().split("\n") keysource.import_keys(user) # Add a new key keys += get_data("data/test_rsa2.pub") mock_get_protocol_keys.return_value = keys.strip().split("\n") returned_sshkeys = keysource.import_keys(user) self.assertEqual(3, SSHKey.objects.count()) self.assertCountEqual(returned_sshkeys, SSHKey.objects.filter(keysource=keysource))
def test_import_ssh_keys_creates_keys_not_duplicate_keysource(self): protocol = random.choice( [KEYS_PROTOCOL_TYPE.LP, KEYS_PROTOCOL_TYPE.GH]) auth_id = factory.make_name('auth_id') ks = "%s:%s" % (protocol, auth_id) keysource = factory.make_KeySource(protocol=protocol, auth_id=auth_id) key_string = get_data('data/test_rsa0.pub') mock_get_protocol_keys = self.patch(keysource_module, 'get_protocol_keys') mock_get_protocol_keys.return_value = [key_string] response = self.client.post(reverse('sshkeys_handler'), data=dict(op='import', keysource=ks)) added_key = get_one(SSHKey.objects.filter(user=self.user)) self.assertEqual(key_string, added_key.key) self.assertEqual(str(keysource), str(added_key.keysource)) self.assertEqual(1, KeySource.objects.count()) self.assertEqual(http.client.OK, response.status_code, response) self.assertThat(mock_get_protocol_keys, MockCalledOnceWith(protocol, auth_id))
def test_import_keys_source_exists_doesnt_remove_keys(self): user = factory.make_User() protocol = random.choice( [KEYS_PROTOCOL_TYPE.LP, KEYS_PROTOCOL_TYPE.GH]) auth_id = factory.make_name('auth_id') keysource = factory.make_KeySource(protocol, auth_id) keys = get_data('data/test_rsa0.pub') + get_data('data/test_rsa1.pub') mock_get_protocol_keys = self.patch(keysource_module, 'get_protocol_keys') mock_get_protocol_keys.return_value = keys.strip().split('\n') returned_sshkeys = keysource.import_keys(user) # only return one key keys = get_data('data/test_rsa0.pub') mock_get_protocol_keys.return_value = keys.strip().split('\n') keysource.import_keys(user) # no key is removed self.assertEqual(2, SSHKey.objects.count()) self.assertCountEqual(returned_sshkeys, SSHKey.objects.filter(keysource=keysource))
def test_sshkey_user_and_key_unique_together_db_level(self): # Even if we hack our way around model-level checks, uniqueness # of the user/key combination is enforced at the database level. protocol = random.choice( [KEYS_PROTOCOL_TYPE.LP, KEYS_PROTOCOL_TYPE.GH]) auth_id = factory.make_name('auth_id') keysource = factory.make_KeySource(protocol=protocol, auth_id=auth_id) key_string = get_data('data/test_rsa0.pub') user = factory.make_User() existing_key = SSHKey(key=key_string, user=user, keysource=keysource) existing_key.save() # The trick to hack around the model-level checks: create a # duplicate key for another user, then attach it to the same # user as the existing key by updating it directly in the # database. redundant_key = SSHKey(key=key_string, user=factory.make_User(), keysource=keysource) redundant_key.save() self.assertRaises(IntegrityError, SSHKey.objects.filter(id=redundant_key.id).update, user=user)