def test_add_link_for_document(self): document = DocumentFactory() documentcloud_id = 111111 normalized_title = 'normalized_title' title = 'title' process_link_func = MagicMock( return_value={ 'documentcloud_id': documentcloud_id, 'normalized_title': normalized_title, 'title': title }) with patch( 'dashboard.services.documentcloud_service.DocumentcloudService.process_link', new=process_link_func): response = self.client.post( reverse('dashboard-document-link'), { 'link': 'https://www.documentcloud.org/documents/%s-%s.html' % (documentcloud_id, normalized_title), 'id': document.id }) response.status_code.should.equal(200) document.refresh_from_db() document.documentcloud_id.should.equal(documentcloud_id) document.normalized_title.should.equal(normalized_title) document.title.should.equal(title)
def test_add_link(self): document = DocumentFactory() allegation = document.allegation allegation.crid = 1002643 allegation.save() crid = allegation.crid documentcloud_id = 1273509 normalized_title = 'cr-{crid}'.format(crid=crid) title = 'CR {crid}'.format(crid=crid) Setting.objects.create(default_site_title='CPDB', story_types_order='', requested_document_email_subject='{crid}', requested_document_email_text='{crid} {link}') RequestEmailFactory(document=document) response = self.client.post( reverse('dashboard-document-link'), { 'link': 'https://www.documentcloud.org/documents/%s-%s.html' % (documentcloud_id, normalized_title) }) response.status_code.should.equal(200) content = json.loads(response.content.decode()) content['crid'].should.contain(str(crid)) document.refresh_from_db() document.documentcloud_id.should.equal(documentcloud_id) document.normalized_title.should.equal(normalized_title) document.title.should.equal(title) # email notification len(mail.outbox).should.equal(1)
def test_add_link(self): document = DocumentFactory() allegation = document.allegation allegation.crid = 1002643 allegation.save() crid = allegation.crid documentcloud_id = 1273509 normalized_title = 'cr-{crid}'.format(crid=crid) title = 'CR {crid}'.format(crid=crid) Setting.objects.create( default_site_title='CPDB', story_types_order='', requested_document_email_subject='{crid}', requested_document_email_text='{crid} {link}' ) RequestEmailFactory(document=document) response = self.client.post(reverse('dashboard-document-link'), { 'link': 'https://www.documentcloud.org/documents/%s-%s.html' % ( documentcloud_id, normalized_title) }) response.status_code.should.equal(200) content = json.loads(response.content.decode()) content['crid'].should.contain(str(crid)) document.refresh_from_db() document.documentcloud_id.should.equal(documentcloud_id) document.normalized_title.should.equal(normalized_title) document.title.should.equal(title) # email notification len(mail.outbox).should.equal(1)
def test_get_document_request_analysis(self): DocumentFactory(requested=False, documentcloud_id=0) # Missing DocumentFactory(pending=False, requested=True, documentcloud_id=0) # Requested DocumentFactory(documentcloud_id=1) # Fulfilled DocumentFactory(pending=True, requested=True) # Pending self.go_to_documents() self.until_ajax_complete() for tab in ['missing', 'requested', 'fulfilled', 'pending']: tab_selector = '.tab-{tab_name} .analysis'.format(tab_name=tab) self.find(tab_selector).text.should.contain('1') self.find('.tab-all .analysis').text.should.contain('4')
def test_return_analaysis_of_document_requests(self): DocumentFactory(requested=False, documentcloud_id=0) # Missing DocumentFactory(pending=False, requested=True, documentcloud_id=0) # Requested DocumentFactory(documentcloud_id=1) # Fulfilled DocumentFactory(pending=True, requested=True) # Pending response, data = self.call_allegation_request_analysis() response.status_code.should.equal(200) for request_type in DOCUMENT_REQUEST_FILTERS: if request_type != 'All': data[request_type].should.equal(1) else: data[request_type].should.equal(4)
def test_return_400_when_submit_invalid_data(self): document = DocumentFactory() email = 'test' params = {'email': email, 'document_id': document.id} response = self.client.post(reverse('mobile:mobile-request-email'), params) response.status_code.should.equal(status.HTTP_400_BAD_REQUEST)
def setUp(self): self.category = AllegationCategoryFactory() self.allegation = AllegationFactory() DocumentFactory(documentcloud_id=None, requested=False, allegation=self.allegation) OfficerAllegationFactory(cat=self.category, allegation=self.allegation)
def test_filter_pending_documents(self): document = DocumentFactory(requested=True, pending=True) self.go_to_documents() self.go_to_tab('Pending') self.should_see_text(document.allegation.crid)
def test_update_auto_set_document_status(self): document = DocumentFactory(requested=True, pending=True) # Update document with documentcloud id document.update(documentcloud_id=1) document.refresh_from_db() document.requested.should.be.false document.pending.should.be.false # Update document pending document.update(pending=False) document.refresh_from_db() document.requested.should.be.true document.documentcloud_id.should.equal(0)
def test_go_to_request_by_crid(self): document = DocumentFactory() self.go_to_documents() self.find('.crid-request-search').send_keys('%s\n' % document.allegation.crid) self.until(self.ajax_complete) self.should_see_text(document.allegation.crid)
def test_update_num_requests(self): document = DocumentFactory() num_requests = document.number_of_request RequestEmailFactory(document=document) Document.objects.get(id=document.id).number_of_request.should.equal(num_requests + 1)
def test_cancel_document_request(self): document = DocumentFactory(requested=True) self.go_to_documents() self.button('Cancel', ".document").click() self.button('OK').click() self.until(self.ajax_complete) Document.objects.get(id=document.id).requested.should.be.false
def test_has_document(self): document = DocumentFactory(documentcloud_id=1) expected_allegations = [OfficerAllegationFactory(allegation=document.allegation)] OfficerAllegationFactory() query_string = 'has_document=true' expected_ids = [allegation.id for allegation in expected_allegations] self.check_built_query(query_string, expected_ids)
def test_sort_document_request(self): no_request_document = DocumentFactory(number_of_request=0) one_request_document = DocumentFactory(number_of_request=1) self.go_to_documents() self.until_ajax_complete() documents = self.find_all('tbody tr') documents[0].text.should.contain( str(one_request_document.allegation.crid)) documents[1].text.should.contain( str(no_request_document.allegation.crid)) self.element_by_tagname_and_text('th', 'No. of requests').click() self.until_ajax_complete() documents = self.find_all('tbody tr') documents[0].text.should.contain( str(no_request_document.allegation.crid)) documents[1].text.should.contain( str(one_request_document.allegation.crid))
def test_query_set(self): document = DocumentFactory(pending=False, requested=True, documentcloud_id=0) document_requestemail_1 = RequestEmailFactory(document=document) document_requestemail_2 = RequestEmailFactory(document=document) result = DocumentRequestQuerySet().get_filter(document.type) len(result).should.equal(1) result = result[0] result.emails.should.contain(document_requestemail_1.email) result.emails.should.contain(document_requestemail_2.email)
def test_error_adding_link(self): DocumentFactory() self.go_to_documents() self.button("Add document").click() self.until(lambda: self.should_see_text('Add document link')) self.element_for_label('Enter URL').send_keys('aaa') self.button('SUBMIT').click() self.until( lambda: self.should_see_text('Invalid link! Please check URL'))
def test_filter_by_has_document(self): document = DocumentFactory(documentcloud_id=1) # set allegation here to keep the test pass allegation = document.allegation allegation.document_id = 1 allegation.save() OfficerAllegationFactory(allegation=allegation) data = self.fetch_officer_allegations(has_document='true') len(data).should.equal(1) data[0]['allegation']['id'].should.equal(document.allegation.id)
def test_set_requesting(self): document = DocumentFactory() response = self.client.post( reverse('dashboard-document-request-status'), { 'id': document.id, 'status': 'requesting' }) response.status_code.should.equal(200) len(Document.objects.filter(pk=document.id, requested=True)).should.equal(1)
def test_save_valid_data(self): document = DocumentFactory() data = { 'document_id': document.id, 'email': '*****@*****.**' } requestSerializer = MobileDocumentRequestViewSerializer(data=data) requestSerializer.is_valid(raise_exception=True).should.be.true requestSerializer.save() RequestEmail.objects.filter(document=document).values_list('email', flat=True).should.contain('*****@*****.**')
def test_pending_fulfilled_document(self): document = DocumentFactory(documentcloud_id=1) response = self.client.post( reverse('dashboard-document-request-status'), { 'id': document.id, 'status': 'pending' }) response.status_code.should.equal(400) self.check_error(response, { '__all__': ['This document cannot be assigned that status'], })
def test_cancel_document_requests(self): document = DocumentFactory(requested=True, number_of_request=10) response = self.client.post( reverse('dashboard-document-request-status'), { 'id': document.id, 'status': 'missing' }) response.status_code.should.equal(200) len(Document.objects.filter(pk=document.id, requested=False)).should.equal(1)
def test_cancel_document_invalid(self): document = DocumentFactory(requested=False) response = self.client.post( reverse('dashboard-document-request-status'), { 'id': document.id, 'status': 'missing' }) response.status_code.should.equal(400) self.check_error(response, { '__all__': ['This document cannot be assigned that status'], })
def test_add_document_link(self): allegation = AllegationFactory(crid='1002643') document = DocumentFactory(allegation=allegation) self.go_to_documents() self.button('Add document').click() self.until(lambda: self.should_see_text('Add document link')) self.element_for_label('Enter URL').send_keys(TEST_DOCUMENT_URL) self.button('SUBMIT').click() self.until(lambda: self.should_see_text( 'The document is successfully added to allegation #{crid}!'.format( crid=document.allegation.crid)))
def test_successfully_create_request_email(self): document = DocumentFactory() email = '*****@*****.**' params = {'email': email, 'document_id': document.id} len(RequestEmail.objects.filter( email=email, document__id=document.id)).should.equal(0) response = self.client.post(reverse('mobile:mobile-request-email'), params) response.status_code.should.equal(status.HTTP_201_CREATED) len(RequestEmail.objects.filter( email=email, document__id=document.id)).should.equal(1)
def test_request_document(self): document = DocumentFactory() session = SessionFactory() response = self.client.post( self.URL, { 'document_id': document.id, 'email': faker.Faker().email(), 'session': session.hash_id, }) response.status_code.should.equal(200) data = self.json(response) data.should.contain('success') data['success'].should.be.ok
def test_pending_valid(self): document = DocumentFactory(requested=True, pending=False) response = self.client.post( reverse('dashboard-document-request-status'), { 'id': document.id, 'status': 'pending' }) response.status_code.should.equal(200) len( Document.objects.filter(id=document.id, requested=True, pending=True)).should.equal(1)
def test_cancel_pending(self): document = DocumentFactory(requested=True, pending=True) self.go_to_documents() self.go_to_tab('Pending') self.button('Cancel Pending').click() self.until(lambda: self. should_see_text('%s document pending has been cancelled.' % document.allegation.crid)) self.find_all('.status>span')[-1].text.should.equal('Requested') self.go_to_tab('Requested') self.should_see_text(document.allegation.crid)
def test_add_link_for_document(self): document = DocumentFactory() documentcloud_id = 111111 normalized_title = 'normalized_title' title = 'title' process_link_func = MagicMock(return_value={ 'documentcloud_id': documentcloud_id, 'normalized_title': normalized_title, 'title': title }) with patch('dashboard.services.documentcloud_service.DocumentcloudService.process_link', new=process_link_func): response = self.client.post(reverse('dashboard-document-link'), { 'link': 'https://www.documentcloud.org/documents/%s-%s.html' % (documentcloud_id, normalized_title), 'id': document.id }) response.status_code.should.equal(200) document.refresh_from_db() document.documentcloud_id.should.equal(documentcloud_id) document.normalized_title.should.equal(normalized_title) document.title.should.equal(title)
def test_invalid_email(self): document = DocumentFactory() data = { 'document_id': document.id, 'email': 'invalid' } requestSerializer = MobileDocumentRequestViewSerializer(data=data) requestSerializer.is_valid.when.called_with(raise_exception=True).should.throw( serializers.ValidationError, 'Enter a valid email address.' ) requestSerializer.is_valid(raise_exception=False).should.be.false
def test_change_requesting_to_pending(self): document = DocumentFactory(requested=True) self.go_to_documents() self.go_to_tab('Requested') self.button('Request').click() self.until(lambda: self.should_see_text( '%s document has been requested.' % document.allegation.crid)) self.go_to_tab('Pending') self.should_see_text(document.allegation.crid) buttons = [x.text for x in self.find_all('button')] buttons.shouldnt.contain('Pending')
def test_allegation_with_full_information(self): view_document_text = 'View' officer_gender = 'X' officer_gender_display = 'X' complaint_witness_gender = 'F' complaint_witness_race = 'Black' complaint_witness_age = 40 complaint_witness_text = 'Female, Black, Age 40' address1 = 4748 address2 = 'N Kimball Ave' addresss = '{add1} {add2}'.format(add1=address1, add2=address2) category = AllegationCategoryFactory() officer = OfficerFactory(gender=officer_gender) current_rank = 'SERGEANT OF POLICE' investigator = InvestigatorFactory(current_rank=current_rank) allegation = AllegationFactory( investigator=investigator, add1=address1, add2=address2, city='Chicago, IL', location='15') DocumentFactory(documentcloud_id=123, allegation=allegation) OfficerAllegationFactory(cat=category, officer=officer, allegation=allegation) ComplainingWitnessFactory( crid=allegation.crid, gender=complaint_witness_gender, allegation=allegation, race=complaint_witness_race, age=complaint_witness_age) self.visit_complaint_page(allegation.crid, category.id) self.until(lambda: self.find('.crid-number')) self.until(lambda: self.find('.allegation-category').text.should.be.equal(category.category)) self.find('.allegation-name').text.should.be.equal(category.allegation_name) self.should_see_text(officer.display_name, '.against-section') self.should_see_text(officer_gender_display, '.against-section') self.should_see_text(complaint_witness_text, '.complaining-witness-list') self.should_see_text(investigator.name, '.investigator .name') self.should_see_text(investigator.current_rank, '.investigator .rank') self.should_see_text(allegation.beat.name, '.location-detail') self.should_see_text(view_document_text, '.document-card') location_detail = self.find('.location-detail').text location_detail.should.contain(addresss) location_detail.should.contain(allegation.city) location_detail.should.contain(allegation.location)
def test_status_undefined(self): status = 'waiting for Half-life 3' document = DocumentFactory() response = self.client.post( reverse('dashboard-document-request-status'), { 'id': document.id, 'status': status }) response.status_code.should.equal(400) self.check_error( response, { 'status': [ 'Select a valid choice. %s is not one of the available choices.' % status ], })
def test_send_notification_on_new_document(self): document = DocumentFactory() unset_document = DocumentFactory(title='UNSET') with mock.patch(self.notification_path): with mock.patch(self.document_cloud_path) as document_cloud: title = fake.name() instance = document_cloud.return_value document = mock.Mock(title=title, id='1-2-3') # search return result instance.documents.search.return_value = [document] management.call_command('update_documents', end=unset_document.id - 1) document.refresh_from_db() document.title.should.equal(title) unset_document.refresh_from_db() unset_document.title.should.equal('UNSET') # it is being called, I've checked with pdb, but not being set send_document_notification.called.should.be.true