Ejemplo n.º 1
0
 def test_get(self):
     user = factory.make_User()
     handler = SSHKeyHandler(user, {}, None)
     sshkey = factory.make_SSHKey(user)
     self.assertEqual(
         self.dehydrate_sshkey(sshkey), handler.get({"id": sshkey.id})
     )
Ejemplo n.º 2
0
 def test_list(self):
     user = factory.make_User()
     handler = SSHKeyHandler(user, {}, None)
     factory.make_SSHKey(user)
     expected_sshkeys = [
         self.dehydrate_sshkey(sshkey) for sshkey in SSHKey.objects.all()
     ]
     self.assertItemsEqual(expected_sshkeys, handler.list({}))
Ejemplo n.º 3
0
 def test_create(self):
     user = factory.make_User()
     handler = SSHKeyHandler(user, {}, None)
     key_string = get_data("data/test_rsa0.pub")
     new_sshkey = handler.create({"key": key_string})
     self.assertThat(
         new_sshkey,
         ContainsDict({"user": Equals(user.id), "key": Equals(key_string)}),
     )
Ejemplo n.º 4
0
 def test_import_keys_calls_save_keys_for_user(self):
     user = factory.make_User()
     handler = SSHKeyHandler(user, {})
     protocol = factory.make_name("protocol")
     auth_id = factory.make_name("auth")
     mock_save_keys = self.patch(KeySource.objects, "save_keys_for_user")
     handler.import_keys({
         "protocol": protocol,
         "auth_id": auth_id,
     })
     self.assertThat(
         mock_save_keys,
         MockCalledOnceWith(user=user, protocol=protocol, auth_id=auth_id))
Ejemplo n.º 5
0
 def test_import_keys_calls_save_keys_for_user_and_create_audit_event(self):
     user = factory.make_User()
     handler = SSHKeyHandler(user, {}, None)
     protocol = factory.make_name("protocol")
     auth_id = factory.make_name("auth")
     mock_save_keys = self.patch(KeySource.objects, "save_keys_for_user")
     handler.import_keys({"protocol": protocol, "auth_id": auth_id})
     self.assertThat(
         mock_save_keys,
         MockCalledOnceWith(user=user, protocol=protocol, auth_id=auth_id),
     )
     event = Event.objects.get(type__level=AUDIT)
     self.assertIsNotNone(event)
     self.assertEqual(event.description, "Imported SSH keys.")
Ejemplo n.º 6
0
 def test_get_doesnt_work_if_not_owned(self):
     user = factory.make_User()
     handler = SSHKeyHandler(user, {}, None)
     not_owned_sshkey = factory.make_SSHKey(factory.make_User())
     self.assertRaises(
         HandlerDoesNotExistError, handler.get, {"id": not_owned_sshkey.id}
     )
Ejemplo n.º 7
0
 def test_import_keys_raises_HandlerError(self):
     user = factory.make_User()
     handler = SSHKeyHandler(user, {}, None)
     protocol = factory.make_name("protocol")
     auth_id = factory.make_name("auth")
     mock_save_keys = self.patch(KeySource.objects, "save_keys_for_user")
     mock_save_keys.side_effect = ImportSSHKeysError()
     self.assertRaises(HandlerError, handler.import_keys, {
         "protocol": protocol,
         "auth_id": auth_id,
     })
Ejemplo n.º 8
0
 def test_delete(self):
     user = factory.make_User()
     sshkey = factory.make_SSHKey(user=user)
     handler = SSHKeyHandler(user, {}, None)
     handler.delete({"id": sshkey.id})
     self.assertIsNone(get_one(SSHKey.objects.filter(id=sshkey.id)))