156 lines
6.7 KiB
Python
156 lines
6.7 KiB
Python
"""File containing Member app tests."""
|
|
|
|
from django.test import TestCase, Client
|
|
from django.contrib.auth.models import User
|
|
from members.management.commands.createsahkopiikkiuser import Command as SahkopiikkiCommand
|
|
from members.models import Member, Payment, Request
|
|
from rest_framework.authtoken.models import Token
|
|
|
|
import logging
|
|
import os
|
|
import pyexcel
|
|
|
|
|
|
class MemberRegisterTestCase(TestCase):
|
|
"""Tests member registration."""
|
|
|
|
def setUp(self):
|
|
"""Setup testing environment by creating member and admin."""
|
|
memb = Member.objects.create(first_name="Tidus", last_name="Tester", email="tidus@tester.fi")
|
|
payment = Payment.objects.create(member=memb, source='AYY')
|
|
appl = Request.objects.create(
|
|
first_name="Liisa", last_name="Mattila",
|
|
email="liisa.mattila@pylly.com", POR="Kouvola",
|
|
AYY=True, jas=False)
|
|
|
|
username, password = 'test_admin', 'password123'
|
|
test_admin = User.objects.create_superuser(
|
|
username, 'myemail@test.com', password)
|
|
self.c = Client()
|
|
self.c.login(username=username, password=password)
|
|
|
|
sc = SahkopiikkiCommand()
|
|
sc.handle()
|
|
|
|
def test_member_created(self):
|
|
"""Test member creation."""
|
|
exists = Member.objects.filter(first_name="Tidus").exists()
|
|
self.assertTrue(exists)
|
|
|
|
def test_import_csv_single_line(self):
|
|
"""Test csv import only with single line in csv file."""
|
|
|
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
with open(os.path.join(current_dir, 'test_resources', 'single_line_import.csv')) as csvFile:
|
|
response = self.c.post('/members/import_csv', {
|
|
'csvFile': csvFile,
|
|
'delimiter': ';',
|
|
'payment_source': 'AYY'
|
|
}, follow=True)
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
def test_import_csv_multi_line(self):
|
|
"""Test csv import with multilined csv."""
|
|
|
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
with open(os.path.join(current_dir, 'test_resources', 'multi_line_import.csv')) as csvFile:
|
|
response = self.c.post('/members/import_csv', {
|
|
'csvFile': csvFile,
|
|
'delimiter': ';',
|
|
'payment_source': 'AYY'
|
|
}, follow=True)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
def test_autocomplete_search_found(self):
|
|
"""Test member autocomplete search"""
|
|
search_terms = 'Tidus'
|
|
response = self.c.get('/members/member-autocomplete?q={}'.format(search_terms), follow=True)
|
|
results = response.json()['results']
|
|
self.assertEqual(len(results), 1)
|
|
|
|
def test_autocomplete_search_not_found(self):
|
|
"""Test member autocomplete search"""
|
|
search_terms = 'Notfound'
|
|
response = self.c.get('/members/member-autocomplete?q={}'.format(search_terms), follow=True)
|
|
results = response.json()['results']
|
|
self.assertEqual(len(results), 0)
|
|
|
|
def test_sahkopiikki_check_by_email_not_found(self):
|
|
"""Test if sähköpiikki auth and search work"""
|
|
email = 'teppo@tulppu.fi'
|
|
wrong_email = 'asd@asd.fi'
|
|
Member.objects.create(email=email, first_name='Teppo', last_name='Tulppu')
|
|
token = Token.objects.get(user__username='sahkopiikki').key
|
|
self.c.defaults['HTTP_AUTHORIZATION'] = 'Token ' + token
|
|
|
|
response = self.c.get('/members/check?email={}'.format(wrong_email), follow=True)
|
|
self.assertEqual(response.json()['exists'], False)
|
|
|
|
def test_sahkopiikki_check_by_email_found(self):
|
|
"""Test if sähköpiikki auth and search work"""
|
|
email = 'teppo@tulppu.fi'
|
|
Member.objects.create(email=email, first_name='Teppo', last_name='Tulppu')
|
|
token = Token.objects.get(user__username='sahkopiikki').key
|
|
self.c.defaults['HTTP_AUTHORIZATION'] = 'Token ' + token
|
|
|
|
response = self.c.get('/members/check?email={}'.format(email), follow=True)
|
|
self.assertEqual(response.json()['exists'], True)
|
|
|
|
def test_sahkopiikki_check_by_email_forbidden(self):
|
|
"""Test if sähköpiikki auth and search work"""
|
|
email = 'teppo@tulppu.fi'
|
|
Member.objects.create(email=email, first_name='Teppo', last_name='Tulppu')
|
|
token = Token.objects.get(user__username='sahkopiikki').key
|
|
self.c.defaults['HTTP_AUTHORIZATION'] = 'Token ' + token + 'DERP'
|
|
|
|
response = self.c.get('/members/check?email={}'.format(email), follow=True)
|
|
self.assertEqual(response.status_code, 401)
|
|
|
|
def test_export_members_excel(self):
|
|
"""Test if the user can download an excel file of the member register"""
|
|
resp = self.c.get('/members/export_members')
|
|
content_type = 'application/vnd.ms-excel'
|
|
self.assertIn(content_type, resp['Content-Type'])
|
|
|
|
content = resp.content
|
|
arrays = pyexcel.get_array(file_content=content, file_type='xlsx')
|
|
tidus_array = ['Tidus', 'Tester', 'tidus@tester.fi', '', '0', '0']
|
|
self.assertIn(tidus_array, arrays)
|
|
|
|
def test_export_payments_excel(self):
|
|
"""Test if the user can download an excel file of the payment register"""
|
|
resp = self.c.get('/members/export_payments')
|
|
content_type = 'application/vnd.ms-excel'
|
|
self.assertIn(content_type, resp['Content-Type'])
|
|
|
|
content = resp.content
|
|
arrays = pyexcel.get_array(file_content=content, file_type='xlsx')
|
|
created = Payment.objects.get(member__email='tidus@tester.fi').date.strftime('%Y-%m-%d %H:%M:%S')
|
|
tidus_array = ['Tidus Tester', created, 'AYY']
|
|
self.assertIn(tidus_array, arrays)
|
|
|
|
def test_export_applications_excel(self):
|
|
"""Test if the user can download an excel file of the member application register"""
|
|
resp = self.c.get('/members/export_applications')
|
|
content_type = 'application/vnd.ms-excel'
|
|
self.assertIn(content_type, resp['Content-Type'])
|
|
|
|
content = resp.content
|
|
arrays = pyexcel.get_array(file_content=content, file_type='xlsx')
|
|
submitted = Request.objects.get(email='liisa.mattila@pylly.com').submitted.strftime('%Y-%m-%d %H:%M:%S')
|
|
liisa_array = ['Liisa', 'Mattila', 'liisa.mattila@pylly.com', 'Kouvola', '1', '0', submitted]
|
|
self.assertIn(liisa_array, arrays)
|
|
|
|
def test_submit_member_application(self):
|
|
"""Test if submitting a member application works"""
|
|
data = {
|
|
'first_name': 'Seppo', 'last_name': 'Saastamoinen',
|
|
'email': 'seppo@saastamoin.en', 'jas': 'on',
|
|
'POR': 'Dipolin viinibaari'
|
|
}
|
|
resp = self.c.post('/members/submit_application', data=data)
|
|
self.assertEqual(resp.status_code, 200)
|
|
|
|
self.assertTrue(Request.objects.filter(email='seppo@saastamoin.en').exists())
|