# -*- coding:utf-8 -* # # Copyright 2016,2017 # - Skia # # Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM, # http://ae.utbm.fr. # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License a 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, write to the Free Sofware Foundation, Inc., 59 Temple # Place - Suite 330, Boston, MA 02111-1307, USA. # # from datetime import datetime import re import base64 import urllib from OpenSSL import crypto from django.test import TestCase from django.core.urlresolvers import reverse from django.core.management import call_command from django.conf import settings from core.models import User from counter.models import Product, Counter, Refilling class EbouticTest(TestCase): def setUp(self): call_command("populate") self.skia = User.objects.filter(username="skia").first() self.subscriber = User.objects.filter(username="subscriber").first() self.old_subscriber = User.objects.filter(username="old_subscriber").first() self.public = User.objects.filter(username="public").first() self.barbar = Product.objects.filter(code="BARB").first() self.refill = Product.objects.filter(code="15REFILL").first() self.cotis = Product.objects.filter(code="1SCOTIZ").first() self.eboutic = Counter.objects.filter(name="Eboutic").first() def generate_bank_valid_answer_from_page_content(self, content): content = str(content) basket_id = re.search(r"PBX_CMD\" value=\"(\d*)\"", content).group(1) amount = re.search(r"PBX_TOTAL\" value=\"(\d*)\"", content).group(1) query = "Amount=%s&BasketID=%s&Auto=42&Error=00000" % (amount, basket_id) with open("./eboutic/tests/private_key.pem") as f: PRIVKEY = f.read() with open("./eboutic/tests/public_key.pem") as f: settings.SITH_EBOUTIC_PUB_KEY = f.read() privkey = crypto.load_privatekey(crypto.FILETYPE_PEM, PRIVKEY) sig = crypto.sign(privkey, query, "sha1") b64sig = base64.b64encode(sig).decode("ascii") url = reverse("eboutic:etransation_autoanswer") + "?%s&Sig=%s" % ( query, urllib.parse.quote_plus(b64sig), ) response = self.client.get(url) return response def test_buy_simple_product_with_sith_account(self): self.client.login(username="subscriber", password="plop") Refilling( amount=10, counter=self.eboutic, operator=self.skia, customer=self.subscriber.customer, ).save() response = self.client.post( reverse("eboutic:main"), {"action": "add_product", "product_id": self.barbar.id}, ) self.assertTrue( '\\n' ' \\n' "\\n Barbar: 1.70 \\xe2\\x82\\xac" in str(response.content) ) response = self.client.post(reverse("eboutic:command")) self.assertTrue( "\\n Barbar\\n 1\\n" " 1.70 \\xe2\\x82\\xac\\n " in str(response.content) ) response = self.client.post( reverse("eboutic:pay_with_sith"), {"action": "pay_with_sith_account"} ) self.assertTrue( "Le paiement a \\xc3\\xa9t\\xc3\\xa9 effectu\\xc3\\xa9\\n" in str(response.content) ) response = self.client.get( reverse( "core:user_account_detail", kwargs={ "user_id": self.subscriber.id, "year": datetime.now().year, "month": datetime.now().month, }, ) ) self.assertTrue( 'class="selected_tab">Compte (8.30 \\xe2\\x82\\xac)' in str(response.content) ) self.assertTrue( 'Eboutic\\n Subscribed User\\n' " Barbar\\n 1\\n 1.70 \\xe2\\x82\\xac\\n" " Compte utilisateur" in str(response.content) ) def test_buy_simple_product_with_credit_card(self): self.client.login(username="subscriber", password="plop") response = self.client.post( reverse("eboutic:main"), {"action": "add_product", "product_id": self.barbar.id}, ) self.assertTrue( '\\n' ' \\n' "\\n Barbar: 1.70 \\xe2\\x82\\xac" in str(response.content) ) response = self.client.post(reverse("eboutic:command")) self.assertTrue( "\\n Barbar\\n 1\\n" " 1.70 \\xe2\\x82\\xac\\n " in str(response.content) ) response = self.generate_bank_valid_answer_from_page_content(response.content) self.assertTrue(response.status_code == 200) self.assertTrue(response.content.decode("utf-8") == "") response = self.client.get( reverse( "core:user_account_detail", kwargs={ "user_id": self.subscriber.id, "year": datetime.now().year, "month": datetime.now().month, }, ) ) self.assertTrue( 'class="selected_tab">Compte (0.00 \\xe2\\x82\\xac)' in str(response.content) ) self.assertTrue( 'Eboutic\\n Subscribed User\\n' " Barbar\\n 1\\n 1.70 \\xe2\\x82\\xac\\n" " Carte bancaire" in str(response.content) ) def test_alter_basket_with_credit_card(self): self.client.login(username="subscriber", password="plop") response = self.client.post( reverse("eboutic:main"), {"action": "add_product", "product_id": self.barbar.id}, ) self.assertTrue( '\\n' ' \\n' "\\n Barbar: 1.70 \\xe2\\x82\\xac" in str(response.content) ) response = self.client.post(reverse("eboutic:command")) self.assertTrue( "\\n Barbar\\n 1\\n" " 1.70 \\xe2\\x82\\xac\\n " in str(response.content) ) response_altered = self.client.post( reverse("eboutic:main"), {"action": "add_product", "product_id": self.barbar.id}, ) self.assertTrue( '\\n' ' \\n' "\\n Barbar: 3.40 \\xe2\\x82\\xac" in str(response_altered.content) ) response = self.generate_bank_valid_answer_from_page_content(response.content) self.assertTrue(response.status_code == 400) self.assertTrue( "Payment failed with error: SuspiciousOperation('Basket total and amount do not match'" in response.content.decode("utf-8") ) def test_buy_refill_product_with_credit_card(self): self.client.login(username="subscriber", password="plop") response = self.client.post( reverse("eboutic:main"), {"action": "add_product", "product_id": self.refill.id}, ) self.assertTrue( '\\n' ' \\n' "\\n Rechargement 15 \\xe2\\x82\\xac: 15.00 \\xe2\\x82\\xac" in str(response.content) ) response = self.client.post(reverse("eboutic:command")) self.assertTrue( "\\n Rechargement 15 \\xe2\\x82\\xac\\n 1\\n" " 15.00 \\xe2\\x82\\xac\\n " in str(response.content) ) response = self.generate_bank_valid_answer_from_page_content(response.content) self.assertTrue(response.status_code == 200) self.assertTrue(response.content.decode("utf-8") == "") response = self.client.get( reverse( "core:user_account_detail", kwargs={ "user_id": self.subscriber.id, "year": datetime.now().year, "month": datetime.now().month, }, ) ) self.assertTrue( 'class="selected_tab">Compte (15.00 \\xe2\\x82\\xac)' in str(response.content) ) self.assertTrue( "\\n \\n \\n" " 15.00 \\xe2\\x82\\xac" in str(response.content) ) def test_buy_subscribe_product_with_credit_card(self): self.client.login(username="old_subscriber", password="plop") response = self.client.get( reverse("core:user_profile", kwargs={"user_id": self.old_subscriber.id}) ) self.assertTrue("Non cotisant" in str(response.content)) response = self.client.post( reverse("eboutic:main"), {"action": "add_product", "product_id": self.cotis.id}, ) self.assertTrue( '\\n' ' \\n' "\\n Cotis 1 semestre: 15.00 \\xe2\\x82\\xac" in str(response.content) ) response = self.client.post(reverse("eboutic:command")) self.assertTrue( "\\n Cotis 1 semestre\\n 1\\n" " 15.00 \\xe2\\x82\\xac\\n " in str(response.content) ) response = self.generate_bank_valid_answer_from_page_content(response.content) self.assertTrue(response.status_code == 200) self.assertTrue(response.content.decode("utf-8") == "") response = self.client.get( reverse( "core:user_account_detail", kwargs={ "user_id": self.old_subscriber.id, "year": datetime.now().year, "month": datetime.now().month, }, ) ) self.assertTrue( 'class="selected_tab">Compte (0.00 \\xe2\\x82\\xac)' in str(response.content) ) self.assertTrue( "\\n \\n \\n" " 15.00 \\xe2\\x82\\xac" in str(response.content) ) response = self.client.get( reverse("core:user_profile", kwargs={"user_id": self.old_subscriber.id}) ) self.assertTrue("Cotisant jusqu\\'au" in str(response.content))