def test_update_officer_allegation_session(self): ids_to_update = [(OfficerAllegationFactory().id, OfficerAllegationFactory().id) for i in range(2)] session_1 = SessionFactory( query={'filters': { 'id': [{ 'value': ids_to_update[0][0] }] }}) SessionFactory( query={'filters': { 'def': [{ 'value': ids_to_update[0][0] }] }}) session_2 = SessionFactory(query={ 'activeComplaints': [ids_to_update[1][0], ids_to_update[1][0]] }) SessionFactory(query={'abc': ids_to_update[1][0]}) update_officer_allegation_session(ids_to_update) session_1.refresh_from_db() session_1.query['filters']['id'][0]['value'].should.equal( ids_to_update[0][1]) session_2.refresh_from_db() session_2.query['activeComplaints'].should.equal([ids_to_update[1][1]])
def test_get_sessions_with_query_for_id_included(self): SessionFactory() session = SessionFactory() params = {'q': session.hash_id} response, data = self.get_sessions(params) response.status_code.should.equal(200) len(data['results']).should.be(1)
def test_add_session_alias(self): alias = 'alias' session = SessionFactory() response = self.client.post('/api/dashboard/alias/', { 'alias': alias, 'target': 'http://cpdb.co{path}'.format(path=session.get_absolute_url()), }) response.status_code.should.equal(201) SessionAlias.objects.filter(alias=alias, session=session, user=self.user).count().should.equal(1)
def test_search_by_session_id(self): session = SessionFactory() SessionFactory() self.go_to_sessions() self.number_of_sessions().should.equal(2) self.search_for_session(session.hash_id) self.until_ajax_complete() self.number_of_sessions().should.equal(1)
def test_get_sessions_with_query_for_title_included(self): query = 'should' match_title = 'should_match' non_match_title = 'match' SessionFactory(title=match_title) SessionFactory(title=non_match_title) params = {'q': query} response, data = self.get_sessions(params) response.status_code.should.equal(200) len(data['results']).should.be(1)
def test_suggest_session_alias(self): alias = 'alias' query = alias[:3] not_searchable = SessionFactory(title='not searchable') session = SessionFactory(title='searchable') SessionAliasFactory(alias=alias, session=session) rebuild_index() self.fill_in('#autocomplete', query) self.until(lambda: self.element_by_classname_and_text( 'autocomplete-session', session.title).should.be.ok) self.element_by_classname_and_text('autocomplete-session', not_searchable.title).shouldnt.be.ok
def test_add_session_alias_full_form(self): session = SessionFactory() title = 'Session title' alias = 'alias' target = 'http://localhost{path}'.format(path=session.get_absolute_url()) self.go_to_sessions() self.button('Add Alias').click() self.until(lambda: self.should_see_text('Add Session Alias')) self.fill_alias_form(alias, title=title, target=target) session_alias = SessionAlias.objects.get(session_id=session.id) session_alias.alias.should.equal(alias) session_alias.title.should.equal(title)
def test_add_session_alias_full_form(self): session = SessionFactory() title = 'Session title' alias = 'alias' target = 'http://localhost{path}'.format( path=session.get_absolute_url()) self.go_to_sessions() self.button('Add Alias').click() self.until(lambda: self.should_see_text('Add Session Alias')) self.fill_alias_form(alias, title=title, target=target) session_alias = SessionAlias.objects.get(session_id=session.id) session_alias.alias.should.equal(alias) session_alias.title.should.equal(title)
def test_view_session_alias(self): session = SessionFactory() session_alias = SessionAliasFactory(title=fake.name()) SessionAliasFactory() self.go_to_sessions() self.element_by_tagname_and_text('li', 'Alias').click() self.until_ajax_complete() self.should_see_text(session_alias.session.hash_id) self.should_see_text(session_alias.user.username) self.should_see_text(session_alias.title) self.should_not_see_text(session.hash_id) row = self.find("tr.alias-row") row.find(".delete").click() alias = row.find(".alias").text self.until(lambda: self.should_see_text("Confirm delete alias")) self.button("OK").click() self.until_ajax_complete() self.until(lambda: self.should_not_see_text(alias)) self.should_see_text('Delete alias successfully') SessionAlias.objects.filter(alias=alias).exists().should.be.false
def test_get_valid_session(self): session = SessionFactory() response, data = self.call_get_session_api( {'hash_id': session.hash_id}) response.status_code.should.equal(200) data = data['data'] data['new'].should.equal(False)
def test_get_session_by_hash(self): session = SessionFactory() response = self.client.get( "/api/sessions/{hash}/".format(hash=session.hash_id)) response.status_code.should.equal(200) response_json = self.json(response) response_json['title'].should.equal(session.title)
def test_add_alias_success(self): session = SessionFactory() response = self.client.post('/api/dashboard/session-alias/', { 'alias': 'alias', 'target': session.id, }) response.status_code.should.equal(201)
def test_get_image(self): session_hash = SessionFactory().hash_id response = self.client.get(reverse('allegation:sunburst-image', args=[session_hash]), SERVER_NAME='localhost', SERVER_PORT='8081') response.status_code.should.equal(status.HTTP_200_OK) imghdr.what('', h=response.content).should.equal('png')
def init_session(self): self.db_session = SessionFactory() session = self.client.session session['owned_sessions'] = [self.db_session.id] session.save() self.update_params['hash'] = self.db_session.hash_id
def test_browse_session_admin(self): self.login_user() self.visit("/admin/models/") link = self.link("Sessions") link.should.be.ok session = SessionFactory() link.click() self.should_see_texts([ session.hash_id, "Fresh", session.query['title'] ]) shared_session = SessionFactory(share_from=session) self.browser.refresh() self.should_see_texts([ shared_session.hash_id, "Shared", shared_session.query['title'] ]) category = AllegationCategoryFactory() SessionFactory(query={ 'filters': { 'cat__category': { 'value': [category.category] } } }) self.browser.refresh() self.should_see_texts([ 'Chicago Police Database', category.category, ]) self.find("#searchbar").send_keys("{query}\n".format(query=session.query['title'])) self.should_see_text(session.query['title']) self.should_not_see_text(shared_session.query['title']) self.fill_in("#searchbar", "{query}\n".format(query=session.hash_id)) self.should_see_text(session.query['title']) self.should_not_see_text(shared_session.query['title'])
def test_search_by_title(self): session = SessionFactory() SessionAliasFactory(session=session, title='abc') SessionAliasFactory(title='edf') response = self.client.get('/api/dashboard/session-alias2/', { 'q': session.title[:3], }) self.response_contain_session(response, session)
def test_add_alias(self): alias = fake.name() session = SessionFactory() self.go_to_sessions() self.create_alias(alias) SessionAlias.objects.get(alias=alias).session.id.should.equal( session.id)
def test_see_sessions_management_section(self): session = SessionFactory() self.should_see_text('Sessions') self.go_to_sessions() self.find("#sessions").should.be.ok self.number_of_sessions().shouldnt.equal(0) self.should_see_text(session.hash_id)
def test_search_by_hash(self): session = SessionFactory() SessionAliasFactory(session=session) SessionAliasFactory() response = self.client.get('/api/dashboard/session-alias2/', { 'q': session.hash_id, }) self.response_contain_session(response, session)
def test_get_details_with_session(self): session = SessionFactory() SuggestionLogFactory(session_id=session.hash_id) FilterLogFactory(session_id=session.hash_id) response, data = self.get_sessions() response.status_code.should.equal(200) len(data['results']).should.be(1) len(data['results'][0]['suggestion_logs']).should.be(1) len(data['results'][0]['filter_logs']).should.be(1)
def test_redirect_desktop_session_to_mobile_detail_page(self): officer = OfficerFactory() filters = {'officer': [{'value': officer.id, 'category': 'officer'}]} session = SessionFactory(query={'filters': filters}) expected_url = MobileUrlBuilder().officer_page(officer) response = self.request_mobile_data_tool_page(hash_id=session.hash_id) response.status_code.should.equal(HTTP_301_MOVED_PERMANENTLY) response.url.should.contain(expected_url)
def test_facebook_og_tags(self): session = SessionFactory() hash_id = session.hash_id slug = 'cpdb-data' self.visit(reverse('datatool', args=[hash_id, slug])) self.find('meta[property="og:title"]').attrs['content'].should.be.equal(session.title) (reverse('datatool', args=[hash_id, slug]) in self.find('meta[property="og:url"]').attrs['content'])\ .should.be.true (reverse('allegation:sunburst-image', args=[hash_id]) in self.find('meta[property="og:image"]').attrs['content']).should.be.true self.find('meta[property="og:image:type"]').attrs['content'].should.be.equal('image/png')
def test_add_alias_custom_title(self): second_alias = fake.name() custom_title = fake.name() session = SessionFactory() self.go_to_sessions() self.create_alias(second_alias, custom_title) SessionAlias.objects.get(alias=second_alias, title=custom_title).session.id.should.equal( session.id)
def test_see_history_of_session(self): category = 'category' session = SessionFactory() suggestion = SuggestionLogFactory(session_id=session.hash_id) FilterLogFactory(session_id=session.hash_id, tag_name='cat__category=category') self.go_to_sessions() self.find('tr.session-row').click() self.should_see_text(suggestion.search_query) self.should_see_text(category)
def test_search_in_sessions_management_section(self): query = 'should' uppercase_query = 'SHOULD' match_title = 'should_match' non_match_title = 'no_match' SessionFactory(title=match_title) SessionFactory(title=non_match_title) self.go_to_sessions() self.number_of_sessions().should.equal(2) self.search_for_session(query) self.until_ajax_complete() self.number_of_sessions().should.equal(1) self.should_see_text(match_title) self.search_for_session(uppercase_query) self.until_ajax_complete() self.number_of_sessions().should.equal(1) self.should_see_text(match_title)
def test_build_query_string_from_session(self): session = SessionFactory(query={ 'filters': { 'bob': [{ 'filter': 'a=b' }], 'bib': [{ 'category': 'c', 'value': 'd' }] }}) query_str = build_query_string_from_session(session) (query_str in ['a=b&c=d', 'c=d&a=b']).should.be.true
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_update_officer_session(self): officer_1 = OfficerFactory() officer_2 = OfficerFactory() session_1 = SessionFactory( query={'filters': { 'officer': [{ 'value': officer_2.pk }] }}) SessionFactory(query={'filters': {'def': [{'value': officer_2.pk}]}}) session_2 = SessionFactory(query={ 'active_officers': [officer_2.pk, officer_2.pk, officer_1.pk] }) SessionFactory(query={'cde': officer_2.pk}) update_officer_session(officer_1, officer_2) session_1.refresh_from_db() session_1.query['filters']['officer'][0]['value'].should.equal( officer_1.pk) session_2.refresh_from_db() session_2.query['active_officers'].should.equal([officer_1.pk])
def test_go_to_suggested_session(self): alias = 'alias' query = alias[:3] session = SessionFactory() SessionAliasFactory(alias=alias, session=session) rebuild_index() current_url = self.browser.current_url self.find('#autocomplete').send_keys(query) self.until(lambda: self.find('.autocomplete-session').click()) self.until(lambda: self.browser.current_url != current_url) cloned_session = Session.objects.get(share_from=session) self.browser.current_url.should.contain(cloned_session.hash_id)
def test_redirect_desktop_session_to_mobile_search_page(self): officer = OfficerFactory() allegation = AllegationFactory() filters = { 'allegation__crid': [{ 'value': allegation.crid, 'category': 'allegation__crid' }], 'officer': [{ 'value': officer.id, 'category': 'officer' }] } session = SessionFactory(query={'filters': filters}) response = self.request_mobile_data_tool_page(hash_id=session.hash_id) response.status_code.should.equal(HTTP_301_MOVED_PERMANENTLY) response.url.should.contain(DEFAULT_REDIRECT_URL)
def test_home_page_generate_new_share_when_access_a_share(self): session = SessionFactory() session_count = Session.objects.all().count() title_slug = slugify(session.title) response = self.visit("/data/%s/%s" % (session.hash_id, title_slug)) Session.objects.all().count().should.equal(session_count + 1) # 1 new session created location = self.browser.current_url new_hash_id = location.split("/")[-2] new_session_id = Session.id_from_hash(new_hash_id)[0] new_session = Session.objects.get(pk=new_session_id) dict(self.compact(new_session.query)).should.equal( self.compact(dict(session.query))) response = self.client.get(location) response.status_code.should.equal(200) Session.objects.all().count().should.equal(session_count + 1) # 0 new session created
def test_update_officer_session(self): officer_1 = OfficerFactory() officer_2 = OfficerFactory() session_1 = SessionFactory( query={'filters': {'officer': [{'value': officer_2.pk}]}}) SessionFactory(query={'filters': {'def': [{'value': officer_2.pk}]}}) session_2 = SessionFactory(query={'active_officers': [officer_2.pk, officer_2.pk, officer_1.pk]}) SessionFactory(query={'cde': officer_2.pk}) update_officer_session(officer_1, officer_2) session_1.refresh_from_db() session_1.query['filters']['officer'][0]['value'].should.equal(officer_1.pk) session_2.refresh_from_db() session_2.query['active_officers'].should.equal([officer_1.pk])
def test_update_officer_allegation_session(self): ids_to_update = [ (OfficerAllegationFactory().id, OfficerAllegationFactory().id) for i in range(2)] session_1 = SessionFactory(query={'filters': {'id': [{'value': ids_to_update[0][0]}]}}) SessionFactory(query={'filters': {'def': [{'value': ids_to_update[0][0]}]}}) session_2 = SessionFactory(query={'activeComplaints': [ ids_to_update[1][0], ids_to_update[1][0]]}) SessionFactory(query={'abc': ids_to_update[1][0]}) update_officer_allegation_session(ids_to_update) session_1.refresh_from_db() session_1.query['filters']['id'][0]['value'].should.equal(ids_to_update[0][1]) session_2.refresh_from_db() session_2.query['activeComplaints'].should.equal([ids_to_update[1][1]])