def test_cancel_draft_service_change_comments_on_jira_issue(self, mock_JIRA): # If we cancel a pending change to a current service, it adds a comment # to the existing jira issue. # make the service we're canceling look like a change to an existing service existing_service = ServiceFactory(status=Service.STATUS_CURRENT) draft_service = ServiceFactory( update_of=existing_service, status=Service.STATUS_DRAFT, ) # Pretend we've created a JIRA issue when the draft was started. issue_key = 'XXX-123' draft_service.jira_records.update(jira_issue_key=issue_key) # Now cancel the draft draft_service.cancel() # We should get a new jira update record created cancel_record = \ draft_service.jira_records.get(update_type=JiraUpdateRecord.CANCEL_DRAFT_SERVICE) # run it: cancel_record.do_jira_work() call_args, call_kwargs = mock_JIRA.return_value.add_comment.call_args # Only checking summary as that has the essentially different value # from what the "new service" case sets. self.assertEqual(issue_key, call_args[0]) self.assertIn('change was canceled', call_args[1]) record = JiraUpdateRecord.objects.get(pk=cancel_record.pk) self.assertEqual(issue_key, record.jira_issue_key) # We should NOT have created a new JIRA record self.assertFalse(mock_JIRA.return_value.create_issue.called)
def setUp(self): self.service_ar = ServiceFactory(name_ar='Arabic', name_en='', name_fr='', description_ar='language-of-Egypt', status=Service.STATUS_CURRENT) self.service_ar.save() self.service_en = ServiceFactory( name_en='English', name_ar='', name_fr='', description_en='language-of-Australia', status=Service.STATUS_CURRENT) self.service_en.save() self.service_fr = ServiceFactory(name_fr='French', name_ar='', name_en='', description_fr='language-of-France', status=Service.STATUS_CURRENT) self.service_fr.save() self.rejected_service_fr = ServiceFactory( name_fr='InactiveParis', name_ar='', name_en='', status=Service.STATUS_REJECTED) self.rejected_service_fr.save()
def test_non_staff_sees_all_current_data(self): provider = ProviderFactory() service1 = ServiceFactory(provider=provider, status=Service.STATUS_CURRENT) service2 = ServiceFactory(provider=provider, status=Service.STATUS_CURRENT) ServiceFactory(status=Service.STATUS_CURRENT) ServiceFactory(provider=provider, status=Service.STATUS_DRAFT) # Draft - not included book = get_export_workbook_for_user(provider.user) xlrd_book = save_and_read_book(book) # First sheet - providers sheet = xlrd_book.get_sheet(0) self.assertEqual(2, sheet.nrows - 1) # first provider values = sheet.row_values(1) data = dict(zip(PROVIDER_HEADINGS, values)) self.assertEqual(provider.id, data['id']) # Second sheet - services sheet = xlrd_book.get_sheet(1) self.assertEqual(3, sheet.nrows - 1) values = sheet.row_values(1) data = dict(zip(SERVICE_HEADINGS, values)) self.assertEqual(service1.id, data['id']) values = sheet.row_values(2) data = dict(zip(SERVICE_HEADINGS, values)) self.assertEqual(service2.id, data['id'])
def test_filter_with_parents(self): # given romania = GeographicRegion.objects.get(slug='romania') self.assertIsNotNone(romania) bucharest = GeographicRegion.objects.get(slug='bucharest') self.assertIsNotNone(bucharest) ServiceFactory(region=romania, status='current') ServiceFactory(region=bucharest, status='current') self.assertEqual(Service.objects.all().count(), 2) # when services_romania = self.api_client.get( '/v2/services/search/?filter=with-parents&geographic_region=' + romania.slug + '&page=1&page_size=100') self.assertEqual(services_romania.status_code, 200) services_bucharest = self.api_client.get( '/v2/services/search/?filter=with-parents&geographic_region=' + bucharest.slug + '&page=1&page_size=100') self.assertEqual(services_bucharest.status_code, 200) # then services_romania_content = json.loads(services_romania.content.decode('utf-8'))['results'] self.assertEqual(len(services_romania_content), 1) services_bucharest_content = json.loads(services_bucharest.content.decode('utf-8'))['results'] self.assertEqual(len(services_bucharest_content), 2)
def test_set_latitude(self): service = ServiceFactory(location=None) service.latitude = -66.2 self.assertEqual(Point(0, -66.2), service.location) service = ServiceFactory(location='POINT( 1.0 2.0)') service.latitude = -66.2 self.assertEqual(Point(1.0, -66.2), service.location)
def test_get_num_by_provider_type(self): a_type = ProviderType.objects.first() b_type = ProviderType.objects.last() a_service_area = ServiceArea.objects.first() b_service_area = ServiceArea.objects.first() # 1 service: area a, type a ServiceFactory(area_of_service=a_service_area, provider__type=a_type) # 2 services: area b, type b ServiceFactory(area_of_service=b_service_area, provider__type=b_type) ServiceFactory(area_of_service=b_service_area, provider__type=b_type) url = reverse('report-num-services-by-provider-type') rsp = self.get_with_token(url) self.assertEqual(OK, rsp.status_code) result = json.loads(rsp.content.decode('utf-8')) self.assertEqual(len(result), ProviderType.objects.all().count()) for r in result: if r['number'] == a_type.number: for t in r['totals']: if t['label_en'] == a_service_area.name_en: self.assertEqual(1, t['total']) else: self.assertEqual(0, t['total']) elif r['number'] == b_type.number: for t in r['totals']: if t['label_en'] == b_service_area.name_en: self.assertEqual(2, t['total']) else: self.assertEqual(0, t['total']) else: self.assertTrue(all([t['total'] == 0 for t in r['totals']]))
def test_service_detail_page(self): service = ServiceFactory(status=Service.STATUS_CURRENT) self.load_page_and_set_language() menu = self.wait_for_element('menu') search = menu.find_elements_by_link_text('Search')[0] search.click() form = self.wait_for_element('search_controls') self.assertHashLocation('/search') form.find_element_by_name('filtered-search').send_keys( service.provider.name_en[:5]) # Results are updated automatically as search characters are entered # Wait a sec to make sure we have the final results time.sleep(1) result = self.wait_for_element('.search-result-list > li', match=By.CSS_SELECTOR) name = result.find_element_by_class_name('name') self.assertEqual(name.text, service.name_en) # Click the service's name to go to its detail page link = name.find_element(by=By.TAG_NAME, value='a') link.click() # Its cost should be displayed in the cost-of-service element WebDriverWait(self.browser, DEFAULT_TIMEOUT).until( expected_conditions.text_to_be_present_in_element( (By.ID, 'cost-of-service'), service.cost_of_service))
def execute_query(self, user_gql_client, profile_input): user = user_gql_client.user group = GroupFactory() service = ServiceFactory() user.groups.add(group) assign_perm("can_manage_profiles", group, service) assign_perm("can_manage_sensitivedata", group, service) assign_perm("can_view_sensitivedata", group, service) mutation = """ mutation createProfile($profileInput: CreateProfileInput!) { createProfile( input: { profile: $profileInput } ) { profile { id } } } """ variables = {"profileInput": profile_input} return user_gql_client.execute(mutation, service=service, variables=variables)
def test_duplicating_services(self): # given user = EmailUserFactory(is_staff=True, is_superuser=True, is_active=True) token, created = Token.objects.get_or_create(user=user) romania = GeographicRegion.objects.get(slug='romania') self.assertIsNotNone(romania) ServiceFactory(region=romania, status='current') existing_service = Service.objects.all() self.assertEqual(len(existing_service), 1) # when duplicate_post = self.api_client.post( path='/v2/services/' + str(existing_service[0].id) + '/duplicate/?new_name=Test123', HTTP_SERVICEINFOAUTHORIZATION="Token %s" % token.key, format='json') self.assertEqual(duplicate_post.status_code, 201) # then duplicated_service = Service.objects.filter(name_en='Test123') self.assertEqual(Service.objects.all().count(), 2) self.assertEqual(len(duplicated_service), 1) self.assertNotEqual(existing_service[0].slug, duplicated_service[0].slug)
def test_localized_search(self): """Search options and results should be localized.""" service = ServiceFactory(status=Service.STATUS_CURRENT) self.load_page_and_set_language('fr') menu = self.wait_for_element('menu') search = menu.find_elements_by_link_text('Recherche')[0] search.click() form = self.wait_for_element('search_controls') self.assertHashLocation('/search') Select(form.find_element_by_name('type')).select_by_visible_text( service.type.name_fr) controls = self.wait_for_element('map-toggle', match=By.CLASS_NAME) controls.find_element_by_name('map-toggle-list').click() try: result = self.wait_for_element('.search-result-list > li', match=By.CSS_SELECTOR) name = result.find_element_by_class_name('name') name_text = name.text except StaleElementReferenceException: # Hit a race where we got a search element but then the page replaced it result = self.wait_for_element('.search-result-list > li', match=By.CSS_SELECTOR) name = result.find_element_by_class_name('name') name_text = name.text self.assertEqual(name_text, service.name_fr)
def test_get_qos(self): a_type = ServiceType.objects.first() service = ServiceFactory(type=a_type) FeedbackFactory(service=service, delivered=True, quality=1) FeedbackFactory(service=service, delivered=True, quality=1) FeedbackFactory(service=service, delivered=True, quality=5) url = reverse('report-qos') rsp = self.get_with_token(url) self.assertEqual(OK, rsp.status_code) result = json.loads(rsp.content.decode('utf-8')) self.assertEqual(len(result), ServiceType.objects.all().count()) for r in result: if r['number'] == a_type.number: # 1, 2, 3, 4, 5 expected_totals = [ 2, 0, 0, 0, 1, ] else: expected_totals = [ 0, 0, 0, 0, 0, ] expected_labels = ['1', '2', '3', '4', '5'] self.assertIn('totals', r) totals = r['totals'] self.assertEqual([t['label_en'] for t in totals], expected_labels) self.assertEqual([t['total'] for t in totals], expected_totals)
def test_mobile_services_have_location_set(self): # If a service is mobile, we set its location on save to the center of its area area = ServiceArea.objects.first() # Mount Lebanon/Baabda service = ServiceFactory(is_mobile=True, location=None, area_of_service=area) self.assertIsNotNone(service.location) self.assertEqual(area.centroid, service.location)
def test_replacing_a_draft_comments_on_jira_issue(self, mock_JIRA): draft_service = ServiceFactory(status=Service.STATUS_DRAFT) # Pretend we've created a JIRA issue when the draft was started. issue_key = 'XXX-123' draft_service.jira_records.update(jira_issue_key=issue_key) mock_JIRA.return_value.create_issue.reset_mock() # Forget we "created a jira record" # Now edit the draft new_draft = ServiceFactory(update_of=draft_service, status=Service.STATUS_DRAFT) # We should have a new update record jira_record = new_draft.jira_records.get(update_type=JiraUpdateRecord.SUPERSEDED_DRAFT) jira_record.do_jira_work() # We should NOT have created another JIRA record self.assertFalse(mock_JIRA.return_value.create_issue.called) # We add a comment with links to the old and new data call_args, call_kwargs = mock_JIRA.return_value.add_comment.call_args self.assertEqual(issue_key, call_args[0]) self.assertIn(new_draft.get_admin_edit_url(), call_args[1])
def test_types_require_no_service(self): service = ServiceFactory() disallow_service_types = JiraUpdateRecord.PROVIDER_CHANGE_UPDATE_TYPES for update_type in disallow_service_types: with self.assertRaises(Exception) as cm: JiraUpdateRecord.objects.create(update_type=update_type, service=service) self.assertTrue( 'must not specify service' in str(cm.exception), msg='Unexpected exception message (%s) for %s' % (str(cm.exception), update_type))
def test_using_export_url_to_get_export(self): provider = ProviderFactory(user=self.user) ServiceFactory(provider=provider, status=Service.STATUS_CURRENT) url = self.get_export_url() rsp = self.client.get(url) self.assertEqual(OK, rsp.status_code) bits = rsp.content errs = validate_and_import_data(self.user, bits) self.assertFalse(errs)
def test_staff_see_all_current_data(self): user = EmailUserFactory(is_staff=True) provider = ProviderFactory() ProviderFactory() ServiceFactory(provider=provider, status=Service.STATUS_CURRENT) ServiceFactory(provider=provider, status=Service.STATUS_CURRENT) ServiceFactory(status=Service.STATUS_CURRENT) ServiceFactory(provider=provider, status=Service.STATUS_DRAFT) book = get_export_workbook_for_user(user) xlrd_book = save_and_read_book(book) # First sheet - providers sheet = xlrd_book.get_sheet(0) self.assertEqual(3, sheet.nrows - 1) # Second sheet - services sheet = xlrd_book.get_sheet(1) self.assertEqual(3, sheet.nrows - 1)
def test_change_service_then_approve_before_task(self, mock_JIRA): # If a service is approved before we try to do the JIRA work, # the JIRA work gracefully works self.test_service.update_of = ServiceFactory() self.jira_record.update_type = JiraUpdateRecord.CHANGE_SERVICE self.jira_record.save() self.test_service.staff_approve(self.staff_user) self.setup_issue_key(mock_JIRA) self.jira_record.do_jira_work()
def test_services_pagination(self): # Services results are paginated by default for x in range(10): ServiceFactory(status=Service.STATUS_CURRENT) rsp = self.client.get(self.url + "?page_size=5") # not authed self.assertEqual(OK, rsp.status_code, msg=rsp.content.decode('utf-8')) response = json.loads(rsp.content.decode('utf-8')) records_returned = response['results'] self.assertEqual(5, len(records_returned))
def setUp(self): self.service = ServiceFactory(status=Service.STATUS_DRAFT, location="POINT (33.0000 35.0000)") self.password = '******' self.user = EmailUserFactory(is_staff=True, password=self.password) assert self.user.is_staff group = Group.objects.get(name='Staff') self.user.groups.add(group) assert self.user.has_perm('services.change_service') assert self.client.login(email=self.user.email, password=self.password)
def test_location_exported_as_lat_long(self): provider = ProviderFactory() service1 = ServiceFactory(provider=provider, status=Service.STATUS_CURRENT) book = get_export_workbook_for_user(provider.user) xlrd_book = save_and_read_book(book) sheet = xlrd_book.get_sheet(1) values = sheet.row_values(1) data = dict(zip(SERVICE_HEADINGS, values)) self.assertEqual(service1.longitude, data['longitude']) self.assertEqual(service1.latitude, data['latitude'])
def test_cancel_cleans_up_pending_changes(self): service1 = ServiceFactory(status=Service.STATUS_CURRENT) # Make copy of service1 as an update service2 = Service.objects.get(pk=service1.pk) service2.pk = None service2.update_of = service1 service2.status = Service.STATUS_DRAFT service2.save() service1.cancel() service2 = Service.objects.get(pk=service2.pk) self.assertEqual(Service.STATUS_CANCELED, service2.status)
def test_create_issue_kwargs_for_update(self, mock_JIRA): self.test_service.update_of = ServiceFactory() self.jira_record.update_type = JiraUpdateRecord.CHANGE_SERVICE self.jira_record.save() self.setup_issue_key(mock_JIRA) self.jira_record.do_jira_work() call_args, call_kwargs = mock_JIRA.return_value.create_issue.call_args # Only checking summary as that has the essentially different value # from what the "new service" case sets. self.assertTrue('summary' in call_kwargs) self.assertTrue('changed service' in call_kwargs['summary'].lower()) self.assertTrue(self.test_service.provider.name_en in call_kwargs['summary'])
def test_open_close_as_hh_mm(self): provider = ProviderFactory() ServiceFactory(provider=provider, status=Service.STATUS_CURRENT, wednesday_close=time(18, 23) ) book = get_export_workbook_for_user(provider.user) xlrd_book = save_and_read_book(book) sheet = xlrd_book.get_sheet(1) values = sheet.row_values(1) data = dict(zip(SERVICE_HEADINGS, values)) self.assertEqual('', data['sunday_open']) self.assertEqual('18:23', data['wednesday_close'])
def test_provider_bad_criterion_id(self): provider = ProviderFactory(user=self.user) service = ServiceFactory(provider=provider, status=Service.STATUS_CURRENT) criterion1 = SelectionCriterionFactory.build(service=service) criterion1.id = 'abc' book = get_export_workbook([provider], None, [criterion1]) rsp = self.import_book(book) self.assertContains(rsp, "Row 2: id: %s is not a valid ID" % criterion1.id, status_code=BAD_REQUEST, msg_prefix=rsp.content.decode('utf-8'))
def test_service_connections_are_ordered_by_id(user_gql_client): profile = ProfileFactory(user=user_gql_client.user) connections = [ ServiceConnectionFactory(profile=profile, service=ServiceFactory()) for _ in range(3) ] executed = user_gql_client.execute(QUERY) connection_edges = executed["data"]["myProfile"]["serviceConnections"]["edges"] assert len(connection_edges) == len(connections) for edge, connection in zip(connection_edges, connections): assert from_global_id(edge["node"]["id"])[1] == str(connection.id)
def test_services_not_paginated_by_default(self): # Make sure that we don't paginate by default when the caller might # not be expecting it. # Create over 100 services - hopefully any default would tend to be # less than that. for x in range(101): ServiceFactory(status=Service.STATUS_CURRENT) rsp = self.client.get(self.url) # not authed self.assertEqual(OK, rsp.status_code, msg=rsp.content.decode('utf-8')) response = json.loads(rsp.content.decode('utf-8')) self.assertEqual( Service.objects.filter(status=Service.STATUS_CURRENT).count(), len(response))
def test_provider_change_nonexistent_criterion(self): provider = ProviderFactory(user=self.user) service = ServiceFactory(provider=provider, status=Service.STATUS_CURRENT) criterion1 = SelectionCriterionFactory(service=service) book = get_export_workbook([provider], None, [criterion1]) crit_id = criterion1.id criterion1.delete() rsp = self.import_book(book) self.assertContains(rsp, "Row 2: id: No selection criterion with id = %s" % crit_id, status_code=BAD_REQUEST, msg_prefix=rsp.content.decode('utf-8'))
def test_search_list_results_limited(self): """No more than 25 services in result""" for i in range(30): ServiceFactory(status=Service.STATUS_CURRENT) self.load_page_and_set_language() menu = self.wait_for_element('menu') search = menu.find_elements_by_link_text('Search')[0] search.click() self.wait_for_element('search_controls') self.assertHashLocation('/search') self.wait_for_element('.search-result-list > li', match=By.CSS_SELECTOR) results = self.browser.find_elements_by_css_selector( '.search-result-list > li') self.assertEqual(25, len(results))
def test_provider_bad_criteria(self): provider = ProviderFactory(user=self.user) service = ServiceFactory(provider=provider, status=Service.STATUS_CURRENT) criterion1 = SelectionCriterionFactory(service=service) criterion2 = SelectionCriterionFactory(service=service) # Change the 2nd one's text before exporting criterion2.text_en = criterion2.text_ar = criterion2.text_fr = '' book = get_export_workbook([provider], None, [criterion1, criterion2]) rsp = self.import_book(book) self.assertContains( rsp, "Selection criterion must have text in at least one language", status_code=BAD_REQUEST, msg_prefix=rsp.content.decode('utf-8'))
def execute_query(self, user_gql_client, profile_input): service = ServiceFactory() setup_profile_and_staff_user_to_service(self.profile, user_gql_client.user, service, can_manage_sensitivedata=True) profile_input["id"] = to_global_id("ProfileNode", self.profile.id) variables = {"profileInput": profile_input} return user_gql_client.execute( EMAILS_MUTATION, service=service, variables=variables, )