conftest.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env python3
  2. # coding=utf-8
  3. import configparser
  4. import socket
  5. from pathlib import Path
  6. import praw
  7. import pytest
  8. from bdfr.oauth2 import OAuth2TokenManager
  9. @pytest.fixture(scope='session')
  10. def reddit_instance():
  11. rd = praw.Reddit(
  12. client_id='U-6gk4ZCh3IeNQ',
  13. client_secret='7CZHY6AmKweZME5s50SfDGylaPg',
  14. user_agent='test',
  15. )
  16. return rd
  17. @pytest.fixture(scope='session')
  18. def authenticated_reddit_instance():
  19. test_config_path = Path('./tests/test_config.cfg')
  20. if not test_config_path.exists():
  21. pytest.skip('Refresh token must be provided to authenticate with OAuth2')
  22. cfg_parser = configparser.ConfigParser()
  23. cfg_parser.read(test_config_path)
  24. if not cfg_parser.has_option('DEFAULT', 'user_token'):
  25. pytest.skip('Refresh token must be provided to authenticate with OAuth2')
  26. token_manager = OAuth2TokenManager(cfg_parser, test_config_path)
  27. reddit_instance = praw.Reddit(
  28. client_id=cfg_parser.get('DEFAULT', 'client_id'),
  29. client_secret=cfg_parser.get('DEFAULT', 'client_secret'),
  30. user_agent=socket.gethostname(),
  31. token_manager=token_manager,
  32. )
  33. return reddit_instance