From 6cd525dcbcd2d90b8b13f24c5507b10c9218a72a Mon Sep 17 00:00:00 2001 From: Wen Liang Date: Tue, 3 Dec 2024 20:50:57 +0800 Subject: [PATCH] test: test favors login pixiv --- src/favorites_crawler/commands/login.py | 1 + tests/test_commands/test_login.py | 107 ++++++++++++++++++++---- 2 files changed, 90 insertions(+), 18 deletions(-) diff --git a/src/favorites_crawler/commands/login.py b/src/favorites_crawler/commands/login.py index a46d4f3..b030a19 100644 --- a/src/favorites_crawler/commands/login.py +++ b/src/favorites_crawler/commands/login.py @@ -49,6 +49,7 @@ def login_pixiv( pixiv_config['REFRESH_TOKEN'] = login_info['refresh_token'] except KeyError as e: print(f'Failed to login. {e!r}') + exit(1) else: dump_config(config, favors_home) print("Login successful.") diff --git a/tests/test_commands/test_login.py b/tests/test_commands/test_login.py index 721e2b8..f00fecb 100644 --- a/tests/test_commands/test_login.py +++ b/tests/test_commands/test_login.py @@ -1,3 +1,5 @@ +from unittest.mock import patch + from typer.testing import CliRunner from favorites_crawler import app @@ -6,6 +8,74 @@ runner = CliRunner() +@patch('favorites_crawler.commands.login.CustomGetPixivToken') +class TestLoginPixiv: + username = 'username' + password = 'password' + user_id = 'user_id' + access_token = 'access_token' + refresh_token = 'refresh_token' + + def test_login_pixiv_success(self, mock_gppt, tmp_path): + favors_home = tmp_path / 'home' + mock_login = mock_gppt.return_value.login + mock_login.return_value = { + 'access_token': self.access_token, + 'refresh_token': self.refresh_token, + 'user': {'id': self.user_id} + } + + result = runner.invoke( + app, ['login', 'pixiv', '-u', self.username, '-p', self.password], + env={'FAVORS_HOME': str(favors_home)} + ) + + mock_login.assert_called_once_with(username=self.username, password=self.password) + assert result.exit_code == 0 + assert "success" in result.stdout + assert (favors_home / 'config.yml').exists() + config = load_config(favors_home)['pixiv'] + assert config['USER_ID'] == self.user_id + assert config['REFRESH_TOKEN'] == self.refresh_token + assert config['ACCESS_TOKEN'] == self.access_token + + def test_login_pixiv_should_failed_when_gppt_raise_exc(self, mock_gppt, tmp_path): + favors_home = tmp_path / 'home' + mock_login = mock_gppt.return_value.login + mock_login.side_effect = Exception + + result = runner.invoke( + app, ['login', 'pixiv', '-u', self.username, '-p', self.password], + env={'FAVORS_HOME': str(favors_home)} + ) + + mock_login.assert_called_once_with(username=self.username, password=self.password) + self.assert_login_failed(result, favors_home) + + def test_login_pixiv_should_failed_when_gppt_return_bad_resp(self, mock_gppt, tmp_path): + favors_home = tmp_path / 'home' + mock_login = mock_gppt.return_value.login + mock_login.return_value = {} + + result = runner.invoke( + app, ['login', 'pixiv', '-u', self.username, '-p', self.password], + env={'FAVORS_HOME': str(favors_home)} + ) + + mock_login.assert_called_once_with(username=self.username, password=self.password) + self.assert_login_failed(result, favors_home) + + @staticmethod + def assert_login_failed(result, favors_home): + assert result.exit_code == 1 + assert "Failed" in result.stdout + assert (favors_home / 'config.yml').exists() + config = load_config(favors_home)['pixiv'] + assert config['USER_ID'] == '' + assert config['REFRESH_TOKEN'] == '' + assert config['ACCESS_TOKEN'] == '' + + class TestLoginYandere: def test_login_yandere_success(self, tmp_path): favors_home = tmp_path / 'home' @@ -19,8 +89,8 @@ def test_login_yandere_success(self, tmp_path): assert result.exit_code == 0 assert "success" in result.stdout assert (favors_home / 'config.yml').exists() - config = load_config(favors_home) - assert config['yandere']['USERNAME'] == username + config = load_config(favors_home)['yandere'] + assert config['USERNAME'] == username class TestLoginNhentai: @@ -40,8 +110,8 @@ def test_login_nhentai_success(self, tmp_path): assert "success" in result.stdout assert (favors_home / 'cookie.txt').exists() assert (favors_home / 'config.yml').exists() - config = load_config(favors_home) - assert config['nhentai']['USER_AGENT'] == self.user_agent + config = load_config(favors_home)['nhentai'] + assert config['USER_AGENT'] == self.user_agent def test_login_nhentai_failed(self, tmp_path): favors_home = tmp_path / 'home' @@ -56,8 +126,8 @@ def test_login_nhentai_failed(self, tmp_path): assert "Failed" in result.stdout assert not (favors_home / 'cookie.txt').exists() assert (favors_home / 'config.yml').exists() - config = load_config(favors_home) - assert config['nhentai']['USER_AGENT'] == '' + config = load_config(favors_home)['nhentai'] + assert config['USER_AGENT'] == '' class TestLoginTwitter: @@ -81,11 +151,11 @@ def test_login_twitter_success(self, tmp_path): assert "success" in result.stdout assert (favors_home / 'cookie.txt').exists() assert (favors_home / 'config.yml').exists() - config = load_config(favors_home) - assert config['twitter']['AUTHORIZATION'] == self.access_token - assert config['twitter']['X_CSRF_TOKEN'] == self.csrf_token - assert config['twitter']['LIKES_ID'] == 'likes_id' - assert config['twitter']['FEATURES'] == { + config = load_config(favors_home)['twitter'] + assert config['AUTHORIZATION'] == self.access_token + assert config['X_CSRF_TOKEN'] == self.csrf_token + assert config['LIKES_ID'] == 'likes_id' + assert config['FEATURES'] == { 'profile_label_improvements_pcf_label_in_post_enabled': False, 'rweb_tipjar_consumption_enabled': True, 'responsive_web_graphql_exclude_directive_enabled': True, @@ -111,7 +181,7 @@ def test_login_twitter_success(self, tmp_path): 'longform_notetweets_inline_media_enabled': True, 'responsive_web_enhance_cards_enabled': False } - assert config['twitter']['USER_ID'] == 'xxx' + assert config['USER_ID'] == 'xxx' def test_login_twitter_failed_when_given_bad_url(self, tmp_path): favors_home = tmp_path / 'home' @@ -138,13 +208,14 @@ def test_login_twitter_failed_when_cookie_not_exists(self, tmp_path): self.assert_login_failed(result, favors_home) - def assert_login_failed(self, result, favors_home): + @staticmethod + def assert_login_failed(result, favors_home): assert result.exit_code == 1 assert "Failed" in result.stdout assert not (favors_home / 'cookie.txt').exists() assert (favors_home / 'config.yml').exists() - config = load_config(favors_home) - assert config['twitter']['AUTHORIZATION'] == '' - assert config['twitter']['X_CSRF_TOKEN'] == '' - assert config['twitter']['LIKES_ID'] == '' - assert config['twitter']['USER_ID'] == '' + config = load_config(favors_home)['twitter'] + assert config['AUTHORIZATION'] == '' + assert config['X_CSRF_TOKEN'] == '' + assert config['LIKES_ID'] == '' + assert config['USER_ID'] == ''