From 2695a809f2bdf5846dd5097fd1aef7fa59c60fba Mon Sep 17 00:00:00 2001 From: "Jules Lasne (jlasne)" Date: Tue, 2 Jan 2018 11:03:58 +0100 Subject: [PATCH 1/7] [STAGE] pushing minor modifications on the test branch Tried to do something with mocks, doesnt work ATM because of the way classes and init works --- tests/test_IntraPy.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 tests/test_IntraPy.py diff --git a/tests/test_IntraPy.py b/tests/test_IntraPy.py new file mode 100644 index 0000000..7bb153a --- /dev/null +++ b/tests/test_IntraPy.py @@ -0,0 +1,29 @@ +from unittest import TestCase +from unittest.mock import Mock, patch +from nose.tools import assert_list_equal +from nose.tools import assert_is_none +from IntraPy.IntraPy import IntraPy + + +class TestIntraPy(TestCase): + @patch('IntraPy.IntraPy.requests.request') + def test_api_request_new_token(self, mock_get): + fake_return = [{ + 'access_token': 'df9580fc7b3c3501cb39646a89050c774ee3a43b3e55633345ef931ffecdf6e6', + 'token_type': 'bearer', + 'expires_in': 7200, + 'scope': 'public', + 'created_at': 1514886496 + }] + mock_get.return_value = Mock(ok=True) + mock_get.return_value.json.return_value = fake_return + intra = IntraPy() + response = intra.api_request_new_token() + assert_list_equal(response, fake_return[0]['access_token']) + + @patch('IntraPy.IntraPy.requests.request') + def test_api_request_new_token_fail(self, mock_get): + mock_get.return_value.ok = False + intra = IntraPy() + response = intra.api_request_new_token() + assert_is_none(response) From 46dcb55ab8c8dea495a09caa003cfac7f6310b5b Mon Sep 17 00:00:00 2001 From: "Jules Lasne (jlasne)" Date: Fri, 19 Jan 2018 14:05:03 +0100 Subject: [PATCH 2/7] [FEATURE] Started adding test to try code coverage --- .gitignore | 1 + .travis.yml | 15 +++++++++++ tests/__init__.py | 0 tests/test_IntraPy.py | 61 ++++++++++++++++++++++++++----------------- 4 files changed, 53 insertions(+), 24 deletions(-) create mode 100644 .travis.yml create mode 100644 tests/__init__.py diff --git a/.gitignore b/.gitignore index e174c2d..ead7ec4 100644 --- a/.gitignore +++ b/.gitignore @@ -106,3 +106,4 @@ ENV/ settings.ini MANIFEST example.py +nosetests diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..975d6c2 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,15 @@ +sudo: false + +language: python + +python: + - 3.6.3 + +install: + - pip install codecov + +script: + - nosetests --with-coverage --verbosity=2 + +after_success: +- codecov \ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_IntraPy.py b/tests/test_IntraPy.py index 7bb153a..19b2f50 100644 --- a/tests/test_IntraPy.py +++ b/tests/test_IntraPy.py @@ -1,29 +1,42 @@ -from unittest import TestCase +""" + A python Library to help make python apps/bots using the 42 API + Copyright (C) 2017-2018 Jules LASNE jlasne@student.42.fr + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +""" +from nose.tools import assert_is_not_none from unittest.mock import Mock, patch -from nose.tools import assert_list_equal -from nose.tools import assert_is_none from IntraPy.IntraPy import IntraPy -class TestIntraPy(TestCase): - @patch('IntraPy.IntraPy.requests.request') - def test_api_request_new_token(self, mock_get): - fake_return = [{ - 'access_token': 'df9580fc7b3c3501cb39646a89050c774ee3a43b3e55633345ef931ffecdf6e6', - 'token_type': 'bearer', - 'expires_in': 7200, - 'scope': 'public', - 'created_at': 1514886496 - }] - mock_get.return_value = Mock(ok=True) - mock_get.return_value.json.return_value = fake_return - intra = IntraPy() - response = intra.api_request_new_token() - assert_list_equal(response, fake_return[0]['access_token']) +@patch('IntraPy.IntraPy.requests.request') +def test_api_with_good_url(mock_get): + # Configure the mock to return a response with an OK status code. + mock_get.return_value.ok = True + # Call the service, which will send a request to the server. + api = IntraPy() + response = api.api_get_single("/v2/achievements") + # If the request is sent successfully, then I expect a response to be returned. + assert_is_not_none(response) + - @patch('IntraPy.IntraPy.requests.request') - def test_api_request_new_token_fail(self, mock_get): - mock_get.return_value.ok = False - intra = IntraPy() - response = intra.api_request_new_token() - assert_is_none(response) +@patch('IntraPy.IntraPy.requests.request') +def test_api_with_bad_url(mock_get): + # Configure the mock to return a response with an OK status code. + mock_get.return_value.ok = False + # Call the service, which will send a request to the server. + api = IntraPy() + response = api.api_get_single("/v2/thisdoesntexist") + # If the request is sent successfully, then I expect a response to be returned. + assert_is_not_none(response) \ No newline at end of file From 4dc7f4c44fee538e5fff79998ddaed66ae65df71 Mon Sep 17 00:00:00 2001 From: "Jules Lasne (jlasne)" Date: Fri, 19 Jan 2018 14:06:53 +0100 Subject: [PATCH 3/7] [FIX] Forgot to add the install of requirements --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 975d6c2..ca78389 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,7 @@ python: - 3.6.3 install: + - pip install -r requirements.txt - pip install codecov script: From 43b427c53bf08372a2c3ed607c749977e776e054 Mon Sep 17 00:00:00 2001 From: "Jules Lasne (jlasne)" Date: Fri, 19 Jan 2018 14:18:22 +0100 Subject: [PATCH 4/7] [FIX] Fixed app uid and secret error as they were'nt present on travis --- .travis.yml | 1 + scripts/create_settings.sh | 6 ++++++ 2 files changed, 7 insertions(+) create mode 100644 scripts/create_settings.sh diff --git a/.travis.yml b/.travis.yml index ca78389..3dc6062 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,6 +8,7 @@ python: install: - pip install -r requirements.txt - pip install codecov + - bash scripts/create_settings.sh script: - nosetests --with-coverage --verbosity=2 diff --git a/scripts/create_settings.sh b/scripts/create_settings.sh new file mode 100644 index 0000000..9faabdf --- /dev/null +++ b/scripts/create_settings.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +cat "[settings]" > settings.ini +cat "APP_UID=$APP_UID" >> settings.ini +cat "APP_SECRET=$APP_SECRET" >> settings.ini +cat "TOKEN_FILE=$TOKEN_FILE" >> settings.ini \ No newline at end of file From 7a6f5a1f053dda99d6ba19e7e801f52925b02398 Mon Sep 17 00:00:00 2001 From: "Jules Lasne (jlasne)" Date: Fri, 19 Jan 2018 14:19:47 +0100 Subject: [PATCH 5/7] [WOW] Such tired, much wow --- scripts/create_settings.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/create_settings.sh b/scripts/create_settings.sh index 9faabdf..52aef69 100644 --- a/scripts/create_settings.sh +++ b/scripts/create_settings.sh @@ -1,6 +1,6 @@ #!/bin/bash -cat "[settings]" > settings.ini -cat "APP_UID=$APP_UID" >> settings.ini -cat "APP_SECRET=$APP_SECRET" >> settings.ini -cat "TOKEN_FILE=$TOKEN_FILE" >> settings.ini \ No newline at end of file +echo "[settings]" > settings.ini +echo "APP_UID=$APP_UID" >> settings.ini +echo "APP_SECRET=$APP_SECRET" >> settings.ini +echo "TOKEN_FILE=$TOKEN_FILE" >> settings.ini \ No newline at end of file From b56f2196b51fa2fa79e58322c2203e889fc68185 Mon Sep 17 00:00:00 2001 From: "Jules Lasne (jlasne)" Date: Sun, 21 Jan 2018 15:36:24 +0100 Subject: [PATCH 6/7] [STAGE] Staged a TODO --- IntraPy/IntraPy.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/IntraPy/IntraPy.py b/IntraPy/IntraPy.py index 01c2ad3..95e27c5 100644 --- a/IntraPy/IntraPy.py +++ b/IntraPy/IntraPy.py @@ -143,6 +143,10 @@ def api_get(self, uri: str, args, methods="GET"): args.from_page += 1 return result + + """ + @todo Add pretty option on api_get_single + """ def api_get_single(self, uri: str, methods="GET"): """ This function will handle all the API requests that send a single json response back. From 5cf350fe6d58b8888a36593b0cb9fd10ac577cdf Mon Sep 17 00:00:00 2001 From: "Jules Lasne (jlasne)" Date: Sun, 21 Jan 2018 15:38:01 +0100 Subject: [PATCH 7/7] [STAGE] Staged tests --- tests/test_IntraPy.py | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/tests/test_IntraPy.py b/tests/test_IntraPy.py index 19b2f50..0cc8b77 100644 --- a/tests/test_IntraPy.py +++ b/tests/test_IntraPy.py @@ -15,9 +15,10 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . """ -from nose.tools import assert_is_not_none +from nose.tools import assert_is_not_none, assert_equal from unittest.mock import Mock, patch from IntraPy.IntraPy import IntraPy +import json @patch('IntraPy.IntraPy.requests.request') @@ -33,10 +34,42 @@ def test_api_with_good_url(mock_get): @patch('IntraPy.IntraPy.requests.request') def test_api_with_bad_url(mock_get): - # Configure the mock to return a response with an OK status code. mock_get.return_value.ok = False - # Call the service, which will send a request to the server. api = IntraPy() response = api.api_get_single("/v2/thisdoesntexist") - # If the request is sent successfully, then I expect a response to be returned. - assert_is_not_none(response) \ No newline at end of file + assert_is_not_none(response) + +""" +def test_get_uid_from_token(): + # We are patching the specified function + mock_get_patcher = patch('IntraPy.IntraPy.requests.request') + # What we want the json to be + info = [{ + "resource_owner_id": "Null", + "scopes":["public"], + "expires_in_seconds": 0000, + "application": + { + "uid":"8ngp6jutwr7ge667eqsnqqacppk7wkv2v6hk7rdtvbtzfu82krdeakg3x4rvcra3" + }, + "created_at": 0000000 + }] + # Call an instance of the desired class before the mock (Necessary here) + api = IntraPy() + # Start patching 'requests.request'. + mock_get = mock_get_patcher.start() + + # Configure the mock to return a response with status code 200 and the info + mock_get.return_value = Mock(status_code=200) + mock_get.return_value.content = json.dumps(info) + + # Call the service, which will send a request to the server. + response = api.get_uid_from_token() + + # Stop patching 'requests'. + mock_get_patcher.stop() + + # Assert that the request-response cycle completed successfully. + assert_equal(response.status_code, 200) + assert_equal(response.json(), info) +""" \ No newline at end of file