Repository: BrandwatchLtd/api_sdk

Issue:
username with capital letters
I came across an issue when I use the token_path instead of the password to request my project:
BWProject(username=YOUR_ACCOUNT, project=YOUR_PROJECT)

When at the beginning I used  my username and password, the tokens.txt was generated with the username in lowercase (_read_auth_file). 
On the other hand, my username contains capital letters, meaning my user´s json will be returned with these capital letters in the username field, so the _test_auth() method will fail in bwproject.py.

I can think of ways to get around it by making it case sensitive, but I would appreciate some suggestions.

Many thanks in advance!


Discussion:

Patch:
diff --git a/.travis.yml b/.travis.yml
deleted file mode 100644
index b33d109..0000000
--- a/.travis.yml
+++ /dev/null
@@ -1,24 +0,0 @@
-language: python
-
-matrix:
-  include:
-    - python: 3.3
-    - python: 3.4
-    - python: 3.5
-    - python: 3.6
-    - python: 3.7
-      dist: xenial
-      sudo: true
-    - python: 3.7-dev
-      dist: xenial
-      sudo: true
-    - python: 3.8-dev
-      dist: xenial
-      sudo: true
-
-install:
-  - python setup.py install
-
-script:
-  - python setup.py test
-
diff --git a/README.md b/README.md
index 7084960..15c6827 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,3 @@
-[![Build Status](https://travis-ci.com/BrandwatchLtd/api_sdk.svg?branch=master)](https://travis-ci.com/BrandwatchLtd/api_sdk)
-
 # Brandwatch API SDK
 
 ## Introduction
diff --git a/bwproject.py b/bwproject.py
index c879cae..3a41d39 100644
--- a/bwproject.py
+++ b/bwproject.py
@@ -59,7 +59,7 @@ class BWUser:
         if "username" in user:
             if username is None:
                 return user["username"], token
-            elif user["username"] == username:
+            elif user["username"].lower() == username.lower():
                 return username, token
             else:
                 raise KeyError("Username " + username + " does not match provided token", user)
@@ -84,8 +84,8 @@ class BWUser:
 
     def _read_auth(self, username, token_path):
         user_tokens = self._read_auth_file(token_path)
-        if username in user_tokens:
-            return self._test_auth(username, user_tokens[username])
+        if username.lower() in user_tokens:
+            return self._test_auth(username, user_tokens[username.lower()])
         else:
             raise KeyError("Token not found in file: " + token_path)
 
diff --git a/setup.py b/setup.py
index 9c1d705..56c9ab2 100644
--- a/setup.py
+++ b/setup.py
@@ -23,15 +23,16 @@ setup(
         'License :: OSI Approved :: MIT License',
 
         'Programming Language :: Python :: 3',
+        'Programming Language :: Python :: 3.2',
         'Programming Language :: Python :: 3.3',
         'Programming Language :: Python :: 3.4',
-        'Programming Language :: Python :: 3.5',
-        'Programming Language :: Python :: 3.6',
-        'Programming Language :: Python :: 3.7'
+        'Programming Language :: Python :: 3.5'
     ],
 
     py_modules=['bwproject', 'bwresources', 'bwdata', 'filters'],
 
-    install_requires=['requests']
+    install_requires=['requests'],
+
+    tests_require=['responses']
 
 )


Test patch:
diff --git a/test/__init__.py b/test/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/test/test_bwproject.py b/test/test_bwproject.py
new file mode 100644
index 0000000..4820262
--- /dev/null
+++ b/test/test_bwproject.py
@@ -0,0 +1,69 @@
+import unittest
+import responses
+import os
+import tempfile
+
+from bwproject import BWProject
+
+
+class TestBWProjectUsernameCaseSensitivity(unittest.TestCase):
+
+    USERNAME = "example@Example.com"
+    ACCESS_TOKEN = "00000000-0000-0000-0000-000000000000"
+    PROJECT_NAME = "Example project"
+    PROJECTS = [
+        {
+            "id": 0,
+            "name": PROJECT_NAME,
+            "description": "",
+            "billableClientId": 0,
+            "billableClientName": "My company",
+            "timezone": "Africa/Abidjan",
+            "billableClientIsPitch": False
+        }
+    ]
+
+    def setUp(self):
+        self.token_path = tempfile.NamedTemporaryFile(suffix='-tokens.txt').name
+
+        responses.add(responses.GET, 'https://api.brandwatch.com/projects',
+                      json={
+                          "resultsTotal": len(self.PROJECTS),
+                          "resultsPage": -1,
+                          "resultsPageSize": -1,
+                          "results": self.PROJECTS
+                      }, status=200)
+
+        responses.add(responses.POST, 'https://api.brandwatch.com/oauth/token',
+                      json={'access_token': self.ACCESS_TOKEN}, status=200)
+
+    def tearDown(self):
+        os.unlink(self.token_path)
+        responses.reset()
+
+
+    @responses.activate
+    def test_lowercase_username(self):
+        self._test_username("example@example.com")
+
+    @responses.activate
+    def test_uppercase_username(self):
+        self._test_username("EXAMPLE@EXAMPLE.COM")
+
+    @responses.activate
+    def test_mixedcase_username(self):
+        self._test_username("eXaMpLe@ExAmPlE.cOm")
+
+    def _test_username(self, username):
+
+        responses.add(responses.GET, 'https://api.brandwatch.com/me', json={"username": username}, status=200)
+
+        BWProject(username=username, project=self.PROJECT_NAME, password="", token_path=self.token_path)
+        try:
+            BWProject(username=username, project=self.PROJECT_NAME, token_path=self.token_path)
+        except KeyError as e:
+            self.fail(e)
+
+
+if __name__ == '__main__':
+    unittest.main()