Files
2022-08-03 00:20:23 +03:00

160 lines
5.6 KiB
Python

"""File containing Member app tests."""
from django.test import TestCase, Client
from unittest import skip
from django.contrib.auth.models import User
from members.models import Member, Payment, Request
from rest_framework.authtoken.models import Token
from datetime import timezone
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)
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_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.replace(tzinfo=timezone.utc)
.astimezone(tz=None)
.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.replace(tzinfo=timezone.utc)
.astimezone(tz=None)
.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())