1
0
Fork 0
mirror of synced 2024-06-26 10:00:20 +12:00

Move scope regex parsing

This commit is contained in:
Serene-Arc 2021-03-08 12:34:03 +10:00 committed by Ali Parlakci
parent dd1831b0ea
commit 7c2b7b0e83
2 changed files with 18 additions and 2 deletions

View file

@ -4,6 +4,7 @@
import configparser
import logging
import random
import re
import socket
import praw
@ -21,7 +22,7 @@ class OAuth2Authenticator:
self.scopes = wanted_scopes
@staticmethod
def _check_scopes(wanted_scopes: list[str]):
def _check_scopes(wanted_scopes: set[str]):
response = requests.get('https://www.reddit.com/api/v1/scopes.json',
headers={'User-Agent': 'fetch-scopes test'})
known_scopes = [scope for scope, data in response.json().items()]
@ -30,6 +31,11 @@ class OAuth2Authenticator:
if scope not in known_scopes:
raise BulkDownloaderException(f'Scope {scope} is not known to reddit')
@staticmethod
def split_scopes(scopes: str) -> set[str]:
scopes = re.split(r'[,: ]+', scopes)
return set(scopes)
def retrieve_new_token(self) -> str:
reddit = praw.Reddit(redirect_uri='http://localhost:8080', user_agent='obtain_refresh_token for BDFR')
state = str(random.randint(0, 65000))

View file

@ -4,7 +4,6 @@
import configparser
from unittest.mock import MagicMock
import praw
import pytest
from bulkredditdownloader.exceptions import BulkDownloaderException
@ -30,6 +29,17 @@ def test_check_scopes(test_scopes: list[str]):
OAuth2Authenticator._check_scopes(test_scopes)
@pytest.mark.parametrize(('test_scopes', 'expected'), (
('history', {'history', }),
('history creddits', {'history', 'creddits'}),
('history, creddits, account', {'history', 'creddits', 'account'}),
('history,creddits,account,flair', {'history', 'creddits', 'account', 'flair'}),
))
def test_split_scopes(test_scopes: str, expected: set[str]):
result = OAuth2Authenticator.split_scopes(test_scopes)
assert result == expected
@pytest.mark.online
@pytest.mark.parametrize('test_scopes', (
('random',),