def test_error_gpg(self, popen, user_exists): user_exists.return_value = False err_msg = "Error message 123" popen.return_value = MockPopenHandle(1, stderr=err_msg) with pytest.raises(GpgErrorException) as e: logic.create_new_key(app, TEST_NAME, TEST_EMAIL, TEST_KEYLENGTH) assert e.message == err_msg
def test_strange_situation_create(self, popen, user_exists): """ After key generation `user_exists` invoked again, and if it doesn't see key it should raise an error """ user_exists.return_value = False popen.return_value = MockPopenHandle(0) with pytest.raises(GpgErrorException): logic.create_new_key(app, TEST_NAME, TEST_EMAIL, TEST_KEYLENGTH)
def test_tmpfiles_errors(self, popen, user_exists): user_exists.return_value = False with mock.patch("tempfile.NamedTemporaryFile") as tmpfile: tmpfile.side_effect = OSError() with pytest.raises(KeygenServiceBaseException) as e: logic.create_new_key(app, TEST_NAME, TEST_EMAIL, TEST_KEYLENGTH) assert not popen.called with mock.patch("tempfile.NamedTemporaryFile") as tmpfile: tmpfile.return_value.write.side_effect = OSError() with pytest.raises(KeygenServiceBaseException) as e: logic.create_new_key(app, TEST_NAME, TEST_EMAIL, TEST_KEYLENGTH) assert not popen.called
def test_simple_create(self, popen, user_exists): """ Check correct key generation. """ with mock.patch("tempfile.NamedTemporaryFile") as tmpfile: def check_gpg_genkey_file_exists(*args, **kwargs): assert tmpfile.called return MockPopenHandle(0) popen.side_effect = check_gpg_genkey_file_exists res = logic.create_new_key(app, TEST_NAME, TEST_EMAIL, TEST_KEYLENGTH) assert res is None
def test_simple_create(self, popen, user_exists): """ Check correct key generation. At first user not exist, but after popen call, it exists """ with mock.patch("tempfile.NamedTemporaryFile") as tmpfile: user_exists_returns = [False, True] user_exists.side_effect = \ lambda *args, **kwargs: user_exists_returns.pop(0) def check_gpg_genkey_file_exists(*args, **kwargs): assert tmpfile.called return MockPopenHandle(0) popen.side_effect = check_gpg_genkey_file_exists res = logic.create_new_key(app, TEST_NAME, TEST_EMAIL, TEST_KEYLENGTH) assert res is None
def test_error_popen(self, popen, user_exists): user_exists.return_value = False popen.side_effect = OSError() with pytest.raises(GpgErrorException): logic.create_new_key(app, TEST_NAME, TEST_EMAIL, TEST_KEYLENGTH)
def test_skip_creation_for_existing_user(self, popen, user_exists): user_exists.return_value = True logic.create_new_key(app, TEST_NAME, TEST_EMAIL, TEST_KEYLENGTH) user_exists.assert_called_once() assert not popen.called