Fix login tests

This commit is contained in:
Christian Cueni 2021-05-12 12:40:22 +02:00
parent 4b1ddd2627
commit 2718362cee
11 changed files with 378 additions and 1066 deletions

View File

@ -400,7 +400,6 @@ ALLOW_BETA_LOGIN = True
HEP_ADMIN_USER = os.environ.get("HEP_ADMIN_USER") HEP_ADMIN_USER = os.environ.get("HEP_ADMIN_USER")
HEP_ADMIN_PASSWORD = os.environ.get("HEP_ADMIN_PASSWORD") HEP_ADMIN_PASSWORD = os.environ.get("HEP_ADMIN_PASSWORD")
HEP_URL = os.environ.get("HEP_URL") HEP_URL = os.environ.get("HEP_URL")
HEP_MYSKILLBOX_GROUP_ID = 5
# HEP Oauth # HEP Oauth
AUTHLIB_OAUTH_CLIENTS = { AUTHLIB_OAUTH_CLIENTS = {

21
server/oauth/factories.py Normal file
View File

@ -0,0 +1,21 @@
import time
from datetime import timedelta
import factory
from django.utils import timezone
from oauth.models import OAuth2Token
IN_A_HOUR = timezone.now() + timedelta(hours=1)
IN_A_HOUR_UNIX = time.mktime(IN_A_HOUR.timetuple())
class Oauth2TokenFactory(factory.django.DjangoModelFactory):
class Meta:
model = OAuth2Token
token_type = 'hep'
access_token = 'asdfgghh'
refresh_token = 'yxcvbnnm'
expires_at = IN_A_HOUR_UNIX

View File

@ -25,7 +25,6 @@ class HepClientNoTokenException(Exception):
class HepClient: class HepClient:
URL = settings.HEP_URL URL = settings.HEP_URL
WEBSITE_ID = 1
HEADERS = { HEADERS = {
'accept': 'application/json', 'accept': 'application/json',
'content-type': 'application/json' 'content-type': 'application/json'

View File

@ -1,4 +1,3 @@
# for Django framework
from authlib.integrations.django_client import OAuth from authlib.integrations.django_client import OAuth
from django.conf import settings from django.conf import settings

View File

@ -0,0 +1,158 @@
import time
from datetime import timedelta
from unittest.mock import patch
import requests
from authlib.integrations.django_client import DjangoRemoteApp
from django.contrib.auth.models import AnonymousUser
from django.contrib.sessions.middleware import SessionMiddleware
from django.test import TestCase, RequestFactory
from django.utils import timezone
from core.factories import UserFactory
from oauth.factories import Oauth2TokenFactory
from oauth.hep_client import HepClient
from oauth.user_signup_login_handler import EMAIL_NOT_VERIFIED, NO_VALID_LICENSE, UNKNOWN_ERROR
from oauth.views import authorize
from users.tests.mock_hep_data_factory import MockResponse, ME_DATA, VALID_STUDENT_ORDERS, VALID_TEACHERS_ORDERS
from users.factories import LicenseFactory
from users.models import Role, User, SchoolClass, License, UserData
IN_A_HOUR = timezone.now() + timedelta(hours=1)
IN_A_HOUR_UNIX = time.mktime(IN_A_HOUR.timetuple())
TOKEN = {
'token_type': 'hep',
'access_token': '123456',
'refresh_token': 'abcdqwer',
'expires_at': IN_A_HOUR_UNIX,
}
class LoginTests(TestCase):
def setUp(self):
self.user = UserFactory(username=ME_DATA['id'], email=ME_DATA['id'])
Role.objects.create_default_roles()
self.teacher_role = Role.objects.get_default_teacher_role()
self.factory = RequestFactory()
def _login(self, url):
request = self.factory.get(url)
middleware = SessionMiddleware()
middleware.process_request(request)
request.session.save()
request.user = AnonymousUser()
return authorize(request)
@patch.object(DjangoRemoteApp, 'authorize_access_token', return_value=TOKEN)
@patch.object(HepClient, 'user_details', return_value=ME_DATA)
def test_user_data_is_synced_on_login(self, user_mock, authorize_mock):
old_mail = 'aschi@iterativ.ch'
self.user.hep_id = ME_DATA['id']
self.user.email = old_mail
self.user.username = old_mail
self.user.save()
now = timezone.now()
expiry_date = now + timedelta(365)
LicenseFactory(expire_date=expiry_date, licensee=self.user, for_role=self.teacher_role).save()
response = self._login('/api/oauth/authorize?code=1234')
user = User.objects.get(hep_id=self.user.hep_id)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, '/login-success?state=success')
self.assertEqual(user.username, ME_DATA['email'])
self.assertEqual(user.email, ME_DATA['email'])
self.assertTrue(self.user.is_authenticated)
@patch.object(HepClient, 'fetch_eorders', return_value=VALID_TEACHERS_ORDERS)
@patch.object(DjangoRemoteApp, 'authorize_access_token', return_value=TOKEN)
@patch.object(HepClient, 'user_details', return_value=ME_DATA)
def test_teacher_can_login_with_valid_license(self, user_mock, authorize_mock, orders_mock):
response = self._login('/api/oauth/authorize?code=1234')
user = User.objects.get(email=ME_DATA['email'])
user_role_key = user.user_roles.get(user=user).role.key
self.assertEqual(user_role_key, Role.objects.TEACHER_KEY)
license = License.objects.get(licensee=user)
self.assertEqual(license.for_role.key, Role.objects.TEACHER_KEY)
school_class = SchoolClass.objects.get(users__in=[user])
self.assertIsNotNone(school_class)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, '/login-success?state=success')
self.assertTrue(self.user.is_authenticated)
try:
UserData.objects.get(user=user)
self.fail('LoginTests.test_teacher_can_login_with_valid_license: Userdata should not exist')
except:
pass
@patch.object(HepClient, 'fetch_eorders', return_value=VALID_STUDENT_ORDERS)
@patch.object(DjangoRemoteApp, 'authorize_access_token', return_value=TOKEN)
@patch.object(HepClient, 'user_details', return_value=ME_DATA)
def test_student_can_login_with_valid_license(self, user_mock, authorize_mock, orders_mock):
response = self._login('/api/oauth/authorize?code=1234')
user = User.objects.get(email=ME_DATA['email'])
user_role_key = user.user_roles.get(user=user).role.key
self.assertEqual(user_role_key, Role.objects.STUDENT_KEY)
license = License.objects.get(licensee=user)
self.assertEqual(license.for_role.key, Role.objects.STUDENT_KEY)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, '/login-success?state=success')
self.assertTrue(self.user.is_authenticated)
@patch.object(HepClient, 'fetch_eorders', return_value=VALID_STUDENT_ORDERS)
@patch.object(DjangoRemoteApp, 'authorize_access_token', return_value=TOKEN)
@patch.object(HepClient, 'user_details', return_value=ME_DATA)
def test_user_with_unconfirmed_email_cannot_login(self, user_mock, authorize_mock, verified_mock):
response = self._login('/api/oauth/authorize?code=1234')
User.objects.get(email=ME_DATA['email'])
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, f'/login-success?state={EMAIL_NOT_VERIFIED}')
@patch.object(HepClient, 'fetch_eorders', return_value=[])
@patch.object(DjangoRemoteApp, 'authorize_access_token', return_value=TOKEN)
@patch.object(HepClient, 'user_details', return_value=ME_DATA)
def test_user_can_login_without_license(self, me_mock, product_mock, verified_mock):
response = self._login('/api/oauth/authorize?code=1234')
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, f'/login-success?state={NO_VALID_LICENSE}')
self.assertTrue(self.user.is_authenticated)
@patch.object(HepClient, 'fetch_eorders', return_value=[])
@patch.object(DjangoRemoteApp, 'authorize_access_token', return_value=TOKEN)
@patch.object(HepClient, 'user_details', return_value=ME_DATA)
def test_user_can_login_local_license_invalid(self, me_mock, product_mock, verified_mock):
now = timezone.now()
expiry_date = now - timedelta(1)
LicenseFactory(expire_date=expiry_date, licensee=self.user, for_role=self.teacher_role).save()
response = self._login('/api/oauth/authorize?code=1234')
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, f'/login-success?state={NO_VALID_LICENSE}')
self.assertTrue(self.user.is_authenticated)
@patch.object(requests, 'get', return_value=MockResponse(500))
def test_user_gets_notified_if_server_error(self, post_mock):
response = self._login('/api/oauth/authorize?code=1234')
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, f'/login-success?state={UNKNOWN_ERROR}')

View File

@ -1,10 +1,11 @@
from authlib.integrations.base_client import OAuthError
from django.conf import settings from django.conf import settings
from django.shortcuts import redirect from django.shortcuts import redirect
from oauth.hep_client import HepClient from oauth.hep_client import HepClient
from oauth.oauth_client import oauth from oauth.oauth_client import oauth
from oauth.models import OAuth2Token from oauth.models import OAuth2Token
from oauth.user_signup_login_handler import handle_user_and_verify_products, EMAIL_NOT_VERIFIED from oauth.user_signup_login_handler import handle_user_and_verify_products, EMAIL_NOT_VERIFIED, UNKNOWN_ERROR
from django.contrib.auth import login as dj_login from django.contrib.auth import login as dj_login
@ -16,21 +17,22 @@ def login(request):
def authorize(request): def authorize(request):
hep_client = HepClient() hep_client = HepClient()
token = oauth.hep.authorize_access_token(request) try:
user_data = hep_client.user_details(token=token) token = oauth.hep.authorize_access_token(request)
user_data = hep_client.user_details(token=token)
user, status_msg = handle_user_and_verify_products(user_data, token) user, status_msg = handle_user_and_verify_products(user_data, token)
user.sync_with_hep_data(user_data) user.sync_with_hep_data(user_data)
except OAuthError:
return redirect(f'/login-success?state={UNKNOWN_ERROR}')
if user and status_msg != EMAIL_NOT_VERIFIED: if user and status_msg != EMAIL_NOT_VERIFIED:
dj_login(request, user) dj_login(request, user)
OAuth2Token.objects.update_or_create_token(token, user) OAuth2Token.objects.update_or_create_token(token, user)
some = hep_client.active_myskillbox_product_for_customer(token=token)
if status_msg: if status_msg:
return redirect(f'/login-success?state={status_msg}') return redirect(f'/login-success?state={status_msg}')
# return status_msg # return status_msg
# return cls.return_login_message(status_msg) # return cls.return_login_message(status_msg)
return redirect(f'/login-success?state={status_msg}') return redirect(f'/login-success?state=success')

View File

@ -7,7 +7,7 @@ from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractUser, Permission from django.contrib.auth.models import AbstractUser, Permission
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.db import models from django.db import models
from django.utils.timezone import make_aware from django.utils.timezone import make_aware, is_aware
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.utils import timezone from django.utils import timezone
@ -316,6 +316,9 @@ class License(models.Model):
def is_product_active(expiry_date, isbn): def is_product_active(expiry_date, isbn):
now = timezone.now() now = timezone.now()
if not is_aware(expiry_date):
expiry_date = make_aware(expiry_date)
return expiry_date >= now >= expiry_date - timedelta(days=MYSKILLBOX_LICENSES[isbn]['duration']) return expiry_date >= now >= expiry_date - timedelta(days=MYSKILLBOX_LICENSES[isbn]['duration'])
def __str__(self): def __str__(self):

View File

@ -1,526 +1,94 @@
{ [
"items": [
{ {
"base_currency_code": "CHF", "id": 6,
"base_discount_amount": 0, "status": "paid",
"base_grand_total": 46, "order_total": 10000,
"base_discount_tax_compensation_amount": 0, "created_at": "2021-05-10T20:52:16.000000Z",
"base_shipping_amount": 0, "billing_address": {
"base_shipping_discount_amount": 0, "id": 20,
"base_shipping_incl_tax": 0, "salutation": "male",
"base_shipping_tax_amount": 0, "first_name": "Hans",
"base_subtotal": 44.88, "last_name": "Meier",
"base_subtotal_incl_tax": 46, "company": "Lustig GmbH",
"base_tax_amount": 1.12, "street": "Sulgenrain 12890",
"base_total_due": 46, "city": "Bern",
"base_to_global_rate": 1, "post_code": "3007",
"base_to_order_rate": 1, "country": "Schweiz"
"billing_address_id": 83693,
"created_at": "2018-07-19 15:05:33",
"customer_email": "1heptest19072018@mailinator.com",
"customer_firstname": "Test",
"customer_gender": 2,
"customer_group_id": 4,
"customer_id": 49124,
"customer_is_guest": 0,
"customer_lastname": "Test",
"customer_note": "coupon",
"customer_note_notify": 1,
"customer_prefix": "Frau",
"discount_amount": 0,
"email_sent": 1,
"entity_id": 57612,
"global_currency_code": "CHF",
"grand_total": 46,
"discount_tax_compensation_amount": 0,
"increment_id": "1004614768",
"is_virtual": 1,
"order_currency_code": "CHF",
"protect_code": "71aedb",
"quote_id": 104401,
"shipping_amount": 0,
"shipping_discount_amount": 0,
"shipping_discount_tax_compensation_amount": 0,
"shipping_incl_tax": 0,
"shipping_tax_amount": 0,
"state": "complete",
"status": "complete",
"store_currency_code": "CHF",
"store_id": 1,
"store_name": "hep verlag\nhep verlag\nhep verlag",
"store_to_base_rate": 0,
"store_to_order_rate": 0,
"subtotal": 44.88,
"subtotal_incl_tax": 46,
"tax_amount": 1.12,
"total_due": 46,
"total_item_count": 1,
"total_qty_ordered": 1,
"updated_at": "2018-07-19 15:05:33",
"weight": 0,
"items": [
{
"amount_refunded": 0,
"base_amount_refunded": 0,
"base_discount_amount": 0,
"base_discount_invoiced": 0,
"base_discount_tax_compensation_amount": 0,
"base_original_price": 46,
"base_price": 44.88,
"base_price_incl_tax": 46,
"base_row_invoiced": 0,
"base_row_total": 44.88,
"base_row_total_incl_tax": 46,
"base_tax_amount": 1.12,
"base_tax_invoiced": 0,
"created_at": "2018-07-19 15:05:33",
"discount_amount": 0,
"discount_invoiced": 0,
"discount_percent": 0,
"free_shipping": 0,
"discount_tax_compensation_amount": 0,
"is_qty_decimal": 0,
"is_virtual": 1,
"item_id": 80317,
"name": "Myskillbox Schüler Edition",
"no_discount": 0,
"order_id": 57612,
"original_price": 46,
"price": 44.88,
"price_incl_tax": 46,
"product_id": 8652,
"product_type": "virtual",
"qty_canceled": 0,
"qty_invoiced": 0,
"qty_ordered": 1,
"qty_refunded": 0,
"qty_shipped": 0,
"quote_item_id": 135166,
"row_invoiced": 0,
"row_total": 44.88,
"row_total_incl_tax": 46,
"sku": "978-3-0355-1397-4",
"store_id": 1,
"tax_amount": 1.12,
"tax_invoiced": 0,
"tax_percent": 2.5,
"updated_at": "2018-07-19 15:05:33",
"weight": 0.01
}
],
"billing_address": {
"address_type": "billing",
"city": "Test",
"country_id": "CH",
"customer_address_id": 47579,
"email": "1heptest19072018@mailinator.com",
"entity_id": 83693,
"firstname": "Test",
"lastname": "Test",
"parent_id": 57612,
"postcode": "0000",
"prefix": "Frau",
"street": [
"Test"
],
"telephone": null
},
"payment": {
"account_status": null,
"additional_information": [
"Rechnung",
null,
null
],
"amount_ordered": 46,
"base_amount_ordered": 46,
"base_shipping_amount": 0,
"cc_last4": null,
"entity_id": 57612,
"method": "checkmo",
"parent_id": 57612,
"shipping_amount": 0,
"extension_attributes": []
},
"status_histories": [
{
"comment": "payed by couponcode",
"created_at": "2018-07-19 15:05:33",
"entity_id": 244885,
"entity_name": "order",
"is_customer_notified": null,
"is_visible_on_front": 0,
"parent_id": 57612,
"status": "complete"
},
{
"comment": "licence-coupon \"ebf81a59b968\"",
"created_at": "2018-07-19 15:05:33",
"entity_id": 244884,
"entity_name": "order",
"is_customer_notified": null,
"is_visible_on_front": 0,
"parent_id": 57612,
"status": "complete"
},
{
"comment": null,
"created_at": "2018-07-19 15:05:33",
"entity_id": 244883,
"entity_name": "order",
"is_customer_notified": 0,
"is_visible_on_front": 0,
"parent_id": 57612,
"status": "complete"
},
{
"comment": "Exported to ERP",
"created_at": "2018-07-19 15:05:33",
"entity_id": 244882,
"entity_name": "order",
"is_customer_notified": 0,
"is_visible_on_front": 0,
"parent_id": 57612,
"status": "complete"
}
],
"extension_attributes": {
"shipping_assignments": [
{
"shipping": {
"total": {
"base_shipping_amount": 0,
"base_shipping_discount_amount": 0,
"base_shipping_incl_tax": 0,
"base_shipping_tax_amount": 0,
"shipping_amount": 0,
"shipping_discount_amount": 0,
"shipping_discount_tax_compensation_amount": 0,
"shipping_incl_tax": 0,
"shipping_tax_amount": 0
}
},
"items": [
{
"amount_refunded": 0,
"base_amount_refunded": 0,
"base_discount_amount": 0,
"base_discount_invoiced": 0,
"base_discount_tax_compensation_amount": 0,
"base_original_price": 46,
"base_price": 44.88,
"base_price_incl_tax": 46,
"base_row_invoiced": 0,
"base_row_total": 44.88,
"base_row_total_incl_tax": 46,
"base_tax_amount": 1.12,
"base_tax_invoiced": 0,
"created_at": "2018-07-19 15:05:33",
"discount_amount": 0,
"discount_invoiced": 0,
"discount_percent": 0,
"free_shipping": 0,
"discount_tax_compensation_amount": 0,
"is_qty_decimal": 0,
"is_virtual": 1,
"item_id": 80317,
"name": "Gesellschaft Ausgabe A (eLehrmittel, Neuauflage)",
"no_discount": 0,
"order_id": 57612,
"original_price": 46,
"price": 44.88,
"price_incl_tax": 46,
"product_id": 8652,
"product_type": "virtual",
"qty_canceled": 0,
"qty_invoiced": 0,
"qty_ordered": 1,
"qty_refunded": 0,
"qty_shipped": 0,
"quote_item_id": 135166,
"row_invoiced": 0,
"row_total": 44.88,
"row_total_incl_tax": 46,
"sku": "978-3-0355-1082-9",
"store_id": 1,
"tax_amount": 1.12,
"tax_invoiced": 0,
"tax_percent": 2.5,
"updated_at": "2018-07-19 15:05:33",
"weight": 0.01
}
]
}
]
}
}, },
{ "delivery_address": {
"base_currency_code": "CHF", "id": 19,
"base_discount_amount": 0, "salutation": "male",
"base_grand_total": 24, "first_name": "Hans",
"base_discount_tax_compensation_amount": 0, "last_name": "Muster",
"base_shipping_amount": 0, "company": "Muster AG",
"base_shipping_discount_amount": 0, "street": "Ruderweg 24",
"base_shipping_incl_tax": 0, "city": "Bern",
"base_shipping_tax_amount": 0, "post_code": "3000",
"base_subtotal": 23.41, "country": "Schweiz"
"base_subtotal_incl_tax": 24, },
"base_tax_amount": 0.59, "entries": [
"base_total_due": 24,
"base_to_global_rate": 1,
"base_to_order_rate": 1,
"billing_address_id": 83696,
"created_at": "2018-07-19 15:19:00",
"customer_email": "1heptest19072018@mailinator.com",
"customer_firstname": "Test",
"customer_gender": 2,
"customer_group_id": 4,
"customer_id": 49124,
"customer_is_guest": 0,
"customer_lastname": "Test",
"customer_note": "coupon",
"customer_note_notify": 1,
"customer_prefix": "Frau",
"discount_amount": 0,
"email_sent": 1,
"entity_id": 57614,
"global_currency_code": "CHF",
"grand_total": 24,
"discount_tax_compensation_amount": 0,
"increment_id": "1004614770",
"is_virtual": 1,
"order_currency_code": "CHF",
"protect_code": "1a88e9",
"quote_id": 104403,
"shipping_amount": 0,
"shipping_discount_amount": 0,
"shipping_discount_tax_compensation_amount": 0,
"shipping_incl_tax": 0,
"shipping_tax_amount": 0,
"state": "complete",
"status": "complete",
"store_currency_code": "CHF",
"store_id": 1,
"store_name": "hep verlag\nhep verlag\nhep verlag",
"store_to_base_rate": 0,
"store_to_order_rate": 0,
"subtotal": 23.41,
"subtotal_incl_tax": 24,
"tax_amount": 0.59,
"total_due": 24,
"total_item_count": 1,
"total_qty_ordered": 1,
"updated_at": "2018-07-19 15:19:00",
"weight": 0,
"items": [
{
"amount_refunded": 0,
"base_amount_refunded": 0,
"base_discount_amount": 0,
"base_discount_invoiced": 0,
"base_discount_tax_compensation_amount": 0,
"base_original_price": 24,
"base_price": 23.41,
"base_price_incl_tax": 24,
"base_row_invoiced": 0,
"base_row_total": 23.41,
"base_row_total_incl_tax": 24,
"base_tax_amount": 0.59,
"base_tax_invoiced": 0,
"created_at": "2018-07-19 15:19:00",
"discount_amount": 0,
"discount_invoiced": 0,
"discount_percent": 0,
"free_shipping": 0,
"discount_tax_compensation_amount": 0,
"is_qty_decimal": 0,
"is_virtual": 1,
"item_id": 80320,
"name": "Gesellschaft Ausgabe A, Arbeitsheft (eLehrmittel, Neuauflage)",
"no_discount": 0,
"order_id": 57614,
"original_price": 24,
"price": 23.41,
"price_incl_tax": 24,
"product_id": 8654,
"product_type": "virtual",
"qty_canceled": 0,
"qty_invoiced": 0,
"qty_ordered": 1,
"qty_refunded": 0,
"qty_shipped": 0,
"quote_item_id": 135169,
"row_invoiced": 0,
"row_total": 23.41,
"row_total_incl_tax": 24,
"sku": "978-3-0355-1185-7",
"store_id": 1,
"tax_amount": 0.59,
"tax_invoiced": 0,
"tax_percent": 2.5,
"updated_at": "2018-07-19 15:19:00",
"weight": 0.01
}
],
"billing_address": {
"address_type": "billing",
"city": "Test",
"country_id": "CH",
"customer_address_id": 47579,
"email": "1heptest19072018@mailinator.com",
"entity_id": 83696,
"firstname": "Test",
"lastname": "Test",
"parent_id": 57614,
"postcode": "0000",
"prefix": "Frau",
"street": [
"Test"
],
"telephone": null
},
"payment": {
"account_status": null,
"additional_information": [
"Rechnung",
null,
null
],
"amount_ordered": 24,
"base_amount_ordered": 24,
"base_shipping_amount": 0,
"cc_last4": null,
"entity_id": 57614,
"method": "checkmo",
"parent_id": 57614,
"shipping_amount": 0,
"extension_attributes": []
},
"status_histories": [
{
"comment": "payed by couponcode",
"created_at": "2018-07-19 15:19:00",
"entity_id": 244890,
"entity_name": "order",
"is_customer_notified": null,
"is_visible_on_front": 0,
"parent_id": 57614,
"status": "complete"
},
{
"comment": "licence-coupon \"ece5e74a2b36\"",
"created_at": "2018-07-19 15:19:00",
"entity_id": 244889,
"entity_name": "order",
"is_customer_notified": null,
"is_visible_on_front": 0,
"parent_id": 57614,
"status": "complete"
},
{
"comment": null,
"created_at": "2018-07-19 15:19:00",
"entity_id": 244888,
"entity_name": "order",
"is_customer_notified": 0,
"is_visible_on_front": 0,
"parent_id": 57614,
"status": "complete"
},
{
"comment": "Exported to ERP",
"created_at": "2018-07-19 15:19:00",
"entity_id": 244887,
"entity_name": "order",
"is_customer_notified": 0,
"is_visible_on_front": 0,
"parent_id": 57614,
"status": "complete"
}
],
"extension_attributes": {
"shipping_assignments": [
{
"shipping": {
"total": {
"base_shipping_amount": 0,
"base_shipping_discount_amount": 0,
"base_shipping_incl_tax": 0,
"base_shipping_tax_amount": 0,
"shipping_amount": 0,
"shipping_discount_amount": 0,
"shipping_discount_tax_compensation_amount": 0,
"shipping_incl_tax": 0,
"shipping_tax_amount": 0
}
},
"items": [
{
"amount_refunded": 0,
"base_amount_refunded": 0,
"base_discount_amount": 0,
"base_discount_invoiced": 0,
"base_discount_tax_compensation_amount": 0,
"base_original_price": 24,
"base_price": 23.41,
"base_price_incl_tax": 24,
"base_row_invoiced": 0,
"base_row_total": 23.41,
"base_row_total_incl_tax": 24,
"base_tax_amount": 0.59,
"base_tax_invoiced": 0,
"created_at": "2018-07-19 15:19:00",
"discount_amount": 0,
"discount_invoiced": 0,
"discount_percent": 0,
"free_shipping": 0,
"discount_tax_compensation_amount": 0,
"is_qty_decimal": 0,
"is_virtual": 1,
"item_id": 80320,
"name": "Gesellschaft Ausgabe A, Arbeitsheft (eLehrmittel, Neuauflage)",
"no_discount": 0,
"order_id": 57614,
"original_price": 24,
"price": 23.41,
"price_incl_tax": 24,
"product_id": 8654,
"product_type": "virtual",
"qty_canceled": 0,
"qty_invoiced": 0,
"qty_ordered": 1,
"qty_refunded": 0,
"qty_shipped": 0,
"quote_item_id": 135169,
"row_invoiced": 0,
"row_total": 23.41,
"row_total_incl_tax": 24,
"sku": "978-3-0355-1185-7",
"store_id": 1,
"tax_amount": 0.59,
"tax_invoiced": 0,
"tax_percent": 2.5,
"updated_at": "2018-07-19 15:19:00",
"weight": 0.01
}
]
}
]
}
}
],
"search_criteria": {
"filter_groups": [
{ {
"filters": [ "id": 3433,
{ "uri": "\/products\/some-produt",
"field": "customer_id", "url": null,
"value": "49124", "title": "helloclass",
"condition_type": "eq" "subtitle": "Lizenz gültig für 4 Jahre",
} "isbn": "111-2-3333-1397-4",
] "slug": "some-product",
"product_type": "eLehrmittel",
"product_form": "",
"cover": "https:\/\/hep-verlag.fra1.digitaloceanspaces.com\/staging\/products\/2921\/111-2-3333-1397-4.jpg",
"price": 100,
"price_total": 100,
"amount": 1,
"authors": []
} }
] ]
}, },
"total_count": 2 {
} "id": 5,
"status": "paid",
"order_total": 10000,
"created_at": "2021-05-10T20:52:16.000000Z",
"billing_address": {
"id": 20,
"salutation": "male",
"first_name": "Hans",
"last_name": "Meier",
"company": "Lustig GmbH",
"street": "Sulgenrain 12890",
"city": "Bern",
"post_code": "3007",
"country": "Schweiz"
},
"delivery_address": {
"id": 19,
"salutation": "male",
"first_name": "Hans",
"last_name": "Muster",
"company": "Muster AG",
"street": "Ruderweg 24",
"city": "Bern",
"post_code": "3000",
"country": "Schweiz"
},
"entries": [
{
"id": 3433,
"uri": "\/products\/myskillbox-lernende",
"url": null,
"title": "mySkillbox für Lernende ",
"subtitle": "Lizenz gültig für 4 Jahre",
"isbn": "978-3-0355-1397-4",
"slug": "myskillbox-lernende",
"product_type": "eLehrmittel",
"product_form": "",
"cover": "https:\/\/hep-verlag.fra1.digitaloceanspaces.com\/staging\/products\/2921\/978-3-0355-1397-4.jpg",
"price": 100,
"price_total": 100,
"amount": 1,
"authors": []
}
]
}
]

View File

@ -1,526 +1,94 @@
{ [
"items": [
{ {
"base_currency_code": "CHF", "id": 6,
"base_discount_amount": 0, "status": "paid",
"base_grand_total": 46, "order_total": 10000,
"base_discount_tax_compensation_amount": 0, "created_at": "2021-05-10T20:52:16.000000Z",
"base_shipping_amount": 0, "billing_address": {
"base_shipping_discount_amount": 0, "id": 20,
"base_shipping_incl_tax": 0, "salutation": "male",
"base_shipping_tax_amount": 0, "first_name": "Hans",
"base_subtotal": 44.88, "last_name": "Meier",
"base_subtotal_incl_tax": 46, "company": "Lustig GmbH",
"base_tax_amount": 1.12, "street": "Sulgenrain 12890",
"base_total_due": 46, "city": "Bern",
"base_to_global_rate": 1, "post_code": "3007",
"base_to_order_rate": 1, "country": "Schweiz"
"billing_address_id": 83693,
"created_at": "2018-07-19 15:05:33",
"customer_email": "1heptest19072018@mailinator.com",
"customer_firstname": "Test",
"customer_gender": 2,
"customer_group_id": 4,
"customer_id": 49124,
"customer_is_guest": 0,
"customer_lastname": "Test",
"customer_note": "coupon",
"customer_note_notify": 1,
"customer_prefix": "Frau",
"discount_amount": 0,
"email_sent": 1,
"entity_id": 57612,
"global_currency_code": "CHF",
"grand_total": 46,
"discount_tax_compensation_amount": 0,
"increment_id": "1004614768",
"is_virtual": 1,
"order_currency_code": "CHF",
"protect_code": "71aedb",
"quote_id": 104401,
"shipping_amount": 0,
"shipping_discount_amount": 0,
"shipping_discount_tax_compensation_amount": 0,
"shipping_incl_tax": 0,
"shipping_tax_amount": 0,
"state": "complete",
"status": "complete",
"store_currency_code": "CHF",
"store_id": 1,
"store_name": "hep verlag\nhep verlag\nhep verlag",
"store_to_base_rate": 0,
"store_to_order_rate": 0,
"subtotal": 44.88,
"subtotal_incl_tax": 46,
"tax_amount": 1.12,
"total_due": 46,
"total_item_count": 1,
"total_qty_ordered": 1,
"updated_at": "2018-07-19 15:05:33",
"weight": 0,
"items": [
{
"amount_refunded": 0,
"base_amount_refunded": 0,
"base_discount_amount": 0,
"base_discount_invoiced": 0,
"base_discount_tax_compensation_amount": 0,
"base_original_price": 46,
"base_price": 44.88,
"base_price_incl_tax": 46,
"base_row_invoiced": 0,
"base_row_total": 44.88,
"base_row_total_incl_tax": 46,
"base_tax_amount": 1.12,
"base_tax_invoiced": 0,
"created_at": "2018-07-19 15:05:33",
"discount_amount": 0,
"discount_invoiced": 0,
"discount_percent": 0,
"free_shipping": 0,
"discount_tax_compensation_amount": 0,
"is_qty_decimal": 0,
"is_virtual": 1,
"item_id": 80317,
"name": "Myskillbox Lehreredition",
"no_discount": 0,
"order_id": 57612,
"original_price": 46,
"price": 44.88,
"price_incl_tax": 46,
"product_id": 8652,
"product_type": "virtual",
"qty_canceled": 0,
"qty_invoiced": 0,
"qty_ordered": 1,
"qty_refunded": 0,
"qty_shipped": 0,
"quote_item_id": 135166,
"row_invoiced": 0,
"row_total": 44.88,
"row_total_incl_tax": 46,
"sku": "978-3-0355-1823-8",
"store_id": 1,
"tax_amount": 1.12,
"tax_invoiced": 0,
"tax_percent": 2.5,
"updated_at": "2018-07-19 15:05:33",
"weight": 0.01
}
],
"billing_address": {
"address_type": "billing",
"city": "Test",
"country_id": "CH",
"customer_address_id": 47579,
"email": "1heptest19072018@mailinator.com",
"entity_id": 83693,
"firstname": "Test",
"lastname": "Test",
"parent_id": 57612,
"postcode": "0000",
"prefix": "Frau",
"street": [
"Test"
],
"telephone": null
},
"payment": {
"account_status": null,
"additional_information": [
"Rechnung",
null,
null
],
"amount_ordered": 46,
"base_amount_ordered": 46,
"base_shipping_amount": 0,
"cc_last4": null,
"entity_id": 57612,
"method": "checkmo",
"parent_id": 57612,
"shipping_amount": 0,
"extension_attributes": []
},
"status_histories": [
{
"comment": "payed by couponcode",
"created_at": "2018-07-19 15:05:33",
"entity_id": 244885,
"entity_name": "order",
"is_customer_notified": null,
"is_visible_on_front": 0,
"parent_id": 57612,
"status": "complete"
},
{
"comment": "licence-coupon \"ebf81a59b968\"",
"created_at": "2018-07-19 15:05:33",
"entity_id": 244884,
"entity_name": "order",
"is_customer_notified": null,
"is_visible_on_front": 0,
"parent_id": 57612,
"status": "complete"
},
{
"comment": null,
"created_at": "2018-07-19 15:05:33",
"entity_id": 244883,
"entity_name": "order",
"is_customer_notified": 0,
"is_visible_on_front": 0,
"parent_id": 57612,
"status": "complete"
},
{
"comment": "Exported to ERP",
"created_at": "2018-07-19 15:05:33",
"entity_id": 244882,
"entity_name": "order",
"is_customer_notified": 0,
"is_visible_on_front": 0,
"parent_id": 57612,
"status": "complete"
}
],
"extension_attributes": {
"shipping_assignments": [
{
"shipping": {
"total": {
"base_shipping_amount": 0,
"base_shipping_discount_amount": 0,
"base_shipping_incl_tax": 0,
"base_shipping_tax_amount": 0,
"shipping_amount": 0,
"shipping_discount_amount": 0,
"shipping_discount_tax_compensation_amount": 0,
"shipping_incl_tax": 0,
"shipping_tax_amount": 0
}
},
"items": [
{
"amount_refunded": 0,
"base_amount_refunded": 0,
"base_discount_amount": 0,
"base_discount_invoiced": 0,
"base_discount_tax_compensation_amount": 0,
"base_original_price": 46,
"base_price": 44.88,
"base_price_incl_tax": 46,
"base_row_invoiced": 0,
"base_row_total": 44.88,
"base_row_total_incl_tax": 46,
"base_tax_amount": 1.12,
"base_tax_invoiced": 0,
"created_at": "2018-07-19 15:05:33",
"discount_amount": 0,
"discount_invoiced": 0,
"discount_percent": 0,
"free_shipping": 0,
"discount_tax_compensation_amount": 0,
"is_qty_decimal": 0,
"is_virtual": 1,
"item_id": 80317,
"name": "Gesellschaft Ausgabe A (eLehrmittel, Neuauflage)",
"no_discount": 0,
"order_id": 57612,
"original_price": 46,
"price": 44.88,
"price_incl_tax": 46,
"product_id": 8652,
"product_type": "virtual",
"qty_canceled": 0,
"qty_invoiced": 0,
"qty_ordered": 1,
"qty_refunded": 0,
"qty_shipped": 0,
"quote_item_id": 135166,
"row_invoiced": 0,
"row_total": 44.88,
"row_total_incl_tax": 46,
"sku": "978-3-0355-1082-9",
"store_id": 1,
"tax_amount": 1.12,
"tax_invoiced": 0,
"tax_percent": 2.5,
"updated_at": "2018-07-19 15:05:33",
"weight": 0.01
}
]
}
]
}
}, },
{ "delivery_address": {
"base_currency_code": "CHF", "id": 19,
"base_discount_amount": 0, "salutation": "male",
"base_grand_total": 24, "first_name": "Hans",
"base_discount_tax_compensation_amount": 0, "last_name": "Muster",
"base_shipping_amount": 0, "company": "Muster AG",
"base_shipping_discount_amount": 0, "street": "Ruderweg 24",
"base_shipping_incl_tax": 0, "city": "Bern",
"base_shipping_tax_amount": 0, "post_code": "3000",
"base_subtotal": 23.41, "country": "Schweiz"
"base_subtotal_incl_tax": 24, },
"base_tax_amount": 0.59, "entries": [
"base_total_due": 24,
"base_to_global_rate": 1,
"base_to_order_rate": 1,
"billing_address_id": 83696,
"created_at": "2018-07-19 15:19:00",
"customer_email": "1heptest19072018@mailinator.com",
"customer_firstname": "Test",
"customer_gender": 2,
"customer_group_id": 4,
"customer_id": 49124,
"customer_is_guest": 0,
"customer_lastname": "Test",
"customer_note": "coupon",
"customer_note_notify": 1,
"customer_prefix": "Frau",
"discount_amount": 0,
"email_sent": 1,
"entity_id": 57614,
"global_currency_code": "CHF",
"grand_total": 24,
"discount_tax_compensation_amount": 0,
"increment_id": "1004614770",
"is_virtual": 1,
"order_currency_code": "CHF",
"protect_code": "1a88e9",
"quote_id": 104403,
"shipping_amount": 0,
"shipping_discount_amount": 0,
"shipping_discount_tax_compensation_amount": 0,
"shipping_incl_tax": 0,
"shipping_tax_amount": 0,
"state": "complete",
"status": "complete",
"store_currency_code": "CHF",
"store_id": 1,
"store_name": "hep verlag\nhep verlag\nhep verlag",
"store_to_base_rate": 0,
"store_to_order_rate": 0,
"subtotal": 23.41,
"subtotal_incl_tax": 24,
"tax_amount": 0.59,
"total_due": 24,
"total_item_count": 1,
"total_qty_ordered": 1,
"updated_at": "2018-07-19 15:19:00",
"weight": 0,
"items": [
{
"amount_refunded": 0,
"base_amount_refunded": 0,
"base_discount_amount": 0,
"base_discount_invoiced": 0,
"base_discount_tax_compensation_amount": 0,
"base_original_price": 24,
"base_price": 23.41,
"base_price_incl_tax": 24,
"base_row_invoiced": 0,
"base_row_total": 23.41,
"base_row_total_incl_tax": 24,
"base_tax_amount": 0.59,
"base_tax_invoiced": 0,
"created_at": "2018-07-19 15:19:00",
"discount_amount": 0,
"discount_invoiced": 0,
"discount_percent": 0,
"free_shipping": 0,
"discount_tax_compensation_amount": 0,
"is_qty_decimal": 0,
"is_virtual": 1,
"item_id": 80320,
"name": "Gesellschaft Ausgabe A, Arbeitsheft (eLehrmittel, Neuauflage)",
"no_discount": 0,
"order_id": 57614,
"original_price": 24,
"price": 23.41,
"price_incl_tax": 24,
"product_id": 8654,
"product_type": "virtual",
"qty_canceled": 0,
"qty_invoiced": 0,
"qty_ordered": 1,
"qty_refunded": 0,
"qty_shipped": 0,
"quote_item_id": 135169,
"row_invoiced": 0,
"row_total": 23.41,
"row_total_incl_tax": 24,
"sku": "978-3-0355-1185-7",
"store_id": 1,
"tax_amount": 0.59,
"tax_invoiced": 0,
"tax_percent": 2.5,
"updated_at": "2018-07-19 15:19:00",
"weight": 0.01
}
],
"billing_address": {
"address_type": "billing",
"city": "Test",
"country_id": "CH",
"customer_address_id": 47579,
"email": "1heptest19072018@mailinator.com",
"entity_id": 83696,
"firstname": "Test",
"lastname": "Test",
"parent_id": 57614,
"postcode": "0000",
"prefix": "Frau",
"street": [
"Test"
],
"telephone": null
},
"payment": {
"account_status": null,
"additional_information": [
"Rechnung",
null,
null
],
"amount_ordered": 24,
"base_amount_ordered": 24,
"base_shipping_amount": 0,
"cc_last4": null,
"entity_id": 57614,
"method": "checkmo",
"parent_id": 57614,
"shipping_amount": 0,
"extension_attributes": []
},
"status_histories": [
{
"comment": "payed by couponcode",
"created_at": "2018-07-19 15:19:00",
"entity_id": 244890,
"entity_name": "order",
"is_customer_notified": null,
"is_visible_on_front": 0,
"parent_id": 57614,
"status": "complete"
},
{
"comment": "licence-coupon \"ece5e74a2b36\"",
"created_at": "2018-07-19 15:19:00",
"entity_id": 244889,
"entity_name": "order",
"is_customer_notified": null,
"is_visible_on_front": 0,
"parent_id": 57614,
"status": "complete"
},
{
"comment": null,
"created_at": "2018-07-19 15:19:00",
"entity_id": 244888,
"entity_name": "order",
"is_customer_notified": 0,
"is_visible_on_front": 0,
"parent_id": 57614,
"status": "complete"
},
{
"comment": "Exported to ERP",
"created_at": "2018-07-19 15:19:00",
"entity_id": 244887,
"entity_name": "order",
"is_customer_notified": 0,
"is_visible_on_front": 0,
"parent_id": 57614,
"status": "complete"
}
],
"extension_attributes": {
"shipping_assignments": [
{
"shipping": {
"total": {
"base_shipping_amount": 0,
"base_shipping_discount_amount": 0,
"base_shipping_incl_tax": 0,
"base_shipping_tax_amount": 0,
"shipping_amount": 0,
"shipping_discount_amount": 0,
"shipping_discount_tax_compensation_amount": 0,
"shipping_incl_tax": 0,
"shipping_tax_amount": 0
}
},
"items": [
{
"amount_refunded": 0,
"base_amount_refunded": 0,
"base_discount_amount": 0,
"base_discount_invoiced": 0,
"base_discount_tax_compensation_amount": 0,
"base_original_price": 24,
"base_price": 23.41,
"base_price_incl_tax": 24,
"base_row_invoiced": 0,
"base_row_total": 23.41,
"base_row_total_incl_tax": 24,
"base_tax_amount": 0.59,
"base_tax_invoiced": 0,
"created_at": "2018-07-19 15:19:00",
"discount_amount": 0,
"discount_invoiced": 0,
"discount_percent": 0,
"free_shipping": 0,
"discount_tax_compensation_amount": 0,
"is_qty_decimal": 0,
"is_virtual": 1,
"item_id": 80320,
"name": "Gesellschaft Ausgabe A, Arbeitsheft (eLehrmittel, Neuauflage)",
"no_discount": 0,
"order_id": 57614,
"original_price": 24,
"price": 23.41,
"price_incl_tax": 24,
"product_id": 8654,
"product_type": "virtual",
"qty_canceled": 0,
"qty_invoiced": 0,
"qty_ordered": 1,
"qty_refunded": 0,
"qty_shipped": 0,
"quote_item_id": 135169,
"row_invoiced": 0,
"row_total": 23.41,
"row_total_incl_tax": 24,
"sku": "978-3-0355-1185-7",
"store_id": 1,
"tax_amount": 0.59,
"tax_invoiced": 0,
"tax_percent": 2.5,
"updated_at": "2018-07-19 15:19:00",
"weight": 0.01
}
]
}
]
}
}
],
"search_criteria": {
"filter_groups": [
{ {
"filters": [ "id": 3433,
{ "uri": "\/products\/some-produt",
"field": "customer_id", "url": null,
"value": "49124", "title": "helloclass",
"condition_type": "eq" "subtitle": "Lizenz gültig für 4 Jahre",
} "isbn": "111-2-3333-1397-4",
] "slug": "some-product",
"product_type": "eLehrmittel",
"product_form": "",
"cover": "https:\/\/hep-verlag.fra1.digitaloceanspaces.com\/staging\/products\/2921\/111-2-3333-1397-4.jpg",
"price": 100,
"price_total": 100,
"amount": 1,
"authors": []
} }
] ]
}, },
"total_count": 2 {
} "id": 5,
"status": "paid",
"order_total": 10000,
"created_at": "2021-05-10T20:52:16.000000Z",
"billing_address": {
"id": 20,
"salutation": "male",
"first_name": "Hans",
"last_name": "Meier",
"company": "Lustig GmbH",
"street": "Sulgenrain 12890",
"city": "Bern",
"post_code": "3007",
"country": "Schweiz"
},
"delivery_address": {
"id": 19,
"salutation": "male",
"first_name": "Hans",
"last_name": "Muster",
"company": "Muster AG",
"street": "Ruderweg 24",
"city": "Bern",
"post_code": "3000",
"country": "Schweiz"
},
"entries": [
{
"id": 3433,
"uri": "\/products\/myskillbox-lehrpersonen",
"url": null,
"title": "mySkillbox für Lehrpersonen ",
"subtitle": "Lizenz gültig für 1 Jahr",
"isbn": "978-3-0355-1861-0",
"slug": "myskillbox-lehrpersonen",
"product_type": "eLehrmittel",
"product_form": "",
"cover": "https:\/\/hep-verlag.fra1.digitaloceanspaces.com\/staging\/products\/2921\/978-3-0355-1861-0.jpg",
"price": 100,
"price_total": 100,
"amount": 1,
"authors": []
}
]
}
]

View File

@ -14,13 +14,13 @@ class MockResponse:
## Setup json data ## Setup json data
def make_orders_valid(order_items): def make_orders_valid(orders):
for order_item in order_items['items']: for order in orders:
if 'created_at' in order_item: if 'created_at' in order:
yesterday = datetime.now() - timedelta(1) yesterday = datetime.now() - timedelta(1)
order_item['created_at'] = datetime.strftime(yesterday, '%Y-%m-%d %H:%M:%S') order['created_at'] = datetime.strftime(yesterday, '%Y-%m-%d %H:%M:%S')
return order_items return orders
# Load data # Load data
dir_path = os.path.dirname(os.path.realpath(__file__)) dir_path = os.path.dirname(os.path.realpath(__file__))
@ -40,8 +40,8 @@ with open('{}/mock_data/email_not_confirmed_me.json'.format(dir_path), 'r') as f
ME_DATA = json.loads(me_data) ME_DATA = json.loads(me_data)
NOT_CONFIRMED_ME = json.loads(not_confirmed_email_me_data) NOT_CONFIRMED_ME = json.loads(not_confirmed_email_me_data)
valid_teacher_order_items = json.loads(valid_teacher_order_data) valid_teacher_orders = json.loads(valid_teacher_order_data)
VALID_TEACHERS_ORDERS = make_orders_valid(valid_teacher_order_items) VALID_TEACHERS_ORDERS = make_orders_valid(valid_teacher_orders)
valid_student_order_items = json.loads(valid_student_order_data) valid_student_orders = json.loads(valid_student_order_data)
VALID_STUDENT_ORDERS = make_orders_valid(valid_student_order_items) VALID_STUDENT_ORDERS = make_orders_valid(valid_student_orders)

View File

@ -1,5 +1,3 @@
from django.conf import settings
from django.test import TestCase from django.test import TestCase
from core.factories import UserFactory from core.factories import UserFactory
@ -8,9 +6,6 @@ from users.models import User
TOKEN = 'abcd12345!' TOKEN = 'abcd12345!'
MYSKILLBOX_GROUP_ID_ME_DATA = ME_DATA.copy()
MYSKILLBOX_GROUP_ID_ME_DATA['group_id'] = settings.HEP_MYSKILLBOX_GROUP_ID
class UserManagerTests(TestCase): class UserManagerTests(TestCase):
def setUp(self): def setUp(self):