def test_edit_node(self): self.mock_api_client(200) self.mock_node_edit(200) publisher = MarketingSitePublisher() publish_data = publisher._get_node_data(self.program, self.user_id) # pylint: disable=protected-access publisher._edit_node(self.api_client, self.node_id, publish_data) # pylint: disable=protected-access self.assert_responses_call_count(5)
def test_get_node_id(self): self.mock_api_client(200) self.mock_node_retrieval(self.program.uuid) publisher = MarketingSitePublisher() node_id = publisher._get_node_id(self.api_client, self.program.uuid) # pylint: disable=protected-access self.assert_responses_call_count(5) self.assertEqual(node_id, self.node_id)
def test_create_node(self): self.mock_api_client(200) self.mock_node_create(self.expected_node, 201) publisher = MarketingSitePublisher() publish_data = publisher._get_node_data(self.program, self.user_id) # pylint: disable=protected-access data = publisher._create_node(self.api_client, publish_data) # pylint: disable=protected-access self.assertEqual(data, self.expected_node['id'])
def test_create_node_failed(self): self.mock_api_client(200) self.mock_node_create({}, 500) publisher = MarketingSitePublisher() publish_data = publisher._get_node_data(self.program, self.user_id) # pylint: disable=protected-access with self.assertRaises(ProgramPublisherException): publisher._create_node(self.api_client, publish_data) # pylint: disable=protected-access
def test_publish_delete_program(self): self.mock_api_client(200) self.mock_node_retrieval(self.program.uuid) self.mock_node_delete(204) publisher = MarketingSitePublisher() publisher.delete_program(self.program) self.assert_responses_call_count(6)
def test_publish_program_no_credential(self): self.program.partner.marketing_site_api_password = None self.program.partner.marketing_site_api_username = None publisher = MarketingSitePublisher() with self.assertRaises(ProgramPublisherException): publisher.publish_program(self.program) self.assert_responses_call_count(0)
def test_publish_modified_program(self): self.mock_api_client(200) self.mock_node_retrieval(self.program.uuid) self.mock_node_edit(200) program_before = ProgramFactory() publisher = MarketingSitePublisher(program_before) publisher.publish_program(self.program) self.assert_responses_call_count(6)
def test_create_node(self): self.mock_api_client(200) expected = {'list': [{'nid': self.nid}]} self.mock_node_create(expected, 201) publisher = MarketingSitePublisher() publish_data = publisher._get_node_data(self.program, self.user_id) # pylint: disable=protected-access data = publisher._create_node(self.api_client, publish_data) # pylint: disable=protected-access self.assertEqual(data, expected)
def test_publish_program_create(self): self.mock_api_client(200) expected = {'list': [{'nid': self.nid}]} self.mock_node_retrieval(self.program.uuid, exists=False) self.mock_node_create(expected, 201) publisher = MarketingSitePublisher() publisher.publish_program(self.program) self.assert_responses_call_count(6)
def test_add_alias_failed(self): self.mock_api_client(200) self.mock_node_retrieval(self.program.uuid) self.mock_node_edit(200) publisher = MarketingSitePublisher() self.mock_add_alias(500) with mock.patch.object(MarketingSitePublisher, '_get_form_build_id_and_form_token', return_value={}): with self.assertRaises(ProgramPublisherException): publisher.publish_program(self.program)
def test_publish_program_create(self): self.mock_api_client(200) self.mock_node_retrieval(self.program.uuid, exists=False) self.mock_node_create(self.expected_node, 201) publisher = MarketingSitePublisher() self.mock_add_alias() with mock.patch.object(MarketingSitePublisher, '_get_headers', return_value={}): with mock.patch.object(MarketingSitePublisher, '_get_form_build_id_and_form_token', return_value={}): publisher.publish_program(self.program) self.assert_responses_call_count(8)
def test_get_alias_form(self): self.mock_api_client(200) self.mock_node_retrieval(self.program.uuid) self.mock_node_edit(200) publisher = MarketingSitePublisher() self.mock_add_alias() self.mock_get_alias_form() with mock.patch.object(MarketingSitePublisher, '_get_headers', return_value={}): publisher.publish_program(self.program) self.assert_responses_call_count(9)
def test_get_delete_form_failed(self): self.mock_api_client(200) self.mock_node_retrieval(self.program.uuid) self.mock_node_edit(200) program_before = ProgramFactory() publisher = MarketingSitePublisher(program_before) self.mock_add_alias() self.mock_get_delete_form(program_before.marketing_slug, 500) with mock.patch.object(MarketingSitePublisher, '_get_headers', return_value={}): with mock.patch.object(MarketingSitePublisher, '_get_form_build_id_and_form_token', return_value={}): with self.assertRaises(ProgramPublisherException): publisher.publish_program(self.program)
def test_publish_modified_program(self): self.mock_api_client(200) self.mock_node_retrieval(self.program.uuid) self.mock_node_edit(200) program_before = ProgramFactory() publisher = MarketingSitePublisher(program_before) self.mock_add_alias() self.mock_delete_alias() with mock.patch.object(MarketingSitePublisher, '_get_headers', return_value={}): with mock.patch.object(MarketingSitePublisher, '_get_form_build_id_and_form_token', return_value={}): with mock.patch.object(MarketingSitePublisher, '_get_delete_alias_url', return_value='/foo'): publisher.publish_program(self.program) self.assert_responses_call_count(9)
def test_get_node_data(self): publisher = MarketingSitePublisher() publish_data = publisher._get_node_data(self.program, self.user_id) # pylint: disable=protected-access expected = { 'type': str(self.program.type).lower(), 'title': self.program.title, 'field_uuid': str(self.program.uuid), 'uuid': str(self.program.uuid), 'author': { 'id': self.user_id, }, 'status': 1 if self.program.status == ProgramStatus.Active else 0 } self.assertDictEqual(publish_data, expected)
def save(self, *args, **kwargs): if waffle.switch_is_active('publish_program_to_marketing_site') and self.partner.has_marketing_site: # Before save, get from database the existing data if exists existing_program = None if self.id: existing_program = Program.objects.get(id=self.id) # Pass existing data to the publisher so it can decide whether we should publish publisher = MarketingSitePublisher(existing_program) with transaction.atomic(): super(Program, self).save(*args, **kwargs) # Once save complete, we need to update the marketing site # So the marketing page for this program is automatically updated publisher.publish_program(self) else: super(Program, self).save(*args, **kwargs) self.reindex_courses()
def delete_program(sender, instance, **kwargs): # pylint: disable=unused-argument if waffle.switch_is_active('publish_program_to_marketing_site') and \ instance.partner.has_marketing_site: publisher = MarketingSitePublisher() publisher.delete_program(instance)
def test_get_non_existent_node_id(self): self.mock_api_client(200) self.mock_node_retrieval(self.program.uuid, exists=False) publisher = MarketingSitePublisher() node_id = publisher._get_node_id(self.api_client, self.program.uuid) # pylint: disable=protected-access self.assertIsNone(node_id)
def test_publish_delete_xseries(self): self.program = ProgramFactory(type=ProgramType.objects.get(name='XSeries')) publisher = MarketingSitePublisher() publisher.delete_program(self.program) self.assert_responses_call_count(0)
def test_publish_delete_non_existent_program(self): self.mock_api_client(200) self.mock_node_retrieval(self.program.uuid, exists=False) publisher = MarketingSitePublisher() publisher.delete_program(self.program) self.assert_responses_call_count(5)
def test_publish_xseries_program(self): self.program.type = ProgramType.objects.get(name='XSeries') publisher = MarketingSitePublisher() publisher.publish_program(self.program) self.assert_responses_call_count(0)
def test_publish_unmodified_program(self): self.mock_api_client(200) publisher = MarketingSitePublisher(self.program) publisher.publish_program(self.program) self.assert_responses_call_count(0)