From 624505d110879d6e9e01fa8b5a6a5107fa09aa77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?moritz=20m=C3=BCnch?= Date: Mon, 12 Jul 2021 23:28:29 +0200 Subject: [PATCH] #1 add user url and view tests --- coinmanager/coinc/tests.py | 122 ++++++++++++++++++++++++++++++++++++- coinmanager/coinc/views.py | 29 ++++----- 2 files changed, 133 insertions(+), 18 deletions(-) diff --git a/coinmanager/coinc/tests.py b/coinmanager/coinc/tests.py index 7ce503c..21b658c 100644 --- a/coinmanager/coinc/tests.py +++ b/coinmanager/coinc/tests.py @@ -1,3 +1,121 @@ -from django.test import TestCase +# encoding: utf-8 +# +# Copyright (C) 2021 willipink.eu +# Author Moritz Münch moritzmuench@mailbox.org +# +# 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 . -# Create your tests here. + +from json import loads + +from django.http import HttpResponse +from django.test import TestCase, Client + +from coinc.models import User +from coinc.views import response, add_user + +CLIENT = Client() +SESSION = CLIENT.session +# session['backend'] = 'facebook' +# session['kwargs'] = {'username':'Chuck Norris','response':{'id':1}} +SESSION.save() + +class TestUser(TestCase): + + ''' all test for the user model ''' + def setUp(self): + self.test_username = 'test_username' + self.test_color = 'test_color' + self.test_request = 'test_request' + + + def test_urls_add_without_username(self): + ''' url: test adding a user without username ''' + test_response = self.client.get( + f'/coinc/add/user//color/{self.test_color}') + self.assertEqual(test_response.status_code, 404) + + + test_response = self.client.get( + f'/coinc/add/user/color/{self.test_color}') + self.assertEqual(test_response.status_code, 404) + + + test_response = self.client.get( + f'/coinc/add/color/{self.test_color}') + self.assertEqual(test_response.status_code, 404) + + + def test_urls_add_without_color(self): + ''' url: test adding a user without color ''' + test_response = self.client.get( + f'/coinc/add/user/{self.test_username}/color') + self.assertEqual(test_response.status_code, 404) + + test_response = self.client.get( + f'/coinc/add/user/{self.test_username}') + self.assertEqual(test_response.status_code, 404) + + + def test_urls_add_without_username_and_color(self): + ''' url: test adding a user without username and color ''' + test_response = self.client.get( + f'/coinc/add/user//color') + self.assertEqual(test_response.status_code, 404) + + + test_response = self.client.get( + f'/coinc/add/user/color') + self.assertEqual(test_response.status_code, 404) + + + test_response = self.client.get( + f'/coinc/add/user') + self.assertEqual(test_response.status_code, 404) + + + def test_urls_add_not_existing_user(self): + ''' url: test adding a user which does not yet exist ''' + + test_response = self.client.get( + f'/coinc/add/user/{self.test_username}/color/{self.test_color}') + self.assertEqual(test_response.status_code, 200) + + + def test_view_add_not_existing_user(self): + ''' view: test adding a user which does not yet exist ''' + + test_response = add_user(self.test_request, self.test_username, + self.test_color) + test_status = loads(test_response.content) + test_status = test_status['status'] + self.assertEqual(test_status, 0) + + + def test_add_existing_user(self): + ''' view: test adding a user twice ''' + + # first user + test_response = self.client.get( + f'/coinc/add/user/{self.test_username}/color/{self.test_color}') + test_status = loads(test_response.content) + test_status = test_status['status'] + self.assertEqual(test_status, 0) + + # second user + test_response = self.client.get( + f'/coinc/add/user/{self.test_username}/color/{self.test_color}') + test_status = loads(test_response.content) + test_status = test_status['status'] + self.assertEqual(test_status, 1) diff --git a/coinmanager/coinc/views.py b/coinmanager/coinc/views.py index 35eff64..e63b40c 100644 --- a/coinmanager/coinc/views.py +++ b/coinmanager/coinc/views.py @@ -224,27 +224,24 @@ def show_coin(coin, value): return { 'value': value } -def add_user(request, username, color): - ''' add a user ''' - - if username and color: - try: - existing_user = User.objects.get(name=username) - return response(1, f"'{username}' ist schon vergeben.") - - except User.DoesNotExist: - User.objects.create(name=username, color=color) - else: - return response(1, 'Du musst einen Namen und Farbe angeben.') - - return response() - - def response(status=0, message=''): ''' return generic json-formatted status output with http-headers ''' return HttpResponse(dumps({ 'status': status, 'message': message })) +def add_user(request, username, color): + ''' add a user ''' + + try: + existing_user = User.objects.get(name=username) + return response(1, f"'{username}' ist schon vergeben.") + + except User.DoesNotExist: + User.objects.create(name=username, color=color) + + return response() + + def add_coin(request): ''' add or update a coin @params request.POST[field]