cleaning up code and writing tests
parent
e11f670408
commit
5376709c7f
@ -0,0 +1,11 @@
|
||||
[run]
|
||||
source = .
|
||||
branch = True
|
||||
omit =
|
||||
.venv/*
|
||||
manage.py
|
||||
|
||||
[report]
|
||||
fail_under = 100
|
||||
show_missing = True
|
||||
skip_covered = True
|
@ -1,111 +0,0 @@
|
||||
from secrets import token_urlsafe
|
||||
from django.test.utils import setup_test_environment
|
||||
from django.test import TestCase, Client
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.paginator import Page
|
||||
from django.urls import reverse
|
||||
from items.models import Item, ItemImage
|
||||
|
||||
|
||||
class ItemsTestCase(TestCase):
|
||||
def setUp(self):
|
||||
self.test_user_username = 'tester'
|
||||
self.test_user_password = token_urlsafe(32)
|
||||
self.test_user = User.objects.create_user(
|
||||
self.test_user_username,
|
||||
password=self.test_user_password
|
||||
)
|
||||
self.test_item = Item.objects.create(
|
||||
owner=self.test_user,
|
||||
name='Test Item',
|
||||
description='Test item',
|
||||
ask_price_xmr=0.3
|
||||
)
|
||||
|
||||
def login(self):
|
||||
self.client.login(
|
||||
username=self.test_user_username,
|
||||
password=self.test_user_password
|
||||
)
|
||||
|
||||
def logout(self):
|
||||
self.client.logout()
|
||||
|
||||
def test_list_items_should_allow_anonymous(self):
|
||||
response = self.client.get(reverse('list_items'))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_get_item_should_allow_anonymous(self):
|
||||
response = self.client.get(reverse('get_item', args=[self.test_item.id]))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_list_items_returns_page(self):
|
||||
response = self.client.get(reverse('list_items'))
|
||||
items = response.context['items']
|
||||
self.assertTrue(isinstance(items, Page))
|
||||
|
||||
def test_create_item_should_require_auth(self):
|
||||
no_auth_response = self.client.get(reverse('create_item'))
|
||||
self.login()
|
||||
auth_response = self.client.get(reverse('create_item'))
|
||||
self.logout()
|
||||
self.assertEqual(no_auth_response.status_code, 302)
|
||||
self.assertTrue(no_auth_response.url.startswith('/accounts/login'))
|
||||
self.assertEqual(auth_response.status_code, 200)
|
||||
|
||||
def test_edit_item_should_require_auth(self):
|
||||
no_auth_response = self.client.get(reverse('edit_item', args=[self.test_item.id]))
|
||||
self.login()
|
||||
auth_response = self.client.get(reverse('edit_item', args=[self.test_item.id]))
|
||||
self.logout()
|
||||
self.assertEqual(no_auth_response.status_code, 302)
|
||||
self.assertTrue(no_auth_response.url.startswith('/accounts/login'))
|
||||
self.assertEqual(auth_response.status_code, 200)
|
||||
|
||||
def test_edit_item_should_require_active_user_is_owner(self):
|
||||
new_user = User.objects.create_user(
|
||||
'tester2',
|
||||
password=token_urlsafe(24)
|
||||
)
|
||||
new_item = Item.objects.create(
|
||||
owner=new_user,
|
||||
name='Test Item 2',
|
||||
description='Test item 2',
|
||||
ask_price_xmr=0.3
|
||||
)
|
||||
self.login()
|
||||
test_item_edit_response = self.client.get(reverse('edit_item', args=[self.test_item.id]))
|
||||
new_item_edit_response = self.client.get(reverse('edit_item', args=[new_item.id]))
|
||||
self.logout()
|
||||
self.assertEqual(test_item_edit_response.status_code, 200)
|
||||
self.assertEqual(new_item_edit_response.status_code, 302)
|
||||
new_item.delete()
|
||||
new_user.delete()
|
||||
|
||||
def test_delete_item_should_require_auth(self):
|
||||
no_auth_response = self.client.get(reverse('delete_item', args=[self.test_item.id]))
|
||||
self.login()
|
||||
auth_response = self.client.get(reverse('delete_item', args=[self.test_item.id]))
|
||||
self.logout()
|
||||
self.assertEqual(no_auth_response.status_code, 302)
|
||||
self.assertTrue(no_auth_response.url.startswith('/accounts/login'))
|
||||
|
||||
def test_delete_item_should_require_active_user_is_owner(self):
|
||||
new_user = User.objects.create_user(
|
||||
'tester3',
|
||||
password=token_urlsafe(24)
|
||||
)
|
||||
new_item = Item.objects.create(
|
||||
owner=new_user,
|
||||
name='Test Item 3',
|
||||
description='Test item 3',
|
||||
ask_price_xmr=0.3
|
||||
)
|
||||
self.login()
|
||||
test_delete_item_response = self.client.get(reverse('delete_item', args=[self.test_item.id]))
|
||||
new_delete_item_response = self.client.get(reverse('delete_item', args=[new_item.id]))
|
||||
self.logout()
|
||||
self.assertEqual(test_delete_item_response.status_code, 302)
|
||||
self.assertEqual(new_delete_item_response.status_code, 302)
|
||||
new_item.delete()
|
||||
new_user.delete()
|
@ -0,0 +1,27 @@
|
||||
from django.test import TestCase
|
||||
from items.forms import CreateItemForm, SearchItemForm
|
||||
from core.monero import AuctionWallet
|
||||
|
||||
|
||||
class ItemFormsTestCase(TestCase):
|
||||
def setUp(self):
|
||||
self.aw = AuctionWallet()
|
||||
|
||||
def test_create_item_form_is_valid(self):
|
||||
data = {
|
||||
'name': 'Expected item name',
|
||||
'description': 'expected description',
|
||||
'whereabouts': 'anywhere in the world',
|
||||
'ask_price_xmr': .1,
|
||||
'payout_address': self.aw.wallet.accounts[0].address(),
|
||||
}
|
||||
form = CreateItemForm(data=data)
|
||||
self.assertTrue(form.is_valid())
|
||||
|
||||
def test_search_item_form_is_valid(self):
|
||||
data = {
|
||||
'search': ''
|
||||
}
|
||||
form = SearchItemForm(data=data)
|
||||
self.assertTrue(form.is_valid())
|
||||
|
@ -0,0 +1,43 @@
|
||||
from secrets import token_urlsafe
|
||||
from django.test import TestCase
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.staticfiles import finders
|
||||
from django.core.files import File
|
||||
from items.models import Item, ItemImage
|
||||
|
||||
|
||||
class ItemModelsTestCase(TestCase):
|
||||
def setUp(self):
|
||||
self.test_user_username = 'tester'
|
||||
self.test_user_password = token_urlsafe(32)
|
||||
self.test_user = User.objects.create_user(
|
||||
self.test_user_username,
|
||||
password=self.test_user_password
|
||||
)
|
||||
self.test_item = Item.objects.create(
|
||||
owner=self.test_user,
|
||||
name='Test Item',
|
||||
description='Test item',
|
||||
ask_price_xmr=0.3
|
||||
)
|
||||
|
||||
|
||||
def test_create_item(self):
|
||||
test_item = Item.objects.create(
|
||||
owner=self.test_user,
|
||||
name='Test Item',
|
||||
description='Test item',
|
||||
ask_price_xmr=0.3
|
||||
)
|
||||
obj_name = f'{test_item.id} - {test_item.owner} - {test_item.name}'
|
||||
self.assertTrue(isinstance(test_item, Item))
|
||||
self.assertEqual(test_item.__str__(), obj_name)
|
||||
|
||||
# def test_create_item_image(self):
|
||||
# static_img = finders.find('images/monero-symbol-800.png')
|
||||
# img = File(open(static_img, 'rb'))
|
||||
# test_item_image = ItemImage.objects.create(
|
||||
# item=self.test_item,
|
||||
# image=img
|
||||
# )
|
||||
# obj_name = f'{self.id} - {self.item.name} - {self.id}'
|
@ -0,0 +1,5 @@
|
||||
[pytest]
|
||||
DJANGO_SETTINGS_MODULE = xmrauctions.settings
|
||||
addopts = --cov=.
|
||||
--cov-report term-missing:skip-covered
|
||||
--cov-fail-under 100
|
Reference in New Issue