#1 add user url and view tests

This commit is contained in:
moritz münch 2021-07-12 23:28:29 +02:00
parent 25f0184d76
commit 624505d110
2 changed files with 133 additions and 18 deletions

View File

@ -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 <http://www.gnu.org/licenses/>.
# 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)

View File

@ -224,27 +224,24 @@ def show_coin(coin, value):
return { 'value': value } return { 'value': value }
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): def add_user(request, username, color):
''' add a user ''' ''' add a user '''
if username and color:
try: try:
existing_user = User.objects.get(name=username) existing_user = User.objects.get(name=username)
return response(1, f"'{username}' ist schon vergeben.") return response(1, f"'{username}' ist schon vergeben.")
except User.DoesNotExist: except User.DoesNotExist:
User.objects.create(name=username, color=color) User.objects.create(name=username, color=color)
else:
return response(1, 'Du musst einen Namen und Farbe angeben.')
return response() 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_coin(request): def add_coin(request):
''' add or update a coin ''' add or update a coin
@params request.POST[field] @params request.POST[field]