diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml new file mode 100644 index 0000000..2fa4b16 --- /dev/null +++ b/.github/workflows/python-app.yml @@ -0,0 +1,33 @@ +name: Build and Test + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Set up Python 3.8 + uses: actions/setup-python@v2 + with: + python-version: 3.8 + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install flake8 pytest + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: Lint with flake8 + run: | + # stop the build if there are Python syntax errors or undefined names + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide + flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + - name: Test with pytest + run: | + pytest test.py diff --git a/requirements.txt b/requirements.txt index 90b4b1c..33be3dc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,17 +1,26 @@ astroid==2.4.2 +atomicwrites==1.4.0 +attrs==19.3.0 autopep8==1.5.4 click==7.1.2 colorama==0.4.3 Flask==1.1.2 gunicorn==20.0.4 +iniconfig==1.0.1 isort==4.3.21 itsdangerous==1.1.0 Jinja2==2.11.2 lazy-object-proxy==1.4.3 MarkupSafe==1.1.1 mccabe==0.6.1 +more-itertools==8.4.0 +packaging==20.4 +pluggy==0.13.1 +py==1.9.0 pycodestyle==2.6.0 pylint==2.5.3 +pyparsing==2.4.7 +pytest==6.0.1 six==1.15.0 toml==0.10.1 Werkzeug==1.0.1 diff --git a/test.py b/test.py new file mode 100644 index 0000000..7f230c1 --- /dev/null +++ b/test.py @@ -0,0 +1,91 @@ +from app import app +import unittest +import json + + +class MorseCodeTest(unittest.TestCase): + + # This method test for the index page that the response status code is 200. + def test_index_statuscode(self): + tester = app.test_client(self) + response = tester.get('/') + self.assertEqual(response.status_code, 200, + 'Index page did not returned 200 status code.') + + # Test the content of the index page. + def test_index_content(self): + tester = app.test_client(self) + response = tester.get('/') + self.assertEqual(response.content_type, 'text/html; charset=utf-8', + 'Response content is not of html type.') + + self.assertTrue(b'The Morse Code' in response.data, + '"The Morse Code" was not found in the returned resposne.') + + # Test the encrypt post api for positive scenario. + def test_encrypt_success(self): + tester = app.test_client(self) + request_data = {'message': 'Hi, Welcome.'} + response = tester.post('/encrypt/', data=request_data, + content_type='application/x-www-form-urlencoded') + + self.assertEqual(response.status_code, 200, + 'Encryt request did not returned 200 status code.') + + self.assertEqual(response.content_type, 'application/json', + 'Response content is not of json type.') + + self.assertEqual(response.json['status'], 'success') + self.assertEqual( + response.json['cipher'], '.... .. --..-- / .-- . .-.. -.-. --- -- . .-.-.-') + + # Test the encrypt post api for negative scenario. + def test_encrypt_error(self): + tester = app.test_client(self) + request_data = {} + response = tester.post('/encrypt/', data=request_data, + content_type='application/x-www-form-urlencoded') + + self.assertEqual(response.status_code, 400, + 'Encryt request did not returned 400 status code.') + + self.assertEqual(response.content_type, 'application/json', + 'Response content is not of json type.') + + self.assertEqual(response.json['status'], 'error') + + # Test the decrypt post api for positive scenario. + def test_decrypt_success(self): + tester = app.test_client(self) + request_data = {'cipher': '.... .. --..-- / .-- . .-.. -.-. --- -- . .-.-.-'} + response = tester.post('/decrypt/', data=request_data, + content_type='application/x-www-form-urlencoded') + + self.assertEqual(response.status_code, 200, + 'Decryt request did not returned 200 status code.') + + self.assertEqual(response.content_type, 'application/json', + 'Response content is not of json type.') + + self.assertEqual(response.json['status'], 'success') + self.assertEqual( + response.json['message'], 'Hi, welcome.') + + # Test the decrypt post api for negative scenario. + def test_decrypt_error(self): + tester = app.test_client(self) + request_data = {} + response = tester.post('/decrypt/', data=request_data, + content_type='application/x-www-form-urlencoded') + + self.assertEqual(response.status_code, 400, + 'Decryt request did not returned 400 status code.') + + self.assertEqual(response.content_type, 'application/json', + 'Response content is not of json type.') + + self.assertEqual(response.json['status'], 'error') + + +if __name__ == '__main__': + unittest.main()