tests for static parts of twitter package tests that twitter credentials from a file are handled correctly test that environment variable has been read correctly each of the following scenarios should raise an error an empty subdir path a subdir path of none a nonexistent directory credentials txt is not in default subdir as read from os environ twitter nonexistent credentials file foobar badoauth11 txt is incomplete the first key in credentials file badoauth12 txt is illformed the first two lines in badoauth13 txt are collapsed various scenarios that should raise errors try auth loadcredskwargs raises valueerror zero length field name in format for python 2 6 oserror for the rest except oserror valueerror pass except exception as e pytest failunexpected exception thrown s e else pytest failoserror exception not thrown def testcorrectfileself auth tests that twitter credentials from a file are handled correctly test that environment variable has been read correctly each of the following scenarios should raise an error an empty subdir path a subdir path of none a nonexistent directory credentials txt is not in default subdir as read from os environ twitter nonexistent credentials file foobar bad_oauth1 1 txt is incomplete the first key in credentials file bad_oauth1 2 txt is ill formed the first two lines in bad_oauth1 3 txt are collapsed various scenarios that should raise errors raises valueerror zero length field name in format for python 2 6 oserror for the rest test that a proper file succeeds and is read correctly
import os

import pytest

pytest.importorskip("twython")

from nltk.twitter import Authenticate


@pytest.fixture
def auth():
    return Authenticate()


class TestCredentials:
    

    @classmethod
    def setup_class(self):
        self.subdir = os.path.join(os.path.dirname(__file__), "files")
        os.environ["TWITTER"] = "twitter-files"

    def test_environment(self, auth):
        
        fn = os.path.basename(auth.creds_subdir)
        assert fn == os.environ["TWITTER"]

    @pytest.mark.parametrize(
        "kwargs",
        [
            
            
            {"subdir": ""},
            
            {"subdir": None},
            
            {"subdir": "/nosuchdir"},
            
            {},
            
            {"creds_file": "foobar"},
            
            {"creds_file": "bad_oauth1-1.txt"},
            
            {"creds_file": "bad_oauth1-2.txt"},
            
            {"creds_file": "bad_oauth1-3.txt"},
        ],
    )
    def test_scenarios_that_should_raise_errors(self, kwargs, auth):
        
        try:
            auth.load_creds(**kwargs)
        
        
        except (OSError, ValueError):
            pass
        except Exception as e:
            pytest.fail("Unexpected exception thrown: %s" % e)
        else:
            pytest.fail("OSError exception not thrown.")

    def test_correct_file(self, auth):
        
        oauth = auth.load_creds(subdir=self.subdir)

        assert auth.creds_fullpath == os.path.join(self.subdir, auth.creds_file)
        assert auth.creds_file == "credentials.txt"
        assert oauth["app_key"] == "a"