# -*- coding: utf-8 -*- # # ITerativ GmbH # http://www.iterativ.ch/ # # Copyright (c) 2020 ITerativ GmbH. All rights reserved. # # Created on 23.01.20 # @author: chrigu import json from datetime import datetime, timedelta from django.test import TestCase from core.hep_client import HepClient, TEACHER_EDITION_DURATION class HepClientTestCases(TestCase): def setUp(self): self.hep_client = HepClient() self.now = datetime.now() def test_has_no_valid_product(self): products = [ { 'edition': 'teacher', 'raw': {}, 'activated': self.now - timedelta(2*TEACHER_EDITION_DURATION), 'status': 'complete' }, { 'edition': 'teacher', 'raw': {}, 'activated': self.now - timedelta(3 * TEACHER_EDITION_DURATION), 'status': 'complete' }, { 'edition': 'teacher', 'raw': {}, 'activated': self.now - timedelta(4 * TEACHER_EDITION_DURATION), 'status': 'complete' } ] relevant_product = self.hep_client._get_relevant_product(products) self.assertIsNone(relevant_product) def test_has_no_not_completed_product(self): products = [ { 'edition': 'teacher', 'raw': {}, 'activated': self.now - timedelta(7), 'status': 'not' } ] relevant_product = self.hep_client._get_relevant_product(products) self.assertIsNone(relevant_product) def test_has_valid_product(self): products = [ { 'edition': 'teacher', 'raw': { 'id': 0 }, 'activated': self.now - timedelta(7), 'status': 'complete' }, { 'edition': 'teacher', 'raw': { 'id': 1 }, 'activated': self.now - timedelta(3 * TEACHER_EDITION_DURATION), 'status': 'complete' }, { 'edition': 'teacher', 'raw': { 'id': 2 }, 'activated': self.now - timedelta(4 * TEACHER_EDITION_DURATION), 'status': 'complete' } ] relevant_product = self.hep_client._get_relevant_product(products) self.assertEqual(relevant_product['raw']['id'], 0) def test_has_multiple_valid_teacher_products_but_only_one_active(self): products = [ { 'edition': 'teacher', 'raw': { 'id': 0 }, 'activated': self.now - timedelta(7), 'status': 'complete' }, { 'edition': 'teacher', 'raw': { 'id': 1 }, 'activated': self.now - timedelta(3 * TEACHER_EDITION_DURATION), 'status': 'complete' }, { 'edition': 'teacher', 'raw': { 'id': 2 }, 'activated': self.now - timedelta(4 * TEACHER_EDITION_DURATION), 'status': 'complete' } ] relevant_product = self.hep_client._get_relevant_product(products) self.assertEqual(relevant_product['raw']['id'], 0) def test_has_valid_student_and_teacher_edition(self): products = [ { 'edition': 'student', 'raw': { 'id': 0 }, 'activated': self.now - timedelta(7), 'status': 'complete' }, { 'edition': 'teacher', 'raw': { 'id': 1 }, 'activated': self.now - timedelta(7), 'status': 'complete' } ] relevant_product = self.hep_client._select_from_teacher_products(products) self.assertEqual(relevant_product['raw']['id'], 1) def test_product_is_active(self): expiry_date = self.now + timedelta(3) is_active = HepClient.is_product_active(expiry_date, 'teacher') self.assertTrue(is_active) def test_product_is_not_active(self): expiry_date = self.now - timedelta(3 * TEACHER_EDITION_DURATION) is_active = HepClient.is_product_active(expiry_date, 'teacher') self.assertFalse(is_active)