diff --git a/docker/.bash_history b/docker/.bash_history new file mode 100644 index 0000000..f21c4e7 --- /dev/null +++ b/docker/.bash_history @@ -0,0 +1,27 @@ +ls +cd toh +python manage.py migrate +python manage.py createsuperuser +python manage.py runserver +python manage.py runserver 0.0.0.0:80000 +python manage.py runserver 0.0.0.0:8000 +clear +python manage.py test +pip install coverage +clear +coverage run --source='.' manage.py test hero +coverage report +coverage run --source='.' manage.py test hero +coverage report +coverage run --source='.' manage.py test hero +coverage report +coverage run --source='.' manage.py test hero +coverage report +coverage run --source='.' manage.py test hero +coverage report +python manage.py runserver 0.0.0.0:8000 +python manage.py runserver 0.0.0.0:8000 +python manage.py makemigrations +python manage.py runserver 0.0.0.0:8000 +python manage.py runserver 0.0.0.0:8000 +clear diff --git a/toh/db.sqlite3 b/toh/db.sqlite3 index 69d2e6a..3f3a526 100644 Binary files a/toh/db.sqlite3 and b/toh/db.sqlite3 differ diff --git a/toh/hero/__pycache__/__init__.cpython-39.pyc b/toh/hero/__pycache__/__init__.cpython-39.pyc index 65eda82..9122eb0 100644 Binary files a/toh/hero/__pycache__/__init__.cpython-39.pyc and b/toh/hero/__pycache__/__init__.cpython-39.pyc differ diff --git a/toh/hero/__pycache__/admin.cpython-39.pyc b/toh/hero/__pycache__/admin.cpython-39.pyc index 21290fd..4993ffa 100644 Binary files a/toh/hero/__pycache__/admin.cpython-39.pyc and b/toh/hero/__pycache__/admin.cpython-39.pyc differ diff --git a/toh/hero/__pycache__/apps.cpython-39.pyc b/toh/hero/__pycache__/apps.cpython-39.pyc index f74e04d..4cd69e1 100644 Binary files a/toh/hero/__pycache__/apps.cpython-39.pyc and b/toh/hero/__pycache__/apps.cpython-39.pyc differ diff --git a/toh/hero/__pycache__/models.cpython-39.pyc b/toh/hero/__pycache__/models.cpython-39.pyc index f16f174..f13c0bd 100644 Binary files a/toh/hero/__pycache__/models.cpython-39.pyc and b/toh/hero/__pycache__/models.cpython-39.pyc differ diff --git a/toh/hero/__pycache__/urls.cpython-39.pyc b/toh/hero/__pycache__/urls.cpython-39.pyc index 8958b25..78e13f4 100644 Binary files a/toh/hero/__pycache__/urls.cpython-39.pyc and b/toh/hero/__pycache__/urls.cpython-39.pyc differ diff --git a/toh/hero/__pycache__/views.cpython-39.pyc b/toh/hero/__pycache__/views.cpython-39.pyc index b0c2dbf..7d5c76b 100644 Binary files a/toh/hero/__pycache__/views.cpython-39.pyc and b/toh/hero/__pycache__/views.cpython-39.pyc differ diff --git a/toh/hero/admin.py b/toh/hero/admin.py index 8c38f3f..5096d20 100644 --- a/toh/hero/admin.py +++ b/toh/hero/admin.py @@ -1,3 +1,5 @@ from django.contrib import admin - +from .models import Hero # Register your models here. + +admin.site.register(Hero) diff --git a/toh/hero/migrations/__pycache__/0001_initial.cpython-39.pyc b/toh/hero/migrations/__pycache__/0001_initial.cpython-39.pyc index 9dd1f64..407744b 100644 Binary files a/toh/hero/migrations/__pycache__/0001_initial.cpython-39.pyc and b/toh/hero/migrations/__pycache__/0001_initial.cpython-39.pyc differ diff --git a/toh/hero/migrations/__pycache__/0002_hero_age.cpython-39.pyc b/toh/hero/migrations/__pycache__/0002_hero_age.cpython-39.pyc index 3594f17..8c7fc72 100644 Binary files a/toh/hero/migrations/__pycache__/0002_hero_age.cpython-39.pyc and b/toh/hero/migrations/__pycache__/0002_hero_age.cpython-39.pyc differ diff --git a/toh/hero/migrations/__pycache__/0003_team.cpython-39.pyc b/toh/hero/migrations/__pycache__/0003_team.cpython-39.pyc index 4498fdd..ee4a991 100644 Binary files a/toh/hero/migrations/__pycache__/0003_team.cpython-39.pyc and b/toh/hero/migrations/__pycache__/0003_team.cpython-39.pyc differ diff --git a/toh/hero/migrations/__pycache__/__init__.cpython-39.pyc b/toh/hero/migrations/__pycache__/__init__.cpython-39.pyc index 5c2c49a..efbf2ab 100644 Binary files a/toh/hero/migrations/__pycache__/__init__.cpython-39.pyc and b/toh/hero/migrations/__pycache__/__init__.cpython-39.pyc differ diff --git a/toh/hero/tests.py b/toh/hero/tests.py index 7ce503c..56d3d1b 100644 --- a/toh/hero/tests.py +++ b/toh/hero/tests.py @@ -1,3 +1,30 @@ -from django.test import TestCase - +from http import client +from urllib import response +from django.test import TestCase, Client +from .models import Hero # Create your tests here. + + +class HeroTestCase(TestCase): + def setUp(self) -> None: + Hero.objects.create(name='ironman1') + Hero.objects.create(name='ironman2') + Hero.objects.create(name='ironman3') + + def test_hero_count(self): + self.assertEqual(Hero.objects.all().count(), 3) + + def test_hero_id(self): + client = Client() + response = client.get('/hero/1/') + + self.assertEqual(response.status_code, 200) + self.assertIn('1', response.content.decode()) + + def test_response_id(self): + client = Client() + resp1 = client.get('/hero/1/') + resp2 = client.get('/hero/2/') + + self.assertIn('1', resp1.content.decode()) + self.assertIn('2', resp2.content.decode()) diff --git a/toh/hero/urls.py b/toh/hero/urls.py index 7384e1e..c938c0e 100644 --- a/toh/hero/urls.py +++ b/toh/hero/urls.py @@ -2,8 +2,9 @@ from . import views urlpatterns = [ + path('token/', views.token, name='token'), path('', views.hero_list), path('<int:id>/', views.id, name='hero_id'), path('<str:name>/', views.name, name='hero_name'), path('info/<int:id>/', views.hero_info, name='hero_info'), -] \ No newline at end of file +] diff --git a/toh/hero/views.py b/toh/hero/views.py index d86c570..fcf2611 100644 --- a/toh/hero/views.py +++ b/toh/hero/views.py @@ -3,17 +3,20 @@ import json from json.decoder import JSONDecodeError from .models import Hero +from django.views.decorators.csrf import ensure_csrf_cookie # def index(request): # return HttpResponse('Hello, world!') + def id(request, id): return HttpResponse(f'Your id is {id}!') + def name(request, name): return HttpResponse(f'Your name is {name}!') -@csrf_exempt + def hero_list(request): if request.method == 'GET': hero_all_list = [hero for hero in Hero.objects.all().values()] @@ -31,6 +34,7 @@ def hero_list(request): else: return HttpResponseNotAllowed(['GET', 'POST']) + @csrf_exempt def hero_info(request, id): if request.method == 'GET': @@ -50,4 +54,12 @@ def hero_info(request, id): response_dict = {"id": hero.id, "name": hero.name, "age": hero.age} return JsonResponse(response_dict, status=200) else: - return HttpResponseNotAllowed(['GET', 'PUT']) \ No newline at end of file + return HttpResponseNotAllowed(['GET', 'PUT']) + + +@ensure_csrf_cookie +def token(request): + if request.method == 'GET': + return HttpResponse(status=204) + else: + return HttpResponseNotAllowed(['GET']) diff --git a/toh/toh/__pycache__/__init__.cpython-39.pyc b/toh/toh/__pycache__/__init__.cpython-39.pyc index 1f06e94..5cbce36 100644 Binary files a/toh/toh/__pycache__/__init__.cpython-39.pyc and b/toh/toh/__pycache__/__init__.cpython-39.pyc differ diff --git a/toh/toh/__pycache__/settings.cpython-39.pyc b/toh/toh/__pycache__/settings.cpython-39.pyc index 28ee7a6..0278fce 100644 Binary files a/toh/toh/__pycache__/settings.cpython-39.pyc and b/toh/toh/__pycache__/settings.cpython-39.pyc differ diff --git a/toh/toh/__pycache__/urls.cpython-39.pyc b/toh/toh/__pycache__/urls.cpython-39.pyc index ec7f97b..2f2bda7 100644 Binary files a/toh/toh/__pycache__/urls.cpython-39.pyc and b/toh/toh/__pycache__/urls.cpython-39.pyc differ diff --git a/toh/toh/__pycache__/wsgi.cpython-39.pyc b/toh/toh/__pycache__/wsgi.cpython-39.pyc index c626f89..b0a6b7a 100644 Binary files a/toh/toh/__pycache__/wsgi.cpython-39.pyc and b/toh/toh/__pycache__/wsgi.cpython-39.pyc differ