Skip to content

Commit

Permalink
test: test favors login pixiv
Browse files Browse the repository at this point in the history
  • Loading branch information
RyouMon committed Dec 3, 2024
1 parent af64a2a commit 6cd525d
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/favorites_crawler/commands/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
107 changes: 89 additions & 18 deletions tests/test_commands/test_login.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from unittest.mock import patch

from typer.testing import CliRunner

from favorites_crawler import app
Expand All @@ -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'
Expand All @@ -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:
Expand All @@ -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'
Expand All @@ -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:
Expand All @@ -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,
Expand All @@ -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'
Expand All @@ -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'] == ''

0 comments on commit 6cd525d

Please sign in to comment.