Store admin token in db
This commit is contained in:
parent
cd7f79072e
commit
6beb4296f7
|
|
@ -65,6 +65,11 @@ class HepClient:
|
|||
logger.info(response.json())
|
||||
return response
|
||||
|
||||
def fetch_admin_token(self, admin_user, password):
|
||||
response = self._call('/rest/deutsch/V1/integration/admin/token', 'post',
|
||||
data={'username': admin_user, 'password': password})
|
||||
return response.json()['token']
|
||||
|
||||
def is_email_available(self, email):
|
||||
response = self._call('/rest/deutsch/V1/customers/isEmailAvailable', method='post',
|
||||
data={'customerEmail': email, 'websiteId': self.WEBSITE_ID})
|
||||
|
|
|
|||
|
|
@ -0,0 +1,39 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# ITerativ GmbH
|
||||
# http://www.iterativ.ch/
|
||||
#
|
||||
# Copyright (c) 2020 ITerativ GmbH. All rights reserved.
|
||||
#
|
||||
# Created on 03.02.20
|
||||
# @author: chrigu <christian.cueni@iterativ.ch>
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.db import models
|
||||
|
||||
from core.hep_client import HepClient
|
||||
|
||||
DEFAULT_PK = 1
|
||||
|
||||
|
||||
class AdminDataManager(models.Manager):
|
||||
hep_client = HepClient()
|
||||
|
||||
def update_admin_token(self):
|
||||
|
||||
admin_token = self.hep_client.fetch_admin_token(settings.HEP_ADMIN_USER, settings.HEP_ADMIN_PASSWORD)
|
||||
|
||||
admin_data, created = self.get_or_create(pk=DEFAULT_PK)
|
||||
admin_data.hep_admin_token = admin_token
|
||||
admin_data.save()
|
||||
return admin_data.hep_admin_token
|
||||
|
||||
def get_admin_token(self):
|
||||
|
||||
try:
|
||||
admin_token = self.get(pk=DEFAULT_PK)
|
||||
except self.model.DoesNotExist:
|
||||
admin_token = self.update_admin_token()
|
||||
|
||||
return admin_token
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# ITerativ GmbH
|
||||
# http://www.iterativ.ch/
|
||||
#
|
||||
# Copyright (c) 2020 ITerativ GmbH. All rights reserved.
|
||||
#
|
||||
# Created on 03.02.20
|
||||
# @author: chrigu <christian.cueni@iterativ.ch>
|
||||
from datetime import datetime
|
||||
|
||||
from django.db import models
|
||||
|
||||
from core.managers import AdminDataManager
|
||||
|
||||
|
||||
class AdminData(models.Model):
|
||||
hep_admin_token = models.CharField(max_length=100, blank=False, null=False)
|
||||
updated_at = models.DateTimeField(blank=False, null=True, auto_now=True)
|
||||
|
||||
objects = AdminDataManager()
|
||||
|
|
@ -372,6 +372,7 @@ TASKBASE_BASEURL = os.environ.get("TASKBASE_BASEURL")
|
|||
USE_LOCAL_REGISTRATION = False
|
||||
|
||||
# HEP
|
||||
HEP_ADMIN_TOKEN = "asdf"
|
||||
HEP_ADMIN_USER = "adminuser"
|
||||
HEP_ADMIN_PASSWORD = "password"
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -122,7 +122,8 @@ class PasswordResetTests(TestCase):
|
|||
|
||||
@patch.object(HepClient, '_customer_orders', return_value=VALID_TEACHERS_ORDERS)
|
||||
@patch.object(HepClient, 'customer_me', return_value=ME_DATA)
|
||||
def test_teacher_can_login_with_remote_user_and_remote_license(self, order_mock, me_token):
|
||||
@patch.object(HepClient, 'fetch_admin_token', return_value={'token': 'AABBCCDDEE**44566'})
|
||||
def test_teacher_can_login_with_remote_user_and_remote_license(self, order_mock, me_mock, admin_token_mock):
|
||||
result = self.make_login_mutation(ME_DATA['email'], TOKEN)
|
||||
|
||||
user = User.objects.get(email=ME_DATA['email'])
|
||||
|
|
@ -141,7 +142,8 @@ class PasswordResetTests(TestCase):
|
|||
|
||||
@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_remote_user_and_remote_license(self, order_mock, me_token):
|
||||
@patch.object(HepClient, 'fetch_admin_token', return_value={'token':'AABBCCDDEE**44566'})
|
||||
def test_student_can_login_with_remote_user_and_remote_license(self, order_mock, me_mock, admin_token_mock):
|
||||
|
||||
result = self.make_login_mutation(ME_DATA['email'], TOKEN)
|
||||
user = User.objects.get(email=ME_DATA['email'])
|
||||
|
|
@ -174,7 +176,8 @@ class PasswordResetTests(TestCase):
|
|||
|
||||
@patch.object(HepClient, 'myskillbox_product_for_customer', return_value=None)
|
||||
@patch.object(HepClient, 'customer_me', return_value=ME_DATA)
|
||||
def test_user_cannot_login_without_license(self, me_mock, product_mock):
|
||||
@patch.object(HepClient, 'fetch_admin_token', return_value={'token': 'AABBCCDDEE**44566'})
|
||||
def test_user_cannot_login_without_license(self, me_mock, product_mock, admin_token_mock):
|
||||
result = self.make_login_mutation(self.user.email, TOKEN)
|
||||
|
||||
self.assertFalse(result.get('data').get('login').get('success'))
|
||||
|
|
@ -182,7 +185,8 @@ class PasswordResetTests(TestCase):
|
|||
|
||||
@patch.object(HepClient, 'myskillbox_product_for_customer', return_value=None)
|
||||
@patch.object(HepClient, 'customer_me', return_value=ME_DATA)
|
||||
def test_user_cannot_login_local_license_invalid(self, product_mock, me_mock):
|
||||
@patch.object(HepClient, 'fetch_admin_token', return_value={'token': 'AABBCCDDEE**44566'})
|
||||
def test_user_cannot_login_local_license_invalid(self, product_mock, me_mock, admin_token_mock):
|
||||
now = timezone.now()
|
||||
expiry_date = now - timedelta(1)
|
||||
LicenseFactory(expire_date=expiry_date, licensee=self.user, for_role=self.teacher_role).save()
|
||||
|
|
|
|||
|
|
@ -10,8 +10,9 @@
|
|||
from django.conf import settings
|
||||
|
||||
from core.hep_client import HepClient, HepClientException
|
||||
from core.models import AdminData
|
||||
from registration.models import License
|
||||
from users.models import User, MagentoToken, UserRole, Role, SchoolClass
|
||||
from users.models import User, UserRole, Role, SchoolClass
|
||||
|
||||
|
||||
def handle_user_and_verify_products(user_data):
|
||||
|
|
@ -36,8 +37,8 @@ def handle_user_and_verify_products(user_data):
|
|||
# Todo how handle invalid license? Cron Job? How to select correct license? Save all licenses? History?
|
||||
except License.DoesNotExist:
|
||||
try:
|
||||
# todo is admin token valid, save it? do we need it?
|
||||
product = hep_client.myskillbox_product_for_customer(settings.HEP_ADMIN_TOKEN, user.hep_id)
|
||||
admin_token = AdminData.objects.get_admin_token()
|
||||
product = hep_client.myskillbox_product_for_customer(admin_token, user.hep_id)
|
||||
except HepClientException:
|
||||
return user, 'unknown_error'
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue