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..3dc6062 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,17 @@ +sudo: false + +language: python + +python: + - 3.6.3 + +install: + - pip install -r requirements.txt + - pip install codecov + - bash scripts/create_settings.sh + +script: + - nosetests --with-coverage --verbosity=2 + +after_success: +- codecov \ No newline at end of file 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. diff --git a/scripts/create_settings.sh b/scripts/create_settings.sh new file mode 100644 index 0000000..52aef69 --- /dev/null +++ b/scripts/create_settings.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +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 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 new file mode 100644 index 0000000..0cc8b77 --- /dev/null +++ b/tests/test_IntraPy.py @@ -0,0 +1,75 @@ +""" + 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, assert_equal +from unittest.mock import Mock, patch +from IntraPy.IntraPy import IntraPy +import json + + +@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_with_bad_url(mock_get): + mock_get.return_value.ok = False + api = IntraPy() + response = api.api_get_single("/v2/thisdoesntexist") + 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