Move scope regex parsing
This commit is contained in:
parent
dd1831b0ea
commit
7c2b7b0e83
|
@ -4,6 +4,7 @@
|
||||||
import configparser
|
import configparser
|
||||||
import logging
|
import logging
|
||||||
import random
|
import random
|
||||||
|
import re
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
import praw
|
import praw
|
||||||
|
@ -21,7 +22,7 @@ class OAuth2Authenticator:
|
||||||
self.scopes = wanted_scopes
|
self.scopes = wanted_scopes
|
||||||
|
|
||||||
@staticmethod
|
@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',
|
response = requests.get('https://www.reddit.com/api/v1/scopes.json',
|
||||||
headers={'User-Agent': 'fetch-scopes test'})
|
headers={'User-Agent': 'fetch-scopes test'})
|
||||||
known_scopes = [scope for scope, data in response.json().items()]
|
known_scopes = [scope for scope, data in response.json().items()]
|
||||||
|
@ -30,6 +31,11 @@ class OAuth2Authenticator:
|
||||||
if scope not in known_scopes:
|
if scope not in known_scopes:
|
||||||
raise BulkDownloaderException(f'Scope {scope} is not known to reddit')
|
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:
|
def retrieve_new_token(self) -> str:
|
||||||
reddit = praw.Reddit(redirect_uri='http://localhost:8080', user_agent='obtain_refresh_token for BDFR')
|
reddit = praw.Reddit(redirect_uri='http://localhost:8080', user_agent='obtain_refresh_token for BDFR')
|
||||||
state = str(random.randint(0, 65000))
|
state = str(random.randint(0, 65000))
|
||||||
|
|
|
@ -4,7 +4,6 @@
|
||||||
import configparser
|
import configparser
|
||||||
from unittest.mock import MagicMock
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
import praw
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from bulkredditdownloader.exceptions import BulkDownloaderException
|
from bulkredditdownloader.exceptions import BulkDownloaderException
|
||||||
|
@ -30,6 +29,17 @@ def test_check_scopes(test_scopes: list[str]):
|
||||||
OAuth2Authenticator._check_scopes(test_scopes)
|
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.online
|
||||||
@pytest.mark.parametrize('test_scopes', (
|
@pytest.mark.parametrize('test_scopes', (
|
||||||
('random',),
|
('random',),
|
||||||
|
|
Loading…
Reference in a new issue