<s> import json <EOL> from rest_framework import status <EOL> from rest_framework . reverse import reverse <EOL> from rest_framework . test import APITestCase <EOL> from silver . models import ProductCode <EOL> from silver . tests . factories import ( AdminUserFactory , ProviderFactory , <EOL> PlanFactory , ProductCodeFactory ) <EOL> class TestPlanEndpoint ( APITestCase ) : <EOL> def setUp ( self ) : <EOL> admin_user = AdminUserFactory . create ( ) <EOL> self . client . force_authenticate ( user = admin_user ) <EOL> def test_create_plan ( self ) : <EOL> url = reverse ( '<STR_LIT>' ) <EOL> ProductCodeFactory . create_batch ( <NUM_LIT:2> ) <EOL> feature1_pc = ProductCode . objects . get ( id = <NUM_LIT:1> ) . value <EOL> plan_pc = ProductCode . objects . get ( id = <NUM_LIT:2> ) . value <EOL> provider = ProviderFactory . create ( ) <EOL> provider_url = reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : provider . pk } ) <EOL> response = self . client . post ( url , json . dumps ( { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT:15> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT>" : plan_pc , <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : feature1_pc } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : "<STR_LIT>" } <EOL> ] , <EOL> '<STR_LIT>' : provider_url <EOL> } ) , content_type = '<STR_LIT:application/json>' ) <EOL> assert response . status_code == status . HTTP_201_CREATED <EOL> def test_create_plan_without_required_fields ( self ) : <EOL> url = reverse ( '<STR_LIT>' ) <EOL> response = self . client . post ( url , json . dumps ( { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT:15> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : False , <EOL> '<STR_LIT>' : [ ] <EOL> } ) , content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . status_code , status . HTTP_400_BAD_REQUEST ) <EOL> def test_patch_plan ( self ) : <EOL> plan = PlanFactory . create ( ) <EOL> url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : plan . pk } ) <EOL> response = self . client . patch ( url , json . dumps ( { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT> <EOL> } ) , content_type = '<STR_LIT:application/json>' ) <EOL> self . assertEqual ( response . data [ '<STR_LIT:name>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( response . data [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> def test_patch_plan_non_editable_field ( self ) : <EOL> plan = PlanFactory . create ( ) <EOL> url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : plan . pk } ) <EOL> response = self . client . patch ( url , json . dumps ( { <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } ) , content_type = '<STR_LIT:application/json>' ) <EOL> self . assertNotEqual ( response . data [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> def test_put_plan ( self ) : <EOL> plan = PlanFactory . create ( ) <EOL> url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : plan . pk } ) <EOL> response = self . client . put ( url ) <EOL> self . assertEqual ( response . status_code , <EOL> status . HTTP_405_METHOD_NOT_ALLOWED ) <EOL> self . assertEqual ( response . data , <EOL> { u'<STR_LIT>' : u'<STR_LIT>' } ) <EOL> def test_get_plan_list ( self ) : <EOL> PlanFactory . create_batch ( <NUM_LIT> ) <EOL> url = reverse ( '<STR_LIT>' ) <EOL> response = self . client . get ( url ) <EOL> full_url = None <EOL> for field in response . data : <EOL> full_url = field . get ( '<STR_LIT:url>' , None ) <EOL> if full_url : <EOL> break <EOL> if full_url : <EOL> domain = full_url . split ( '<STR_LIT:/>' ) [ <NUM_LIT:2> ] <EOL> full_url = full_url . split ( domain ) [ <NUM_LIT:0> ] + domain + url <EOL> assert response . status_code == status . HTTP_200_OK <EOL> assert response . _headers [ '<STR_LIT>' ] == ( '<STR_LIT>' , '<STR_LIT:<>' + full_url + '<STR_LIT>' + <EOL> '<STR_LIT:<>' + full_url + '<STR_LIT>' + <EOL> '<STR_LIT:<>' + full_url + '<STR_LIT>' ) <EOL> response = self . client . get ( url + '<STR_LIT>' ) <EOL> assert response . status_code == status . HTTP_200_OK <EOL> assert response . _headers [ '<STR_LIT>' ] == ( '<STR_LIT>' , '<STR_LIT:<>' + full_url + '<STR_LIT>' + <EOL> '<STR_LIT:<>' + full_url + '<STR_LIT>' + <EOL> '<STR_LIT:<>' + full_url + '<STR_LIT>' ) <EOL> def test_get_plan_detail ( self ) : <EOL> plan = PlanFactory . create ( ) <EOL> url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : plan . pk } ) <EOL> response = self . client . get ( url ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> self . assertNotEqual ( response . data , [ ] ) <EOL> def test_get_plan_detail_unexisting ( self ) : <EOL> url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> response = self . client . get ( url ) <EOL> self . assertEqual ( response . status_code , status . HTTP_404_NOT_FOUND ) <EOL> def test_delete_plan ( self ) : <EOL> plan = PlanFactory . create ( ) <EOL> plan . enabled = True <EOL> plan . save ( ) <EOL> url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> response = self . client . delete ( url ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> self . assertEqual ( response . data , { "<STR_LIT>" : True } ) <EOL> def test_delete_plan_unexisting ( self ) : <EOL> url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> response = self . client . delete ( url ) <EOL> self . assertEqual ( response . status_code , status . HTTP_404_NOT_FOUND ) </s>
