mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-09 19:40:19 +00:00
Add some Eboutic tests
This commit is contained in:
151
eboutic/tests.py
151
eboutic/tests.py
@ -21,7 +21,156 @@
|
||||
# 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 Customer, ProductType, 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)
|
||||
self.assertTrue(response.status_code == 200)
|
||||
self.assertTrue(response.content.decode("utf-8") == "")
|
||||
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("<input type=\"hidden\" name=\"action\" value=\"add_product\">\\n"
|
||||
" <button type=\"submit\" name=\"product_id\" value=\"4\"> + </button>\\n"
|
||||
"</form>\\n Barbar: 1.70 \\xe2\\x82\\xac</li>" in str(response.content))
|
||||
response = self.client.post(reverse("eboutic:command"))
|
||||
self.assertTrue("<tr>\\n <td>Barbar</td>\\n <td>1</td>\\n"
|
||||
" <td>1.70 \\xe2\\x82\\xac</td>\\n </tr>" 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)</a>" in str(response.content))
|
||||
self.assertTrue("<td>Eboutic</td>\\n <td><a href=\"/user/3/\">Subscribed User</a></td>\\n"
|
||||
" <td>Barbar</td>\\n <td>1</td>\\n <td>1.70 \\xe2\\x82\\xac</td>\\n"
|
||||
" <td>Compte utilisateur</td>" 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("<input type=\"hidden\" name=\"action\" value=\"add_product\">\\n"
|
||||
" <button type=\"submit\" name=\"product_id\" value=\"4\"> + </button>\\n"
|
||||
"</form>\\n Barbar: 1.70 \\xe2\\x82\\xac</li>" in str(response.content))
|
||||
response = self.client.post(reverse("eboutic:command"))
|
||||
self.assertTrue("<tr>\\n <td>Barbar</td>\\n <td>1</td>\\n"
|
||||
" <td>1.70 \\xe2\\x82\\xac</td>\\n </tr>" in str(response.content))
|
||||
|
||||
response = self.generate_bank_valid_answer_from_page_content(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 (0.00 \\xe2\\x82\\xac)</a>" in str(response.content))
|
||||
self.assertTrue("<td>Eboutic</td>\\n <td><a href=\"/user/3/\">Subscribed User</a></td>\\n"
|
||||
" <td>Barbar</td>\\n <td>1</td>\\n <td>1.70 \\xe2\\x82\\xac</td>\\n"
|
||||
" <td>Carte bancaire</td>" in str(response.content))
|
||||
|
||||
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("<input type=\"hidden\" name=\"action\" value=\"add_product\">\\n"
|
||||
" <button type=\"submit\" name=\"product_id\" value=\"3\"> + </button>\\n"
|
||||
"</form>\\n Rechargement 15 \\xe2\\x82\\xac: 15.00 \\xe2\\x82\\xac</li>" in str(response.content))
|
||||
response = self.client.post(reverse("eboutic:command"))
|
||||
self.assertTrue("<tr>\\n <td>Rechargement 15 \\xe2\\x82\\xac</td>\\n <td>1</td>\\n"
|
||||
" <td>15.00 \\xe2\\x82\\xac</td>\\n </tr>" in str(response.content))
|
||||
|
||||
response = self.generate_bank_valid_answer_from_page_content(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 (15.00 \\xe2\\x82\\xac)</a>" in str(response.content))
|
||||
self.assertTrue("<td>\\n <ul>\\n \\n "
|
||||
"<li>1 x Rechargement 15 \\xe2\\x82\\xac - 15.00 \\xe2\\x82\\xac</li>\\n"
|
||||
" \\n </ul>\\n </td>\\n"
|
||||
" <td>15.00 \\xe2\\x82\\xac</td>" 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("<input type=\"hidden\" name=\"action\" value=\"add_product\">\\n"
|
||||
" <button type=\"submit\" name=\"product_id\" value=\"1\"> + </button>\\n"
|
||||
"</form>\\n Cotis 1 semestre: 15.00 \\xe2\\x82\\xac</li>" in str(response.content))
|
||||
response = self.client.post(reverse("eboutic:command"))
|
||||
self.assertTrue("<tr>\\n <td>Cotis 1 semestre</td>\\n <td>1</td>\\n"
|
||||
" <td>15.00 \\xe2\\x82\\xac</td>\\n </tr>" in str(response.content))
|
||||
|
||||
response = self.generate_bank_valid_answer_from_page_content(response.content)
|
||||
|
||||
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)</a>" in str(response.content))
|
||||
self.assertTrue("<td>\\n <ul>\\n \\n "
|
||||
"<li>1 x Cotis 1 semestre - 15.00 \\xe2\\x82\\xac</li>\\n"
|
||||
" \\n </ul>\\n </td>\\n"
|
||||
" <td>15.00 \\xe2\\x82\\xac</td>" 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))
|
||||
|
||||
|
||||
|
||||
# Create your tests here.
|
||||
|
28
eboutic/tests/private_key.pem
Normal file
28
eboutic/tests/private_key.pem
Normal file
@ -0,0 +1,28 @@
|
||||
-----BEGIN PRIVATE KEY-----
|
||||
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCzLRPk/qRi1KNR
|
||||
a9+r5Mm00avv0jwawTrQ02Oq86/1GBvWWiPkme5GYx8g18TxHX/qvE7N+P/tfMYt
|
||||
PaFh+5z07BAQYBehdJrZF6XxW5j9GZiCAfIG2uOZ7WwHhT/C8ksUF/rq24Yde5b9
|
||||
uZzrT3G66N019iaLHKK0fIE69T574RP0gpby9PY/13XHj+DQ3dp29+K9V4h53uQt
|
||||
axrVmW8QQKvuJVD2T6DySg/l6sYYD3p5B2bbSjS/R1mALT3qFGZaB4GsUblqTMJF
|
||||
wvkcxBblxuIOjbsrIViH6xSdN/LKK9fPyFkQhs1xbWlX2ZxfVHGDtYlizf7B/Ai0
|
||||
NL4zL0anAgMBAAECggEAJd71QYWBAVKoYmFGmXJ2H73hdYMeKRmGcPT9L/jpzAgY
|
||||
ein7RCo07rOstKhmfAAcNWUv0uE6Vtv0l0NbhPZFqo7qpktpMzsOL6yL6oPNxlFO
|
||||
psv6d/B0Aujn2H8VhwLnU4vuAQ39PuYMd/xval0UUMk/WFR6uRSIX1WhivCjEFOb
|
||||
bT2ZOmw8LoTTnzScop3Q9Mte0VCNAL6sZIhBvgGJBM8bKjGg7CQ3yJsn4PM5w6fK
|
||||
T7RFnHh/SeVwaB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----END PRIVATE KEY-----
|
9
eboutic/tests/public_key.pem
Normal file
9
eboutic/tests/public_key.pem
Normal file
@ -0,0 +1,9 @@
|
||||
-----BEGIN PUBLIC KEY-----
|
||||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsy0T5P6kYtSjUWvfq+TJ
|
||||
tNGr79I8GsE60NNjqvOv9Rgb1loj5JnuRmMfINfE8R1/6rxOzfj/7XzGLT2hYfuc
|
||||
9OwQEGAXoXSa2Rel8VuY/RmYggHyBtrjme1sB4U/wvJLFBf66tuGHXuW/bmc609x
|
||||
uujdNfYmixyitHyBOvU+e+ET9IKW8vT2P9d1x4/g0N3advfivVeIed7kLWsa1Zlv
|
||||
EECr7iVQ9k+g8koP5erGGA96eQdm20o0v0dZgC096hRmWgeBrFG5akzCRcL5HMQW
|
||||
5cbiDo27KyFYh+sUnTfyyivXz8hZEIbNcW1pV9mcX1Rxg7WJYs3+wfwItDS+My9G
|
||||
pwIDAQAB
|
||||
-----END PUBLIC KEY-----
|
38
eboutic/tests/test.py
Executable file
38
eboutic/tests/test.py
Executable file
@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding:utf-8 -*
|
||||
#
|
||||
# Skia < skia AT libskia DOT so >
|
||||
#
|
||||
# Beerware licensed software - 2017
|
||||
#
|
||||
|
||||
import base64
|
||||
from OpenSSL import crypto
|
||||
|
||||
with open("./private_key.pem") as f:
|
||||
PRVKEY = f.read()
|
||||
with open("./public_key.pem") as f:
|
||||
PUBKEY = f.read()
|
||||
|
||||
string = "Amount=400&BasketID=4000&Auto=42&Error=00000\n"
|
||||
|
||||
# Sign
|
||||
prvkey = crypto.load_privatekey(crypto.FILETYPE_PEM, PRVKEY)
|
||||
sig = crypto.sign(prvkey, string, "sha1")
|
||||
b64sig = base64.b64encode(sig)
|
||||
print(b64sig)
|
||||
|
||||
# Verify
|
||||
pubkey = crypto.load_publickey(crypto.FILETYPE_PEM, PUBKEY)
|
||||
cert = crypto.X509()
|
||||
cert.set_pubkey(pubkey)
|
||||
sig = base64.b64decode(b64sig)
|
||||
try:
|
||||
crypto.verify(cert, sig, string, "sha1")
|
||||
print("Verify OK")
|
||||
except:
|
||||
print("Verify failed")
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user