Test user with no login

This commit is contained in:
Christian Cueni 2020-01-28 16:36:06 +01:00
parent 9c4e2de296
commit e98d2774fd
6 changed files with 602 additions and 33 deletions

View File

@ -26,6 +26,10 @@ class HepClientException(Exception):
pass
class HepClientUnauthorizedException(Exception):
pass
class HepClient:
URL = 'https://stage.hep-verlag.ch'
# URL = 'https://www.hep-verlag.ch'
@ -53,7 +57,9 @@ class HepClient:
response = requests.get(request_url, headers=headers)
# Todo handle 401 and most important network errors
if response.status_code != 200:
if response.status_code == 401:
raise HepClientUnauthorizedException(response.status_code, response.json())
elif response.status_code != 200:
raise HepClientException(response.status_code, response.json())
logger.info(response.json())

View File

@ -13,9 +13,8 @@ from django.conf import settings
from django.contrib.auth import authenticate, login
from graphene import relay
from core.hep_client import HepClient
from core.hep_client import HepClient, HepClientUnauthorizedException
from registration.models import License
from users.managers import UserRoleManager
from users.models import MagentoToken, User, Role, UserRole, SchoolClass
@ -37,18 +36,6 @@ class Login(relay.ClientIDMutation):
username = kwargs.get('username_input')
password = kwargs.get('password_input')
# get token
# wrong password
#
# is email verified
# -> show screen
# local license
# -> show coupon
# get hep orders
# -> show coupon
# save role information
# login
if settings.USE_LOCAL_REGISTRATION:
user = authenticate(username=username, password=password)
if user is None:
@ -58,13 +45,16 @@ class Login(relay.ClientIDMutation):
else:
hep_client = HepClient()
# Todo network error catch error
token = hep_client.customer_token(username, password)
try:
token = hep_client.customer_token(username, password)
except HepClientUnauthorizedException:
error = LoginError(field='invalid_credentials')
return cls(success=False, errors=[error])
try:
user = User.objects.get(email=username)
except User.DoesNotExist:
user = User.objects.create_user_from_hep(token)
# ISBN "123-4-5678-9012-3"
magento_token, created = MagentoToken.objects.get_or_create(user=user)
magento_token.token = token['token']
@ -82,12 +72,14 @@ class Login(relay.ClientIDMutation):
if product:
license = License.objects.create_license_for_role(user, product['activated'],
product['raw'], product['edition'])
# todo handle no license case
else:
# todo go to shop
pass
UserRole.objects.create_role_for_user(user, license.for_role.key)
if license.for_role.key == Role.objects.TEACHER_KEY:
UserRole.objects.create_role_for_user(user, license.for_role.key)
default_class_name = SchoolClass.generate_default_group_name()
default_class = SchoolClass.objects.create(name=default_class_name)
user.school_classes.add(default_class)

View File

@ -0,0 +1,526 @@
{
"items": [
{
"base_currency_code": "CHF",
"base_discount_amount": 0,
"base_grand_total": 46,
"base_discount_tax_compensation_amount": 0,
"base_shipping_amount": 0,
"base_shipping_discount_amount": 0,
"base_shipping_incl_tax": 0,
"base_shipping_tax_amount": 0,
"base_subtotal": 44.88,
"base_subtotal_incl_tax": 46,
"base_tax_amount": 1.12,
"base_total_due": 46,
"base_to_global_rate": 1,
"base_to_order_rate": 1,
"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": "123-4-5678-9012-3",
"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
}
]
}
]
}
},
{
"base_currency_code": "CHF",
"base_discount_amount": 0,
"base_grand_total": 24,
"base_discount_tax_compensation_amount": 0,
"base_shipping_amount": 0,
"base_shipping_discount_amount": 0,
"base_shipping_incl_tax": 0,
"base_shipping_tax_amount": 0,
"base_subtotal": 23.41,
"base_subtotal_incl_tax": 24,
"base_tax_amount": 0.59,
"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": [
{
"field": "customer_id",
"value": "49124",
"condition_type": "eq"
}
]
}
]
},
"total_count": 2
}

View File

@ -11,6 +11,8 @@ import json
import os
from datetime import timedelta, datetime
from unittest.mock import patch
import requests
from django.contrib.sessions.middleware import SessionMiddleware
from django.test import TestCase, RequestFactory
from django.utils import timezone
@ -27,26 +29,44 @@ FAKE_TOKEN = 'abcd12345!'
## Setup json data
def make_orders_valid(order_items):
for order_item in order_items['items']:
for status in order_item['status_histories']:
if status['comment'] == 'payed by couponcode':
yesterday = datetime.now() - timedelta(1)
status['created_at'] = datetime.strftime(yesterday, '%Y-%m-%d %H:%M:%S')
return order_items
# Load data
dir_path = os.path.dirname(os.path.realpath(__file__))
with open('{}/valid_teacher_orders.json'.format(dir_path), 'r') as file:
order_data = file.read()
with open('{}/test_data/valid_teacher_orders.json'.format(dir_path), 'r') as file:
valid_teacher_order_data = file.read()
with open('{}/me_data.json'.format(dir_path), 'r') as file:
with open('{}/test_data/valid_student_orders.json'.format(dir_path), 'r') as file:
valid_student_order_data = file.read()
with open('{}/test_data/me_data.json'.format(dir_path), 'r') as file:
me_data = file.read()
ME_DATA = json.loads(me_data)
order_items = json.loads(order_data)
valid_teacher_order_items = json.loads(valid_teacher_order_data)
VALID_TEACHERS_ORDERS = make_orders_valid(valid_teacher_order_items)
for order_item in order_items['items']:
for status in order_item['status_histories']:
if status['comment'] == 'payed by couponcode':
yesterday = datetime.now() - timedelta(1)
status['created_at'] = datetime.strftime(yesterday, '%Y-%m-%d %H:%M:%S')
valid_student_order_items = json.loads(valid_student_order_data)
VALID_STUDENT_ORDERS = make_orders_valid(valid_student_order_items)
ORDERS = order_items
## Mocks
class MockResponse:
def __init__(self, status_code):
self.status_code = status_code
def json(self):
return {}
class PasswordResetTests(TestCase):
@ -98,9 +118,9 @@ class PasswordResetTests(TestCase):
self.assertTrue(self.user.is_authenticated)
@patch.object(HepClient, 'customer_token', return_value={'token': FAKE_TOKEN})
@patch.object(HepClient, '_customer_orders', return_value=ORDERS)
@patch.object(HepClient, '_customer_orders', return_value=VALID_TEACHERS_ORDERS)
@patch.object(HepClient, 'customer_me', return_value=ME_DATA)
def test_user_can_login_with_local_user_and_remote_license(self, order_mock, token_mock, me_token):
def test_teacher_can_login_with_local_user_and_remote_license(self, order_mock, token_mock, me_token):
result = self.make_login_mutation(ME_DATA['email'], 'test123')
user = User.objects.get(email=ME_DATA['email'])
@ -119,10 +139,35 @@ class PasswordResetTests(TestCase):
self.assertTrue(result.get('data').get('login').get('success'))
self.assertTrue(self.user.is_authenticated)
@patch.object(HepClient, 'customer_token', return_value={'token': FAKE_TOKEN})
@patch.object(HepClient, '_customer_orders', return_value=VALID_STUDENT_ORDERS)
@patch.object(HepClient, 'customer_me', return_value=ME_DATA)
def test_student_can_login_with_local_user_and_remote_license(self, order_mock, token_mock, me_token):
result = self.make_login_mutation(ME_DATA['email'], 'test123')
user = User.objects.get(email=ME_DATA['email'])
token = MagentoToken.objects.get(user=user)
self.assertEqual(token.token, FAKE_TOKEN)
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.assertTrue(result.get('data').get('login').get('success'))
self.assertTrue(self.user.is_authenticated)
@patch.object(requests, 'post', return_value=MockResponse(401))
def test_user_with_no_login_cannot_login(self, post_fn):
result = self.make_login_mutation(ME_DATA['email'], 'test123')
self.assertFalse(result.get('data').get('login').get('success'))
## can login with license and user
## can login with no user and license
# can login with no user and local license
# cannot login without user
# ?can login with no user and local license
## cannot login without user
# cannot login with user and not verfied
# cannot login with user and no license
# cannot login with user and expired license