From a14228c90273bd83f1fac20cbb7659627326b393 Mon Sep 17 00:00:00 2001 From: Guillaume Vincent Date: Wed, 20 Jun 2018 09:37:55 +0200 Subject: [PATCH] Fix flake8 and bandit tests * enable voting job on pep8 tox env * fix pep8 tox job * add db.sqlite3 in ignore file Change-Id: If11fb74553a6f1cb986cbed17e0fc8386d08c032 --- .gitignore | 2 +- .zuul.yaml | 3 +-- ara/api/tests/factories.py | 2 +- ara/api/tests/tests_file.py | 24 ++++++++++++------------ ara/api/tests/tests_file_content.py | 1 - ara/api/tests/tests_playbook.py | 6 +++--- ara/api/tests/tests_playbook_file.py | 10 +++++----- ara/api/views.py | 2 +- ara/server/settings.py | 4 ++-- tox.ini | 2 +- 10 files changed, 27 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 48d190e..0cd1e62 100644 --- a/.gitignore +++ b/.gitignore @@ -91,6 +91,6 @@ ENV/ # Rope project settings .ropeproject -.*/db.sqlite3 +db.sqlite3 www/ data/ diff --git a/.zuul.yaml b/.zuul.yaml index b74ec5b..8e9329e 100644 --- a/.zuul.yaml +++ b/.zuul.yaml @@ -2,8 +2,7 @@ check: jobs: - tox-py35 - - tox-pep8: - voting: false + - tox-pep8 gate: jobs: - tox-py35 diff --git a/ara/api/tests/factories.py b/ara/api/tests/factories.py index 3ca2dc9..ecb7fbb 100644 --- a/ara/api/tests/factories.py +++ b/ara/api/tests/factories.py @@ -16,7 +16,7 @@ class FileFactory(factory.DjangoModelFactory): class Meta: model = models.File - path = '/tmp/playbook.yml' + path = '/path/playbook.yml' content = factory.SubFactory(FileContentFactory) diff --git a/ara/api/tests/tests_file.py b/ara/api/tests/tests_file.py index c8fc737..d056739 100644 --- a/ara/api/tests/tests_file.py +++ b/ara/api/tests/tests_file.py @@ -7,13 +7,13 @@ from ara.api.tests import factories class FileTestCase(APITestCase): def test_file_factory(self): file_content = factories.FileContentFactory() - file = factories.FileFactory(path='/tmp/playbook.yml', content=file_content) - self.assertEqual(file.path, '/tmp/playbook.yml') + file = factories.FileFactory(path='/path/playbook.yml', content=file_content) + self.assertEqual(file.path, '/path/playbook.yml') self.assertEqual(file.content.sha1, file_content.sha1) def test_file_serializer(self): serializer = serializers.FileSerializer(data={ - 'path': '/tmp/playbook.yml', + 'path': '/path/playbook.yml', 'content': '# playbook' }) serializer.is_valid() @@ -25,7 +25,7 @@ class FileTestCase(APITestCase): content = '# playbook' serializer = serializers.FileSerializer(data={ - 'path': '/tmp/1/playbook.yml', + 'path': '/path/1/playbook.yml', 'content': content }) serializer.is_valid() @@ -33,7 +33,7 @@ class FileTestCase(APITestCase): file_content.refresh_from_db() serializer2 = serializers.FileSerializer(data={ - 'path': '/tmp/2/playbook.yml', + 'path': '/path/2/playbook.yml', 'content': content }) serializer2.is_valid() @@ -46,7 +46,7 @@ class FileTestCase(APITestCase): def test_create_file(self): self.assertEqual(0, models.File.objects.count()) request = self.client.post('/api/v1/files/', { - 'path': '/tmp/playbook.yml', + 'path': '/path/playbook.yml', 'content': '# playbook' }) self.assertEqual(201, request.status_code) @@ -69,24 +69,24 @@ class FileTestCase(APITestCase): def test_update_file(self): file = factories.FileFactory() - self.assertNotEqual('/tmp/new_playbook.yml', file.path) + self.assertNotEqual('/path/new_playbook.yml', file.path) request = self.client.put('/api/v1/files/%s/' % file.id, { - "path": "/tmp/new_playbook.yml", + "path": "/path/new_playbook.yml", 'content': '# playbook' }) self.assertEqual(200, request.status_code) file_updated = models.File.objects.get(id=file.id) - self.assertEqual('/tmp/new_playbook.yml', file_updated.path) + self.assertEqual('/path/new_playbook.yml', file_updated.path) def test_partial_update_file(self): file = factories.FileFactory() - self.assertNotEqual('/tmp/new_playbook.yml', file.path) + self.assertNotEqual('/path/new_playbook.yml', file.path) request = self.client.patch('/api/v1/files/%s/' % file.id, { - "path": "/tmp/new_playbook.yml", + "path": "/path/new_playbook.yml", }) self.assertEqual(200, request.status_code) file_updated = models.File.objects.get(id=file.id) - self.assertEqual('/tmp/new_playbook.yml', file_updated.path) + self.assertEqual('/path/new_playbook.yml', file_updated.path) def test_delete_file(self): file = factories.FileFactory() diff --git a/ara/api/tests/tests_file_content.py b/ara/api/tests/tests_file_content.py index 5e3b202..b4f6d3e 100644 --- a/ara/api/tests/tests_file_content.py +++ b/ara/api/tests/tests_file_content.py @@ -1,6 +1,5 @@ from rest_framework.test import APITestCase -from ara.api import serializers, models from ara.api.tests import factories diff --git a/ara/api/tests/tests_playbook.py b/ara/api/tests/tests_playbook.py index fea770f..c6ee428 100644 --- a/ara/api/tests/tests_playbook.py +++ b/ara/api/tests/tests_playbook.py @@ -15,7 +15,7 @@ class PlaybookTestCase(APITestCase): serializer = serializers.PlaybookSerializer(data={ 'ansible_version': '2.4.0', 'file': { - 'path': '/tmp/playbook.yml', + 'path': '/path/playbook.yml', 'content': '# playbook' } }) @@ -28,7 +28,7 @@ class PlaybookTestCase(APITestCase): serializer = serializers.PlaybookSerializer(data={ 'ansible_version': '2.4.0', 'file': { - 'path': '/tmp/playbook.yml', + 'path': '/path/playbook.yml', 'content': '# playbook' }, 'parameters': {'foo': 'bar'} @@ -65,7 +65,7 @@ class PlaybookTestCase(APITestCase): request = self.client.post('/api/v1/playbooks/', { "ansible_version": "2.4.0", 'file': { - 'path': '/tmp/playbook.yml', + 'path': '/path/playbook.yml', 'content': '# playbook' } }) diff --git a/ara/api/tests/tests_playbook_file.py b/ara/api/tests/tests_playbook_file.py index bee5974..ee95fcf 100644 --- a/ara/api/tests/tests_playbook_file.py +++ b/ara/api/tests/tests_playbook_file.py @@ -12,11 +12,11 @@ class PlaybookFileTestCase(APITestCase): self.client.post('/api/v1/playbooks/', { 'ansible_version': '2.4.0', 'file': { - 'path': '/tmp/playbook.yml', + 'path': '/path/playbook.yml', 'content': '# playbook' }, 'files': [{ - 'path': '/tmp/host', + 'path': '/path/host', 'content': '# host' }], }) @@ -27,7 +27,7 @@ class PlaybookFileTestCase(APITestCase): playbook = factories.PlaybookFactory() self.assertEqual(1, models.File.objects.all().count()) self.client.post('/api/v1/playbooks/%s/files/' % playbook.id, { - 'path': '/tmp/playbook.yml', + 'path': '/path/playbook.yml', 'content': '# playbook' }) self.assertEqual(2, models.File.objects.all().count()) @@ -39,11 +39,11 @@ class PlaybookFileTestCase(APITestCase): number_file_contents = models.FileContent.objects.all().count() content = '# playbook %s' % time.time() self.client.post('/api/v1/playbooks/%s/files/' % playbook.id, { - 'path': '/tmp/1/playbook.yml', + 'path': '/path/1/playbook.yml', 'content': content }) self.client.post('/api/v1/playbooks/%s/files/' % playbook.id, { - 'path': '/tmp/2/playbook.yml', + 'path': '/path/2/playbook.yml', 'content': content }) self.assertEqual(number_playbooks + 2, models.File.objects.all().count()) diff --git a/ara/api/views.py b/ara/api/views.py index 9729872..4540fed 100644 --- a/ara/api/views.py +++ b/ara/api/views.py @@ -14,7 +14,7 @@ # # You should have received a copy of the GNU General Public License # along with ARA. If not, see . -from rest_framework.decorators import api_view, detail_route +from rest_framework.decorators import api_view from rest_framework.response import Response from rest_framework.reverse import reverse diff --git a/ara/server/settings.py b/ara/server/settings.py index 71a7563..15c4e18 100644 --- a/ara/server/settings.py +++ b/ara/server/settings.py @@ -1,15 +1,15 @@ import logging import os -import random import sys from envparse import env +from django.utils.crypto import get_random_string BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) def get_secret_key(secret_key): if not secret_key: - return "".join([random.choice("abcdefghijklmnopqrstuvwxyz0123456789!@#$^&*(-_=+)") for i in range(50)]) + return get_random_string(length=50) return secret_key diff --git a/tox.ini b/tox.ini index d40d0f3..cfe8247 100644 --- a/tox.ini +++ b/tox.ini @@ -57,4 +57,4 @@ max-line-length = 120 ignore = E123,E125,E741 enable-extensions=H106,H203 show-source = True -exclude=.venv,.git,.tox,dist,doc,*lib/python*,*egg,build +exclude=.venv,.git,.tox,dist,doc,*lib/python*,*egg,build,ara/api/migrations