def test_create_study_plan_for_user(self): token = get_token_from_user(user=self.user) self.client.credentials(HTTP_AUTHORIZATION='Token ' + token.key) study_plan = StudyPlanFactory.build(name='AWS Certification') lesson_1 = LessonFactory.build() lesson_2 = LessonFactory.build() data = json.dumps({ 'name': study_plan.name, 'lessons': [{ 'topic': lesson_1.topic, }, { 'topic': lesson_2.topic, }] }) response = self.client.post(reverse('study-plan-list', kwargs={'version': 'v1'}), data=data, content_type='application/json') study_plan_created = StudyPlan.objects.get(name=study_plan.name) self.assertEqual(response.status_code, status.HTTP_201_CREATED) study_plan_data = json.loads(response.content) self.assertEqual(study_plan_data['name'], study_plan_created.name) self.assertEqual(study_plan_data['lessons'][0]['topic'], study_plan_created.lessons.all()[0].topic) self.assertEqual(study_plan_data['lessons'][1]['topic'], study_plan_created.lessons.all()[1].topic)
def test_create_order(self): token = get_token_from_user(user=self.user) self.client.credentials(HTTP_AUTHORIZATION='Token ' + token.key) response = self.client.post(reverse('order-list', kwargs={'version': 'v1'}), content_type='application/json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) order = Order.objects.get(user=self.user) self.assertEqual(order.order_status, 'Pending')
def test_list_orders_from_user(self): OrderFactory(user=self.user) token = get_token_from_user(user=self.user) self.client.credentials(HTTP_AUTHORIZATION='Token ' + token.key) response = self.client.get( reverse('order-list', kwargs={'version': 'v1'})) self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_list_study_plan_from_user(self): token = get_token_from_user(user=self.user) self.client.credentials(HTTP_AUTHORIZATION='Token ' + token.key) response = self.client.get( reverse('study-plan-list', kwargs={'version': 'v1'})) self.assertEqual(response.status_code, status.HTTP_200_OK) study_plan_data = json.loads(response.content)[0] self.assertEqual(study_plan_data['id'], self.study_plan.id) self.assertEqual(study_plan_data['lessons'][0]['topic'], self.study_plan.lessons.all()[0].topic)
def test_add_lesson_to_user_study_plan(self): lesson = LessonFactory() data = json.dumps({'topic': lesson.topic}) token = get_token_from_user(user=self.user) self.client.credentials(HTTP_AUTHORIZATION='Token ' + token.key) response = self.client.post(reverse('lesson-list', kwargs={'version': 'v1'}), data=data, content_type='application/json') self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_list_lessons_given_a_study_plan(self): token = get_token_from_user(user=self.user) self.client.credentials(HTTP_AUTHORIZATION='Token ' + token.key) response = self.client.get( reverse('lesson-detail', kwargs={ 'version': 'v1', 'pk': self.study_plan.id })) self.assertEqual(response.status_code, status.HTTP_200_OK) response_data = json.loads(response.content) self.assertEqual(response_data['topic'], self.lesson.topic)
def test_study_plan_aggregated_data_feature_for_premium_accounts(self): token = get_token_from_user(user=self.user) self.client.credentials(HTTP_AUTHORIZATION='Token ' + token.key) response = self.client.get( reverse('study-plan-get-lesson-aggregated-data', kwargs={ 'version': 'v1', 'pk': self.study_plan.id }), ) self.assertEqual(response.status_code, status.HTTP_200_OK) study_plan_data = json.loads(response.content) self.assertEqual(study_plan_data['total_lessons_count'], 1)
def test_get_order_detail(self): order = OrderFactory(user=self.user) token = get_token_from_user(user=self.user) self.client.credentials(HTTP_AUTHORIZATION='Token ' + token.key) response = self.client.get( reverse('order-get-order-detail', kwargs={ 'version': 'v1', 'pk': order.id }), ) self.assertEqual(response.status_code, status.HTTP_200_OK) order_data = json.loads(response.content) self.assertEqual(order_data['order_status'], order.order_status)
def test_it_returns_404_for_a_user_trying_to_access_a_order_from_another_user( self): new_user = UserFactory() create_token(user=new_user) order = OrderFactory(user=self.user) token = get_token_from_user(user=new_user) self.client.credentials(HTTP_AUTHORIZATION='Token ' + token.key) response = self.client.get( reverse('order-get-order-detail', kwargs={ 'version': 'v1', 'pk': order.id }), ) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_it_returns_403_for_free_user_when_access_study_plan_aggregated_data_feature_for_premium_accounts( self): user = UserFactory(first_name='Spike Lee') user_membership = UserMembershipFactory(user=user) lesson = LessonFactory() study_plan = StudyPlanFactory(user_membership=user_membership, lessons=[lesson]) create_token(user=user) token = get_token_from_user(user=user) self.client.credentials(HTTP_AUTHORIZATION='Token ' + token.key) response = self.client.get( reverse('study-plan-get-lesson-aggregated-data', kwargs={ 'version': 'v1', 'pk': study_plan.id }), ) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)