Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7]
python-version: [3.7, 3.10.1]
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
333 changes: 332 additions & 1 deletion poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mypy = "^0.910"
yapf = "^0.31.0"
pytest-mock = "^3.6.1"
pytest-cov = "^3.0.0"
pytest-vcr = "^1.0.2"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
65 changes: 65 additions & 0 deletions tests/cassettes/test_qiita_create_caller.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
interactions:
- request:
body: null
headers:
Authorization:
- Bearer fcb2e4a0658f103e4921e684c27d2822e12eebdb
Cache-Control:
- no-cache, no-store
Connection:
- close
Host:
- qiita.com
User-Agent:
- Python-urllib/3.10
method: GET
uri: https://qiita.com/api/v2/authenticated_user
response:
body:
string: '{"description":"Programmer","facebook_id":"","followees_count":2,"followers_count":1,"github_login_name":"ryokat3","id":"ryokat3","items_count":9,"linkedin_id":"","location":"Japan","name":"Ryoji
Kato","organization":"","permanent_id":115148,"profile_image_url":"https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/115148/profile-images/1641617983","team_only":false,"twitter_screen_name":null,"website_url":"","image_monthly_upload_limit":104857600,"image_monthly_upload_remaining":104857600}'
headers:
Cache-Control:
- max-age=0, private, must-revalidate
Connection:
- close
Content-Type:
- application/json; charset=utf-8
Date:
- Sun, 16 Jan 2022 14:29:36 GMT
ETag:
- W/"2aaf04820679c58d926153e93c3c317f"
Rate-Limit:
- '1000'
Rate-Remaining:
- '915'
Rate-Reset:
- '1642344297'
Referrer-Policy:
- strict-origin-when-cross-origin
Server:
- nginx
Strict-Transport-Security:
- max-age=2592000
Transfer-Encoding:
- chunked
Vary:
- Origin
X-Content-Type-Options:
- nosniff
X-Download-Options:
- noopen
X-Frame-Options:
- SAMEORIGIN
X-Permitted-Cross-Domain-Policies:
- none
X-Request-Id:
- 4d77dcb7-9de1-4b95-b732-8ebb8bfd4a87
X-Runtime:
- '0.093739'
X-XSS-Protection:
- 1; mode=block
status:
code: 200
message: OK
version: 1
2,622 changes: 2,622 additions & 0 deletions tests/cassettes/test_subcommand_check.yaml

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions tests/cassettes/test_subcommand_purge.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
interactions:
- request:
body: null
headers:
Authorization:
- Bearer fcb2e4a0658f103e4921e684c27d2822e12eebdb
Cache-Control:
- no-cache, no-store
Connection:
- close
Host:
- qiita.com
User-Agent:
- Python-urllib/3.10
method: GET
uri: https://qiita.com/api/v2/items/1234567890ABCDEFG
response:
body:
string: '{"message":"Not found","type":"not_found"}'
headers:
Connection:
- close
Content-Type:
- application/json
Date:
- Sun, 16 Jan 2022 14:33:42 GMT
Rate-Limit:
- '1000'
Rate-Remaining:
- '901'
Rate-Reset:
- '1642344296'
Server:
- nginx
Strict-Transport-Security:
- max-age=2592000
Transfer-Encoding:
- chunked
Vary:
- Origin
X-Request-Id:
- 929cfe73-9358-49e5-bd0c-770dbcd1ef21
X-Runtime:
- '0.221728'
status:
code: 404
message: Not Found
version: 1
5,242 changes: 5,242 additions & 0 deletions tests/cassettes/test_subcommand_show_diff.yaml

Large diffs are not rendered by default.

5,242 changes: 5,242 additions & 0 deletions tests/cassettes/test_subcommand_sync.yaml

Large diffs are not rendered by default.

128 changes: 128 additions & 0 deletions tests/cassettes/test_subcommand_upload.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
interactions:
- request:
body: '{"body": "![img1](https://raw.githubusercontent.com/github-id/github-repo/master/img1.png)",
"tags": [{"name": "test", "versions": []}], "title": "md3", "private": true}'
headers:
Authorization:
- Bearer fcb2e4a0658f103e4921e684c27d2822e12eebdb
Cache-Control:
- no-cache, no-store
Connection:
- close
Content-Length:
- '169'
Content-Type:
- application/json
Host:
- qiita.com
User-Agent:
- Python-urllib/3.10
method: POST
uri: https://qiita.com/api/v2/items
response:
body:
string: '{"rendered_body":"\u003cp\u003e\u003ca href=\"https://camo.qiitausercontent.com/d42dfcd6e60ca82bd25818138a4adf6a51e83e2f/68747470733a2f2f7261772e67697468756275736572636f6e74656e742e636f6d2f6769746875622d69642f6769746875622d7265706f2f6d61737465722f696d67312e706e67\"
target=\"_blank\" rel=\"nofollow noopener\"\u003e\u003cimg src=\"https://camo.qiitausercontent.com/d42dfcd6e60ca82bd25818138a4adf6a51e83e2f/68747470733a2f2f7261772e67697468756275736572636f6e74656e742e636f6d2f6769746875622d69642f6769746875622d7265706f2f6d61737465722f696d67312e706e67\"
alt=\"img1\" data-canonical-src=\"https://raw.githubusercontent.com/github-id/github-repo/master/img1.png\"\u003e\u003c/a\u003e\u003c/p\u003e\n","body":"![img1](https://raw.githubusercontent.com/github-id/github-repo/master/img1.png)\n","coediting":false,"comments_count":0,"created_at":"2022-01-16T23:33:40+09:00","group":null,"id":"74f0120f486b3ad86390","likes_count":0,"private":true,"reactions_count":0,"tags":[{"name":"test","versions":[]}],"title":"md3","updated_at":"2022-01-16T23:33:40+09:00","url":"https://qiita.com/ryokat3/private/74f0120f486b3ad86390","user":{"description":"Programmer","facebook_id":"","followees_count":2,"followers_count":1,"github_login_name":"ryokat3","id":"ryokat3","items_count":9,"linkedin_id":"","location":"Japan","name":"Ryoji
Kato","organization":"","permanent_id":115148,"profile_image_url":"https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/115148/profile-images/1641617983","team_only":false,"twitter_screen_name":null,"website_url":""},"page_views_count":null,"team_membership":null}'
headers:
Cache-Control:
- max-age=0, private, must-revalidate
Connection:
- close
Content-Type:
- application/json; charset=utf-8
Date:
- Sun, 16 Jan 2022 14:33:40 GMT
ETag:
- W/"587fd07f34a3268eff76294b588093ea"
Rate-Limit:
- '1000'
Rate-Remaining:
- '907'
Rate-Reset:
- '1642344296'
Referrer-Policy:
- strict-origin-when-cross-origin
Server:
- nginx
Strict-Transport-Security:
- max-age=2592000
Transfer-Encoding:
- chunked
Vary:
- Origin
X-Content-Type-Options:
- nosniff
X-Download-Options:
- noopen
X-Frame-Options:
- SAMEORIGIN
X-Permitted-Cross-Domain-Policies:
- none
X-Request-Id:
- 325b6c22-1d2e-4e5d-9dc5-e6638dc0a20a
X-Runtime:
- '0.261906'
X-XSS-Protection:
- 1; mode=block
status:
code: 201
message: Created
- request:
body: null
headers:
Authorization:
- Bearer fcb2e4a0658f103e4921e684c27d2822e12eebdb
Cache-Control:
- no-cache, no-store
Connection:
- close
Host:
- qiita.com
User-Agent:
- Python-urllib/3.10
method: DELETE
uri: https://qiita.com/api/v2/items/74f0120f486b3ad86390
response:
body:
string: ''
headers:
Cache-Control:
- no-cache
Connection:
- close
Date:
- Sun, 16 Jan 2022 14:33:40 GMT
Rate-Limit:
- '1000'
Rate-Remaining:
- '906'
Rate-Reset:
- '1642344296'
Referrer-Policy:
- strict-origin-when-cross-origin
Server:
- nginx
Strict-Transport-Security:
- max-age=2592000
Vary:
- Origin
X-Content-Type-Options:
- nosniff
X-Download-Options:
- noopen
X-Frame-Options:
- SAMEORIGIN
X-Permitted-Cross-Domain-Policies:
- none
X-Request-Id:
- 20b9cfd8-611d-42fe-81e1-fee5223dc470
X-Runtime:
- '0.214891'
X-XSS-Protection:
- 1; mode=block
status:
code: 204
message: No Content
version: 1
31 changes: 23 additions & 8 deletions tests/test_qiita_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from typing import Generator, List, Optional, NamedTuple, Dict, Callable
from dataclasses import dataclass

from qiita_sync.qiita_sync import ApplicationError, CommandError, GitHubArticle, QiitaArticle, QiitaSync, git_get_HEAD
from qiita_sync.qiita_sync import QIITA_API_ENDPOINT, ApplicationError, CommandError, GitHubArticle, QiitaArticle, QiitaSync, git_get_HEAD
from qiita_sync.qiita_sync import exec_command, qsync_get_access_token
from qiita_sync.qiita_sync import DEFAULT_ACCESS_TOKEN_FILE, DEFAULT_INCLUDE_GLOB, DEFAULT_EXCLUDE_GLOB
from qiita_sync.qiita_sync import GITHUB_REF, GITHUB_CONTENT_URL
from qiita_sync.qiita_sync import GITHUB_REF, GITHUB_CONTENT_URL, ACCESS_TOKEN_ENV
from qiita_sync.qiita_sync import qsync_init, qsync_argparse, Maybe
from qiita_sync.qiita_sync import rel_path, add_path, url_add_path, get_utc, str2bool, is_url
from qiita_sync.qiita_sync import git_get_topdir, git_get_remote_url, git_get_default_branch
Expand Down Expand Up @@ -209,13 +209,14 @@ def update_test_file(fp: Path):
# CLI Test
########################################################################


@pytest.mark.vcr()
def test_subcommand_download(topdir_fx: Path, mocker: MockerFixture):
get_qsync([MarkdownAsset("md2.md", gen_md2), MarkdownAsset("md3.md", gen_md3), Asset("img1.png")])
mocker.patch('sys.argv', ['qiita_sync.py', 'download', '.'])
qsync_main()


@pytest.mark.vcr()
def test_subcommand_check(topdir_fx: Path, mocker: MockerFixture, capsys: CaptureFixture):
get_qsync([MarkdownAsset("md2.md", gen_md2), MarkdownAsset("md3.md", gen_md3), Asset("img1.png")])
article2 = GitHubArticle.fromFile(topdir_fx.joinpath("md2.md"))
Expand All @@ -229,6 +230,7 @@ def test_subcommand_check(topdir_fx: Path, mocker: MockerFixture, capsys: Captur
assert qsync_str_local_only(article3) in captured.out


@pytest.mark.vcr()
def test_subcommand_show_diff(topdir_fx: Path, mocker: MockerFixture, capsys: CaptureFixture):
mocker.patch('sys.argv', ['qiita_sync.py', 'sync', str(topdir_fx)])
qsync_main()
Expand All @@ -246,6 +248,7 @@ def test_subcommand_show_diff(topdir_fx: Path, mocker: MockerFixture, capsys: Ca
assert 'Local is new' in captured.out


@pytest.mark.vcr()
def test_subcommand_upload(topdir_fx: Path, mocker: MockerFixture, capsys: CaptureFixture):
get_qsync([MarkdownAsset("md3.md", gen_md3), Asset("img1.png")])
target = topdir_fx.joinpath("md3.md")
Expand All @@ -263,6 +266,7 @@ def test_subcommand_upload(topdir_fx: Path, mocker: MockerFixture, capsys: Captu
qsync_main()


@pytest.mark.vcr()
def test_subcommand_sync(topdir_fx: Path, mocker: MockerFixture, capsys: CaptureFixture):
mocker.patch('sys.argv', ['qiita_sync.py', 'sync', str(topdir_fx)])
qsync_main()
Expand All @@ -274,6 +278,7 @@ def test_subcommand_sync(topdir_fx: Path, mocker: MockerFixture, capsys: Capture
assert "" == captured.out


@pytest.mark.vcr()
def test_subcommand_purge(topdir_fx: Path, mocker: MockerFixture, capsys: CaptureFixture):
get_qsync([MarkdownAsset("md2.md", gen_md2), MarkdownAsset("md3.md", gen_md3), Asset("img1.png")])
target = topdir_fx.joinpath("md2.md")
Expand Down Expand Up @@ -443,11 +448,21 @@ def test_git_get_HEAD():
########################################################################


def test_qsync():
# Get token from environment variable
access_token = qsync_get_access_token('foobar')
caller = qiita_create_caller(access_token)
print(qiita_get_authenticated_user_id(caller))
def test_qsync_get_access_token(topdir_fx: Path):
token_file = 'access_token.txt'
access_token = os.environ.get(ACCESS_TOKEN_ENV)
if access_token is not None:
with topdir_fx.joinpath(token_file).open('w') as fp:
fp.write(access_token)
assert access_token == qsync_get_access_token(token_file)
assert access_token == qsync_get_access_token("hehe.txt")


@pytest.mark.vcr()
def test_qiita_create_caller(topdir_fx: Path):
caller = qiita_create_caller(qsync_get_access_token("hehe.txt"))
id = qiita_get_authenticated_user_id(caller)
assert id == "ryokat3"


########################################################################
Expand Down