def setUp(self): super(BulletinTestCase, self).setUp() self.test_user_util = TestUserUtility() self.user = self.test_user_util.user self.location = Location(name_en='test location', loc_type='Village') self.location.save() self.actor = Actor(fullname_en='Test Actor', fullname_ar='Test name ar', nickname_en='nick name', nickname_ar='nick name') self.actor.save() self.role = ActorRole(role_status='Detained', actor_id=self.actor.pk) self.role.save() self.sourceType = SourceType(source_type='test source type', description='test source description') self.sourceType.save() self.source = Source(reliability_score=0, name_en='test source', source_type_id=self.sourceType.pk) self.source.save() self.source = Source(reliability_score=0, name_en='test source 2', source_type_id=self.sourceType.pk) self.source.save() self.label = Label(name_en='test label') self.label.save() self.comment = Comment(assigned_user_id=self.user.pk, status_id=3, comments_en='test comment') self.comment.save() self.media = Media(media_type='Video', name_en='test media', media_file='') self.media.save() fixture = AutoFixture(Bulletin, generate_m2m={1, 5}) fixture.create(10) try: self.api_key = ApiKey.objects.get(user=self.user) except ApiKey.DoesNotExist: self.api_key = ApiKey.objects.create(user=self.user) self.auth_string = '&username={0}&api_key={1}'.format( self.user.username, self.api_key.key)
def setUp(self): super(ActorRelationshipTestCase, self).setUp() self.user = User(username='******', password='******', email='*****@*****.**') self.user.save() self.actor = Actor(fullname_ar='Test name ar', nickname_en='nick name', nickname_ar='nick name') self.actor.save() fixture = AutoFixture(ActorRelationship, generate_m2m={1, 4}) actorRelationshipps = fixture.create(10) try: self.api_key = ApiKey.objects.get(user=self.user) except ApiKey.DoesNotExist: self.api_key = ApiKey.objects.create(user=self.user) self.auth_string = '&username={0}&api_key={1}'.format( self.user.username, self.api_key.key)
def setUp(self): super(ActorTestCase, self).setUp() self.test_user_util = TestUserUtility() self.user = self.test_user_util.user fixture = AutoFixture(Actor) fixture.create(10) self.actor = Actor(fullname_en='Test Actor', fullname_ar='Test name ar', nickname_en='nick name', nickname_ar='nick name') self.actor.save() try: self.api_key = ApiKey.objects.get(user=self.user) except ApiKey.DoesNotExist: self.api_key = ApiKey.objects.create(user=self.user) self.auth_string = '&username={0}&api_key={1}'.format( self.user.username, self.api_key.key)
def setUp(self): super(IncidentTestCase, self).setUp() self.test_user_util = TestUserUtility() self.user = self.test_user_util.user self.location = Location(name_en='test location', loc_type='Village') self.location.save() self.actor = Actor( fullname_en='Test Actor', fullname_ar='Test name ar', nickname_en='nick name', nickname_ar='nick name') self.actor.save() self.role = ActorRole(role_status='Detained', actor_id=self.actor.pk) self.role.save() self.statusUpdate = StatusUpdate(status_en='test status') self.statusUpdate.save() self.crimeCategory = CrimeCategory( name_en='test crime category', level=1, description_en='test source incident_details') self.crimeCategory.save() self.label = Label(name_en='test label') self.label.save() self.comment = Comment( assigned_user_id=self.user.pk, status_id=self.statusUpdate.pk, comments_en='test comment') self.comment.save() fixture = AutoFixture(Incident, generate_m2m={1, 5}) fixture.create(10) try: self.api_key = ApiKey.objects.get(user=self.user) except ApiKey.DoesNotExist: self.api_key = ApiKey.objects.create(user=self.user) self.auth_string = '&username={0}&api_key={1}'.format( self.user.username, self.api_key.key)
def setUp(self): super(ActorTestCase, self).setUp() self.test_user_util = TestUserUtility() self.user = self.test_user_util.user fixture = AutoFixture(Actor) fixture.create(10) self.actor = Actor( fullname_en='Test Actor', fullname_ar='Test name ar', nickname_en='nick name', nickname_ar='nick name' ) self.actor.save() try: self.api_key = ApiKey.objects.get(user=self.user) except ApiKey.DoesNotExist: self.api_key = ApiKey.objects.create(user=self.user) self.auth_string = '&username={0}&api_key={1}'.format( self.user.username, self.api_key.key)
def test_actor_post(self): post_data = { 'fullname_en': "Test Actor", 'fullname_ar': "Test Actor Arabic", 'nickname_en': "Nickname en", 'nickname_ar': "Nickname Arabic", 'comment': 'created' } url = '/api/v1/actor/?format=json{}'.format(self.auth_string) response = self.api_client.post(url, data=post_data) self.assertEqual(response.status_code, 201) # test that a comment get added new_actor_dict = json.loads(response.content) new_actor = Actor(id=new_actor_dict['id']) actor_comments = new_actor.actor_comments.all() self.assertEqual(len(actor_comments), 1) self.assertEqual(actor_comments[0].status.status_en, 'Human Created') vs = VersionStatus.objects.filter( user_id=self.user.id).order_by('version_timestamp') self.assertEqual(len(vs), 1)
class ActorRoleTestCase(ResourceTestCase): def setUp(self): super(ActorRoleTestCase, self).setUp() self.user = User(username='******', password='******', email='*****@*****.**') self.user.save() self.actor = Actor(fullname_en='Test Actor', fullname_ar='Test name ar', nickname_en='nick name', nickname_ar='nick name') self.actor.save() fixture = AutoFixture(ActorRole, generate_m2m={1, 4}) fixture.create(10) try: self.api_key = ApiKey.objects.get(user=self.user) except ApiKey.DoesNotExist: self.api_key = ApiKey.objects.create(user=self.user) self.auth_string = '&username={0}&api_key={1}'.format( self.user.username, self.api_key.key) def tearDown(self): Actor.objects.all().delete() ActorRole.objects.all().delete() def test_actorRole_get(self): url = '/api/v1/actorRole/?format=json{}'.format(self.auth_string) response = self.api_client.get(url) self.assertEqual(response.status_code, 200) unauth_url = '/api/v1/actorRole/?format=json' response = self.api_client.get(unauth_url) self.assertEqual(response.status_code, 401) def test_actorRole_post(self): post_data = { 'role_status': "K", 'actor': "/api/v1/actor/{0}/".format(self.actor.pk) } url = '/api/v1/actorRole/?format=json{}'.format(self.auth_string) response = self.api_client.post(url, data=post_data) self.assertEqual(response.status_code, 201) def test_actorRole_put(self): precreated_actor_role = ActorRole.objects.all()[0] url = '/api/v1/actorRole/{0}/?format=json{1}'.format( precreated_actor_role.id, self.auth_string) put_data = { 'role_status': "K", 'actor': "/api/v1/actor/{0}/".format(self.actor.pk), } response = self.api_client.put(url, data=put_data) self.assertEqual(response.status_code, 202) def test_actorRole_patch(self): url = '/api/v1/actorRole/?format=json{}'.format(self.auth_string) patch_data = { 'objects': [{ 'role_status': "K", 'actor': "/api/v1/actor/{0}/".format(self.actor.pk) }, { 'role_status': "K", 'actor': "/api/v1/actor/{0}/".format(self.actor.pk) }] } response = self.api_client.patch(url, data=patch_data) self.assertEqual(response.status_code, 202) def test_actorRole_validation(self): post_data = { 'role_status': "Invalid key code", 'actor': "/api/v1/actor/{0}/".format(self.actor.pk) } url = '/api/v1/actorRole/?format=json{}'.format(self.auth_string) response = self.api_client.post(url, data=post_data) self.assertEqual(response.status_code, 400)
class ActorRelationshipTestCase(ResourceTestCase): def setUp(self): super(ActorRelationshipTestCase, self).setUp() self.user = User(username='******', password='******', email='*****@*****.**') self.user.save() self.actor = Actor(fullname_ar='Test name ar', nickname_en='nick name', nickname_ar='nick name') self.actor.save() fixture = AutoFixture(ActorRelationship, generate_m2m={1, 4}) actorRelationshipps = fixture.create(10) try: self.api_key = ApiKey.objects.get(user=self.user) except ApiKey.DoesNotExist: self.api_key = ApiKey.objects.create(user=self.user) self.auth_string = '&username={0}&api_key={1}'.format( self.user.username, self.api_key.key) def tearDown(self): Actor.objects.all().delete() ActorRelationship.objects.all().delete() def test_actor_relationship_get(self): url = '/api/v1/actorRelationship/?format=json{}'.format(self.auth_string) response = self.api_client.get(url) self.assertEqual(response.status_code, 200) unauth_url = '/api/v1/actorRelationship/?format=json' response = self.api_client.get(unauth_url) self.assertEqual(response.status_code, 401) def test_actor_relationship_post(self): post_data = { 'relation_status': "Parent", 'actor': "/api/v1/actor/{0}/".format(self.actor.pk), } url = '/api/v1/actorRelationship/?format=json{}'.format(self.auth_string) response = self.api_client.post(url, data=post_data) self.assertEqual(response.status_code, 201) def test_actor_relationship_put(self): precreated_actor_rel = ActorRelationship.objects.all()[0] url = '/api/v1/actorRelationship/{0}/?format=json{1}'.format(precreated_actor_rel.id, self.auth_string) put_data = { 'relation_status': "Parent", 'actor': "/api/v1/actor/{0}/".format(self.actor.pk), } response = self.api_client.put(url, data=put_data) self.assertEqual(response.status_code, 202) def test_actor_relationship_patch(self): url = '/api/v1/actorRelationship/?format=json{}'.format(self.auth_string) patch_data = { 'objects': [ { 'relation_status': "Parent", 'actor': "/api/v1/actor/{0}/".format(self.actor.pk), }, { 'relation_status': "Parent", 'actor': "/api/v1/actor/{0}/".format(self.actor.pk), } ] } response = self.api_client.patch(url, data=patch_data) self.assertEqual(response.status_code, 202)
class ActorTestCase(ResourceTestCase): ''' test the actor tastypie api ''' fixtures = ['test_data_role.json', 'status_update', ] def setUp(self): super(ActorTestCase, self).setUp() self.test_user_util = TestUserUtility() self.user = self.test_user_util.user fixture = AutoFixture(Actor) fixture.create(10) self.actor = Actor( fullname_en='Test Actor', fullname_ar='Test name ar', nickname_en='nick name', nickname_ar='nick name' ) self.actor.save() try: self.api_key = ApiKey.objects.get(user=self.user) except ApiKey.DoesNotExist: self.api_key = ApiKey.objects.create(user=self.user) self.auth_string = '&username={0}&api_key={1}'.format( self.user.username, self.api_key.key) def tearDown(self): Actor.objects.all().delete() User.objects.all().delete() def test_actor_get(self): url = '/api/v1/actor/?format=json{}'.format(self.auth_string) response = self.api_client.get(url) self.assertEqual(response.status_code, 200) unauth_url = '/api/v1/actor/?format=json' response = self.api_client.get(unauth_url) self.assertEqual(response.status_code, 401) def test_actor_post(self): post_data = { 'fullname_en': "Test Actor", 'fullname_ar': "Test Actor Arabic", 'nickname_en': "Nickname en", 'nickname_ar': "Nickname Arabic", 'comment': 'created' } url = '/api/v1/actor/?format=json{}'.format(self.auth_string) response = self.api_client.post(url, data=post_data) self.assertEqual(response.status_code, 201) # test that a comment get added new_actor_dict = json.loads(response.content) new_actor = Actor(id=new_actor_dict['id']) actor_comments = new_actor.actor_comments.all() self.assertEqual(len(actor_comments), 1) self.assertEqual(actor_comments[0].status.status_en, 'Human Created') vs = VersionStatus.objects.filter( user_id=self.user.id).order_by('version_timestamp') self.assertEqual(len(vs), 1) def test_actor_bulletin_related(self): post_data = { 'fullname_en': "Test Actor", 'fullname_ar': "Test Actor Arabic", 'nickname_en': "Nickname en", 'nickname_ar': "Nickname Arabic", 'comment': 'created' } url = '/api/v1/actor/?format=json{}'.format(self.auth_string) response = self.api_client.post(url, data=post_data) self.assertEqual(response.status_code, 201) new_actor_dict = json.loads(response.content) post_data = { 'role_status': "K", 'actor': "/api/v1/actor/{0}/".format( new_actor_dict['id'] ) } url = '/api/v1/actorRole/?format=json{}'.format(self.auth_string) response = self.api_client.post(url, data=post_data) self.assertEqual(response.status_code, 201) new_actorrole_dict = json.loads(response.content) post_data = { 'title_en': "Test Bulletin", 'description_ar': "description Arabic", 'confidence_score': 73, 'sources': [], 'bulletin_imported_comments': [], 'assigned_user': '******', 'actors_role': [ '/api/v1/actorRole/{0}/'.format( new_actorrole_dict['id'] ) ], 'times': [], 'medias': [], 'locations': [], 'labels': [], 'ref_bulletins': [], 'comment': 'new bulletin', } url = '/api/v1/bulletin/?format=json{}'.format(self.auth_string) response = self.api_client.post(url, data=post_data) self.assertEqual(response.status_code, 201) request_url = '/api/v1/actor/{0}/?format=json{1}'.format( new_actor_dict['id'], self.auth_string ) response = self.api_client.get(request_url) actor_dict_post = json.loads(response.content) self.assertEqual( actor_dict_post['related_bulletins'][0], '/api/v1/bulletin/1/' ) def test_actor_incident_related(self): post_data = { 'fullname_en': "Test Actor", 'fullname_ar': "Test Actor Arabic", 'nickname_en': "Nickname en", 'nickname_ar': "Nickname Arabic", 'comment': 'created' } url = '/api/v1/actor/?format=json{}'.format(self.auth_string) response = self.api_client.post(url, data=post_data) self.assertEqual(response.status_code, 201) new_actor_dict = json.loads(response.content) post_data = { 'role_status': "K", 'actor': "/api/v1/actor/{0}/".format( new_actor_dict['id'] ) } url = '/api/v1/actorRole/?format=json{}'.format(self.auth_string) response = self.api_client.post(url, data=post_data) self.assertEqual(response.status_code, 201) new_actorrole_dict = json.loads(response.content) post_data = { 'title_en': "Test Incident", 'incident_details_ar': "incident_details Arabic", 'confidence_score': 11, 'assigned_user': '******', 'incident_comments': ['/api/v1/comment/1/', ], 'bulletins': [], 'actors_role': [ '/api/v1/actorRole/{0}/'.format( new_actorrole_dict['id'] ) ], 'crimes': [], 'labels': [], 'times': [], 'locations': [], 'ref_incidents': [], 'status': '/api/v1/statusUpdate/1/', 'comment': 'Comment', 'status_uri': '/api/v1/statusUpdate/1/' } url = '/api/v1/incident/?format=json{}'.format(self.auth_string) response = self.api_client.post(url, data=post_data) self.assertEqual(response.status_code, 201) request_url = '/api/v1/actor/{0}/?format=json{1}'.format( new_actor_dict['id'], self.auth_string ) response = self.api_client.get(request_url) actor_dict_post = json.loads(response.content) self.assertEqual( actor_dict_post['related_incidents'][0], '/api/v1/incident/1/' ) def test_actor_put(self): ''' create and actor and attach a comment to it, then send a put to create a new one. check that the comment get's appended and that for data-analyst users it can only be updated ''' self.test_user_util.add_user_to_group('data-analyst') post_data = { 'assigned_user': '******', 'comments_en': "Test Comment", 'comments_ar': "Test Comment Arabic", } url = '/api/v1/comment/?format=json{}'.format(self.auth_string) response = self.api_client.post(url, data=post_data) comment_uri = json.loads(response.content)['resource_uri'] precreated_actor = Actor.objects.all()[0] url = '/api/v1/actor/{0}/?format=json{1}'.format( precreated_actor.id, self.auth_string) actor_comments = [comment_uri, ] put_data = create_put_data(1, actor_comments) response = self.api_client.put(url, data=put_data) self.check_dehydrated_data(response) self.assertEqual(response.status_code, 200) self.assertEqual(retrieve_last_comment_status(response), 'Updated') def test_senior_data_analyst_put(self): self.test_user_util.add_user_to_group('senior-data-analyst') precreated_actor = Actor.objects.all()[0] url = '/api/v1/actor/{0}/?format=json{1}'.format( precreated_actor.id, self.auth_string) put_data = create_put_data(4) response = self.api_client.put(url, data=put_data) self.assertEqual(response.status_code, 200) self.assertEqual(retrieve_last_comment_status(response), 'Reviewed') def test_chief_data_analyst_put(self): self.test_user_util.add_user_to_group('chief-data-analyst') precreated_actor = Actor.objects.all()[0] url = '/api/v1/actor/{0}/?format=json{1}'.format( precreated_actor.id, self.auth_string) put_data = create_put_data(5) response = self.api_client.put(url, data=put_data) self.assertEqual(response.status_code, 200) self.assertEqual(retrieve_last_comment_status(response), 'Finalized') def test_finalized_is_not_updated(self): precreated_actor = Actor.objects.all()[0] comment = Comment( assigned_user_id=1, comments_en='comment', status_id=5 ) comment.save() precreated_actor.actor_comments.add(comment) url = '/api/v1/actor/{0}/?format=json{1}'.format( precreated_actor.id, self.auth_string) print url put_data = create_put_data(4) response = self.api_client.put(url, data=put_data) self.assertEqual(response.status_code, 403) def test_assigned_user_perm_enforced(self): self.test_user_util.add_user_to_group('data-analyst') fixture = AutoFixture(User) fixture.create(1) precreated_actor = Actor.objects.all()[0] precreated_actor.assigned_user = User.objects.get(id=2) precreated_actor.save() url = '/api/v1/actor/{0}/?format=json{1}'.format( precreated_actor.id, self.auth_string) put_data = create_put_data(4) put_data['assigned_user'] = '******' response = self.api_client.put(url, data=put_data) assigned_user_id = retrieve_user_id(response) self.assertEqual(assigned_user_id, 2) self.test_user_util.add_user_to_group('chief-data-analyst') response = self.api_client.put(url, data=put_data) assigned_user_id = retrieve_user_id(response) self.assertEqual(assigned_user_id, 1) def check_dehydrated_data(self, response): """ Test that returned data contains required dehydrated fields. New fields should be added to this method as they are added to the API to ensure that consistency between front and back end. """ dehydrate_keys = { 'related_bulletins', 'related_incidents', 'count_incidents', 'count_bulletins', 'roles', 'actors_role', 'actors', 'thumbnail_url', 'actor_roles_status', 'most_recent_status_actor' } content = json.loads(response.content) self.assertEqual( all( test in content for test in dehydrate_keys ), True )
class ActorTestCase(ResourceTestCase): ''' test the actor tastypie api ''' fixtures = [ 'test_data_role.json', 'status_update', ] def setUp(self): super(ActorTestCase, self).setUp() self.test_user_util = TestUserUtility() self.user = self.test_user_util.user fixture = AutoFixture(Actor) fixture.create(10) self.actor = Actor(fullname_en='Test Actor', fullname_ar='Test name ar', nickname_en='nick name', nickname_ar='nick name') self.actor.save() try: self.api_key = ApiKey.objects.get(user=self.user) except ApiKey.DoesNotExist: self.api_key = ApiKey.objects.create(user=self.user) self.auth_string = '&username={0}&api_key={1}'.format( self.user.username, self.api_key.key) def tearDown(self): Actor.objects.all().delete() User.objects.all().delete() def test_actor_get(self): url = '/api/v1/actor/?format=json{}'.format(self.auth_string) response = self.api_client.get(url) self.assertEqual(response.status_code, 200) unauth_url = '/api/v1/actor/?format=json' response = self.api_client.get(unauth_url) self.assertEqual(response.status_code, 401) def test_actor_post(self): post_data = { 'fullname_en': "Test Actor", 'fullname_ar': "Test Actor Arabic", 'nickname_en': "Nickname en", 'nickname_ar': "Nickname Arabic", 'comment': 'created' } url = '/api/v1/actor/?format=json{}'.format(self.auth_string) response = self.api_client.post(url, data=post_data) self.assertEqual(response.status_code, 201) # test that a comment get added new_actor_dict = json.loads(response.content) new_actor = Actor(id=new_actor_dict['id']) actor_comments = new_actor.actor_comments.all() self.assertEqual(len(actor_comments), 1) self.assertEqual(actor_comments[0].status.status_en, 'Human Created') vs = VersionStatus.objects.filter( user_id=self.user.id).order_by('version_timestamp') self.assertEqual(len(vs), 1) def test_actor_bulletin_related(self): post_data = { 'fullname_en': "Test Actor", 'fullname_ar': "Test Actor Arabic", 'nickname_en': "Nickname en", 'nickname_ar': "Nickname Arabic", 'comment': 'created' } url = '/api/v1/actor/?format=json{}'.format(self.auth_string) response = self.api_client.post(url, data=post_data) self.assertEqual(response.status_code, 201) new_actor_dict = json.loads(response.content) post_data = { 'role_status': "K", 'actor': "/api/v1/actor/{0}/".format(new_actor_dict['id']) } url = '/api/v1/actorRole/?format=json{}'.format(self.auth_string) response = self.api_client.post(url, data=post_data) self.assertEqual(response.status_code, 201) new_actorrole_dict = json.loads(response.content) post_data = { 'title_en': "Test Bulletin", 'description_ar': "description Arabic", 'confidence_score': 73, 'sources': [], 'bulletin_imported_comments': [], 'assigned_user': '******', 'actors_role': ['/api/v1/actorRole/{0}/'.format(new_actorrole_dict['id'])], 'times': [], 'medias': [], 'locations': [], 'labels': [], 'ref_bulletins': [], 'comment': 'new bulletin', } url = '/api/v1/bulletin/?format=json{}'.format(self.auth_string) response = self.api_client.post(url, data=post_data) self.assertEqual(response.status_code, 201) request_url = '/api/v1/actor/{0}/?format=json{1}'.format( new_actor_dict['id'], self.auth_string) response = self.api_client.get(request_url) actor_dict_post = json.loads(response.content) self.assertEqual(actor_dict_post['related_bulletins'][0], '/api/v1/bulletin/1/') def test_actor_incident_related(self): post_data = { 'fullname_en': "Test Actor", 'fullname_ar': "Test Actor Arabic", 'nickname_en': "Nickname en", 'nickname_ar': "Nickname Arabic", 'comment': 'created' } url = '/api/v1/actor/?format=json{}'.format(self.auth_string) response = self.api_client.post(url, data=post_data) self.assertEqual(response.status_code, 201) new_actor_dict = json.loads(response.content) post_data = { 'role_status': "K", 'actor': "/api/v1/actor/{0}/".format(new_actor_dict['id']) } url = '/api/v1/actorRole/?format=json{}'.format(self.auth_string) response = self.api_client.post(url, data=post_data) self.assertEqual(response.status_code, 201) new_actorrole_dict = json.loads(response.content) post_data = { 'title_en': "Test Incident", 'incident_details_ar': "incident_details Arabic", 'confidence_score': 11, 'assigned_user': '******', 'incident_comments': [ '/api/v1/comment/1/', ], 'bulletins': [], 'actors_role': ['/api/v1/actorRole/{0}/'.format(new_actorrole_dict['id'])], 'crimes': [], 'labels': [], 'times': [], 'locations': [], 'ref_incidents': [], 'status': '/api/v1/statusUpdate/1/', 'comment': 'Comment', 'status_uri': '/api/v1/statusUpdate/1/' } url = '/api/v1/incident/?format=json{}'.format(self.auth_string) response = self.api_client.post(url, data=post_data) self.assertEqual(response.status_code, 201) request_url = '/api/v1/actor/{0}/?format=json{1}'.format( new_actor_dict['id'], self.auth_string) response = self.api_client.get(request_url) actor_dict_post = json.loads(response.content) self.assertEqual(actor_dict_post['related_incidents'][0], '/api/v1/incident/1/') def test_actor_put(self): ''' create and actor and attach a comment to it, then send a put to create a new one. check that the comment get's appended and that for data-analyst users it can only be updated ''' self.test_user_util.add_user_to_group('data-analyst') post_data = { 'assigned_user': '******', 'comments_en': "Test Comment", 'comments_ar': "Test Comment Arabic", } url = '/api/v1/comment/?format=json{}'.format(self.auth_string) response = self.api_client.post(url, data=post_data) comment_uri = json.loads(response.content)['resource_uri'] precreated_actor = Actor.objects.all()[0] url = '/api/v1/actor/{0}/?format=json{1}'.format( precreated_actor.id, self.auth_string) actor_comments = [ comment_uri, ] put_data = create_put_data(1, actor_comments) response = self.api_client.put(url, data=put_data) self.check_dehydrated_data(response) self.assertEqual(response.status_code, 202) self.assertEqual(retrieve_last_comment_status(response), 'Updated') def test_senior_data_analyst_put(self): self.test_user_util.add_user_to_group('senior-data-analyst') precreated_actor = Actor.objects.all()[0] url = '/api/v1/actor/{0}/?format=json{1}'.format( precreated_actor.id, self.auth_string) put_data = create_put_data(4) response = self.api_client.put(url, data=put_data) self.assertEqual(response.status_code, 202) self.assertEqual(retrieve_last_comment_status(response), 'Reviewed') def test_chief_data_analyst_put(self): self.test_user_util.add_user_to_group('chief-data-analyst') precreated_actor = Actor.objects.all()[0] url = '/api/v1/actor/{0}/?format=json{1}'.format( precreated_actor.id, self.auth_string) put_data = create_put_data(5) response = self.api_client.put(url, data=put_data) self.assertEqual(response.status_code, 202) self.assertEqual(retrieve_last_comment_status(response), 'Finalized') def test_finalized_is_not_updated(self): precreated_actor = Actor.objects.all()[0] comment = Comment(assigned_user_id=1, comments_en='comment', status_id=5) comment.save() precreated_actor.actor_comments.add(comment) url = '/api/v1/actor/{0}/?format=json{1}'.format( precreated_actor.id, self.auth_string) print url put_data = create_put_data(4) response = self.api_client.put(url, data=put_data) self.assertEqual(response.status_code, 403) def test_assigned_user_perm_enforced(self): self.test_user_util.add_user_to_group('data-analyst') fixture = AutoFixture(User) fixture.create(1) precreated_actor = Actor.objects.all()[0] precreated_actor.assigned_user = User.objects.get(id=2) precreated_actor.save() url = '/api/v1/actor/{0}/?format=json{1}'.format( precreated_actor.id, self.auth_string) put_data = create_put_data(4) put_data['assigned_user'] = '******' response = self.api_client.put(url, data=put_data) assigned_user_id = retrieve_user_id(response) self.assertEqual(assigned_user_id, 2) self.test_user_util.add_user_to_group('chief-data-analyst') response = self.api_client.put(url, data=put_data) assigned_user_id = retrieve_user_id(response) self.assertEqual(assigned_user_id, 1) def check_dehydrated_data(self, response): """ Test that returned data contains required dehydrated fields. New fields should be added to this method as they are added to the API to ensure that consistency between front and back end. """ dehydrate_keys = { 'related_bulletins', 'related_incidents', 'count_incidents', 'count_bulletins', 'roles', 'actors_role', 'actors', 'thumbnail_url', 'actor_roles_status', 'most_recent_status_actor' } content = json.loads(response.content) self.assertEqual(all(test in content for test in dehydrate_keys), True)
class IncidentTestCase(ResourceTestCase): fixtures = [ 'test_data_role.json', 'status_update', ] def setUp(self): super(IncidentTestCase, self).setUp() self.test_user_util = TestUserUtility() self.user = self.test_user_util.user self.location = Location(name_en='test location', loc_type='Village') self.location.save() self.actor = Actor(fullname_en='Test Actor', fullname_ar='Test name ar', nickname_en='nick name', nickname_ar='nick name') self.actor.save() self.role = ActorRole(role_status='Detained', actor_id=self.actor.pk) self.role.save() self.statusUpdate = StatusUpdate(status_en='test status') self.statusUpdate.save() self.crimeCategory = CrimeCategory( name_en='test crime category', level=1, description_en='test source incident_details') self.crimeCategory.save() self.label = Label(name_en='test label') self.label.save() self.comment = Comment(assigned_user_id=self.user.pk, status_id=self.statusUpdate.pk, comments_en='test comment') self.comment.save() fixture = AutoFixture(Incident, generate_m2m={1, 5}) fixture.create(10) try: self.api_key = ApiKey.objects.get(user=self.user) except ApiKey.DoesNotExist: self.api_key = ApiKey.objects.create(user=self.user) self.auth_string = '&username={0}&api_key={1}'.format( self.user.username, self.api_key.key) def tearDown(self): User.objects.all().delete() Incident.objects.all().delete() Actor.objects.all().delete() ActorRole.objects.all().delete() Location.objects.all().delete() TimeInfo.objects.all().delete() Comment.objects.all().delete() StatusUpdate.objects.all().delete() CrimeCategory.objects.all().delete() def test_incident_get(self): url = '/api/v1/incident/?format=json{}'.format(self.auth_string) response = self.api_client.get(url) self.assertEqual(response.status_code, 200) unauth_url = '/api/v1/incident/?format=json' response = self.api_client.get(unauth_url) self.assertEqual(response.status_code, 401) def test_incident_post(self): post_data = { 'title_en': "Test Incident", 'incident_details_ar': "incident_details Arabic", 'confidence_score': 11, 'assigned_user': '******', 'incident_comments': [ '/api/v1/comment/1/', ], 'bulletins': [], 'actors_role': [], 'crimes': [], 'labels': [], 'times': [], 'locations': [], 'ref_incidents': [], 'status': '/api/v1/statusUpdate/1/', 'comment': 'Comment', 'status_uri': '/api/v1/statusUpdate/1/' } url = '/api/v1/incident/?format=json{}'.format(self.auth_string) response = self.api_client.post(url, data=post_data) self.assertEqual(response.status_code, 201) new_incident_dict = json.loads(response.content) new_incident = Incident(id=new_incident_dict['id']) incident_comments = new_incident.incident_comments.all() self.assertEqual(len(incident_comments), 1) vs = VersionStatus.objects.filter( user_id=self.user.id).order_by('version_timestamp') self.assertEqual(len(vs), 1) def test_incident_put(self): i = Incident.objects.all()[0] url = '/api/v1/incident/{0}/?format=json{1}'.format( i.id, self.auth_string) put_data = create_put_data() response = self.api_client.put(url, data=put_data) self.check_dehydrated_data(response) self.assertEqual(response.status_code, 200) vs = VersionStatus.objects.filter( user_id=self.user.id).order_by('version_timestamp') self.assertEqual(len(vs), 1) def test_finalized_is_not_updated(self): precreated_incident = Incident.objects.all()[0] comment = Comment(assigned_user_id=1, comments_en='comment', status_id=5) comment.save() precreated_incident.incident_comments.add(comment) url = '/api/v1/incident/{0}/?format=json{1}'.format( precreated_incident.id, self.auth_string) put_data = create_put_data(4) response = self.api_client.put(url, data=put_data) self.assertEqual(response.status_code, 403) def test_assigned_user_perm_enforced(self): self.test_user_util.add_user_to_group('data-analyst') fixture = AutoFixture(User) fixture.create(1) precreated_incident = Incident.objects.all()[0] precreated_incident.assigned_user = User.objects.get(id=2) precreated_incident.save() url = '/api/v1/incident/{0}/?format=json{1}'.format( precreated_incident.id, self.auth_string) put_data = create_put_data(4) response = self.api_client.put(url, data=put_data) assigned_user_id = retrieve_user_id(response) self.assertEqual(assigned_user_id, 2) self.test_user_util.add_user_to_group('chief-data-analyst') response = self.api_client.put(url, data=put_data) assigned_user_id = retrieve_user_id(response) self.assertEqual(assigned_user_id, 1) def check_dehydrated_data(self, response): """ Test that returned data contains required dehydrated fields. New fields should be added to this method as they are added to the API to ensure that consistency between front and back end. """ dehydrate_keys = [ 'incident_locations', 'incident_labels', 'incident_times', 'incident_crimes', 'most_recent_status_incident', 'count_actors', 'count_bulletins', 'count_incidents', 'actor_roles_status', 'actors', 'actors_role' ] content = json.loads(response.content) self.assertEqual(all(test in content for test in dehydrate_keys), True)
class IncidentTestCase(ResourceTestCase): fixtures = ['test_data_role.json', 'status_update', ] def setUp(self): super(IncidentTestCase, self).setUp() self.test_user_util = TestUserUtility() self.user = self.test_user_util.user self.location = Location(name_en='test location', loc_type='Village') self.location.save() self.actor = Actor( fullname_en='Test Actor', fullname_ar='Test name ar', nickname_en='nick name', nickname_ar='nick name') self.actor.save() self.role = ActorRole(role_status='Detained', actor_id=self.actor.pk) self.role.save() self.statusUpdate = StatusUpdate(status_en='test status') self.statusUpdate.save() self.crimeCategory = CrimeCategory( name_en='test crime category', level=1, description_en='test source incident_details') self.crimeCategory.save() self.label = Label(name_en='test label') self.label.save() self.comment = Comment( assigned_user_id=self.user.pk, status_id=self.statusUpdate.pk, comments_en='test comment') self.comment.save() fixture = AutoFixture(Incident, generate_m2m={1, 5}) fixture.create(10) try: self.api_key = ApiKey.objects.get(user=self.user) except ApiKey.DoesNotExist: self.api_key = ApiKey.objects.create(user=self.user) self.auth_string = '&username={0}&api_key={1}'.format( self.user.username, self.api_key.key) def tearDown(self): User.objects.all().delete() Incident.objects.all().delete() Actor.objects.all().delete() ActorRole.objects.all().delete() Location.objects.all().delete() TimeInfo.objects.all().delete() Comment.objects.all().delete() StatusUpdate.objects.all().delete() CrimeCategory.objects.all().delete() def test_incident_get(self): url = '/api/v1/incident/?format=json{}'.format(self.auth_string) response = self.api_client.get(url) self.assertEqual(response.status_code, 200) unauth_url = '/api/v1/incident/?format=json' response = self.api_client.get(unauth_url) self.assertEqual(response.status_code, 401) def test_incident_post(self): post_data = { 'title_en': "Test Incident", 'incident_details_ar': "incident_details Arabic", 'confidence_score': 11, 'assigned_user': '******', 'incident_comments': ['/api/v1/comment/1/', ], 'bulletins': [], 'actors_role': [], 'crimes': [], 'labels': [], 'times': [], 'locations': [], 'ref_incidents': [], 'status': '/api/v1/statusUpdate/1/', 'comment': 'Comment', 'status_uri': '/api/v1/statusUpdate/1/' } url = '/api/v1/incident/?format=json{}'.format(self.auth_string) response = self.api_client.post(url, data=post_data) self.assertEqual(response.status_code, 201) new_incident_dict = json.loads(response.content) new_incident = Incident(id=new_incident_dict['id']) incident_comments = new_incident.incident_comments.all() self.assertEqual(len(incident_comments), 1) vs = VersionStatus.objects.filter(user_id=self.user.id).order_by('version_timestamp') self.assertEqual(len(vs), 1) def test_incident_put(self): i = Incident.objects.all()[0] url = '/api/v1/incident/{0}/?format=json{1}'.format( i.id, self.auth_string) put_data = create_put_data() response = self.api_client.put(url, data=put_data) self.check_dehydrated_data(response) self.assertEqual(response.status_code, 202) vs = VersionStatus.objects.filter(user_id=self.user.id).order_by('version_timestamp') self.assertEqual(len(vs), 1) def test_finalized_is_not_updated(self): precreated_incident = Incident.objects.all()[0] comment = Comment( assigned_user_id=1, comments_en='comment', status_id=5 ) comment.save() precreated_incident.incident_comments.add(comment) url = '/api/v1/incident/{0}/?format=json{1}'.format( precreated_incident.id, self.auth_string) put_data = create_put_data(4) response = self.api_client.put(url, data=put_data) self.assertEqual(response.status_code, 403) def test_assigned_user_perm_enforced(self): self.test_user_util.add_user_to_group('data-analyst') fixture = AutoFixture(User) fixture.create(1) precreated_incident = Incident.objects.all()[0] precreated_incident.assigned_user = User.objects.get(id=2) precreated_incident.save() url = '/api/v1/incident/{0}/?format=json{1}'.format( precreated_incident.id, self.auth_string) put_data = create_put_data(4) response = self.api_client.put(url, data=put_data) assigned_user_id = retrieve_user_id(response) self.assertEqual(assigned_user_id, 2) self.test_user_util.add_user_to_group('chief-data-analyst') response = self.api_client.put(url, data=put_data) assigned_user_id = retrieve_user_id(response) self.assertEqual(assigned_user_id, 1) def check_dehydrated_data(self, response): """ Test that returned data contains required dehydrated fields. New fields should be added to this method as they are added to the API to ensure that consistency between front and back end. """ dehydrate_keys = [ 'incident_locations', 'incident_labels', 'incident_times', 'incident_crimes', 'most_recent_status_incident', 'count_actors', 'count_bulletins', 'count_incidents', 'actor_roles_status', 'actors', 'actors_role' ] content = json.loads(response.content) self.assertEqual( all( test in content for test in dehydrate_keys ), True )
def setUp(self): super(BulletinTestCase, self).setUp() self.test_user_util = TestUserUtility() self.user = self.test_user_util.user self.location = Location(name_en='test location', loc_type='Village') self.location.save() self.actor = Actor( fullname_en='Test Actor', fullname_ar='Test name ar', nickname_en='nick name', nickname_ar='nick name' ) self.actor.save() self.role = ActorRole(role_status='Detained', actor_id=self.actor.pk) self.role.save() self.sourceType = SourceType( source_type='test source type', description='test source description' ) self.sourceType.save() self.source = Source( reliability_score=0, name_en='test source', source_type_id=self.sourceType.pk ) self.source.save() self.source = Source( reliability_score=0, name_en='test source 2', source_type_id=self.sourceType.pk ) self.source.save() self.label = Label(name_en='test label') self.label.save() self.comment = Comment( assigned_user_id=self.user.pk, status_id=3, comments_en='test comment' ) self.comment.save() self.media = Media( media_type='Video', name_en='test media', media_file='' ) self.media.save() fixture = AutoFixture(Bulletin, generate_m2m={1, 5}) fixture.create(10) try: self.api_key = ApiKey.objects.get(user=self.user) except ApiKey.DoesNotExist: self.api_key = ApiKey.objects.create(user=self.user) self.auth_string = '&username={0}&api_key={1}'.format( self.user.username, self.api_key.key)
class BulletinTestCase(ResourceTestCase): fixtures = ['status_update', ] def setUp(self): super(BulletinTestCase, self).setUp() self.test_user_util = TestUserUtility() self.user = self.test_user_util.user self.location = Location(name_en='test location', loc_type='Village') self.location.save() self.actor = Actor( fullname_en='Test Actor', fullname_ar='Test name ar', nickname_en='nick name', nickname_ar='nick name' ) self.actor.save() self.role = ActorRole(role_status='Detained', actor_id=self.actor.pk) self.role.save() self.sourceType = SourceType( source_type='test source type', description='test source description' ) self.sourceType.save() self.source = Source( reliability_score=0, name_en='test source', source_type_id=self.sourceType.pk ) self.source.save() self.source = Source( reliability_score=0, name_en='test source 2', source_type_id=self.sourceType.pk ) self.source.save() self.label = Label(name_en='test label') self.label.save() self.comment = Comment( assigned_user_id=self.user.pk, status_id=3, comments_en='test comment' ) self.comment.save() self.media = Media( media_type='Video', name_en='test media', media_file='' ) self.media.save() fixture = AutoFixture(Bulletin, generate_m2m={1, 5}) fixture.create(10) try: self.api_key = ApiKey.objects.get(user=self.user) except ApiKey.DoesNotExist: self.api_key = ApiKey.objects.create(user=self.user) self.auth_string = '&username={0}&api_key={1}'.format( self.user.username, self.api_key.key) def tearDown(self): User.objects.all().delete() Actor.objects.all().delete() ActorRole.objects.all().delete() Location.objects.all().delete() TimeInfo.objects.all().delete() Media.objects.all().delete() Comment.objects.all().delete() StatusUpdate.objects.all().delete() Bulletin.objects.all().delete() def test_bulletin_get(self): url = '/api/v1/bulletin/?format=json{}'.format(self.auth_string) response = self.api_client.get(url) self.assertEqual(response.status_code, 200) unauth_url = '/api/v1/bulletin/?format=json' response = self.api_client.get(unauth_url) self.assertEqual(response.status_code, 401) def test_bulletin_post(self): post_data = { 'title_en': "Test Bulletin", 'description_ar': "description Arabic", 'confidence_score': 73, 'sources': ['/api/v1/source/1/', ], 'bulletin_imported_comments': ['/api/v1/comment/1/', ], 'assigned_user': '******', 'actors_role': [], 'times': [], 'medias': [], 'locations': [], 'labels': [], 'ref_bulletins': [], 'comment': 'new bulletin', } url = '/api/v1/bulletin/?format=json{}'.format(self.auth_string) response = self.api_client.post(url, data=post_data) self.assertEqual(response.status_code, 201) new_bulletin_dict = json.loads(response.content) new_bulletin = Bulletin(id=new_bulletin_dict['id']) bulletin_comments = new_bulletin.bulletin_comments.all() self.assertEqual(len(bulletin_comments), 1) vs = VersionStatus.objects.filter( user_id=self.user.id).order_by('version_timestamp') self.assertEqual(len(vs), 1) def test_bulletin_put(self): self.test_user_util.add_user_to_group('data-analyst') b = Bulletin.objects.all()[1] url = '/api/v1/bulletin/{0}/?format=json{1}'.format( b.id, self.auth_string ) put_data = create_put_data(5) response = self.api_client.put(url, data=put_data) self.assertEqual(response.status_code, 202) self.check_dehydrated_data(response) self.assertEqual(retrieve_last_comment_status(response), 'Updated') vs = VersionStatus.objects.filter( user_id=self.user.id).order_by('version_timestamp') self.assertEqual(len(vs), 1) def test_data_entry_put(self): self.test_user_util.add_user_to_group('data-entry') b = Bulletin.objects.all()[0] url = '/api/v1/bulletin/{0}/?format=json{1}'.format( b.id, self.auth_string ) put_data = create_put_data(3) response = self.api_client.put(url, data=put_data) self.assertEqual(response.status_code, 202) self.assertEqual(retrieve_last_comment_status(response), 'Updated') b.assigned_user = None b.save() response = self.api_client.put(url, data=put_data) self.assertEqual(response.status_code, 403) def test_senior_data_analyst_put(self): self.test_user_util.add_user_to_group('senior-data-analyst') b = Bulletin.objects.all()[0] url = '/api/v1/bulletin/{0}/?format=json{1}'.format( b.id, self.auth_string ) put_data = create_put_data(4) response = self.api_client.put(url, data=put_data) self.assertEqual(response.status_code, 202) self.assertEqual(retrieve_last_comment_status(response), 'Reviewed') def test_chief_data_analyst_put(self): self.test_user_util.add_user_to_group('chief-data-analyst') b = Bulletin.objects.all()[0] url = '/api/v1/bulletin/{0}/?format=json{1}'.format( b.id, self.auth_string ) put_data = create_put_data(5) response = self.api_client.put(url, data=put_data) self.assertEqual(response.status_code, 202) self.assertEqual(retrieve_last_comment_status(response), 'Finalized') def test_finalized_is_not_updated(self): precreated_bulletin = Bulletin.objects.all()[0] comment = Comment( assigned_user_id=1, comments_en='comment', status_id=5 ) comment.save() precreated_bulletin.bulletin_comments.add(comment) url = '/api/v1/bulletin/{0}/?format=json{1}'.format( precreated_bulletin.id, self.auth_string) put_data = create_put_data(4) response = self.api_client.put(url, data=put_data) self.assertEqual(response.status_code, 403) def test_assigned_user_perm_enforced(self): self.test_user_util.add_user_to_group('data-analyst') fixture = AutoFixture(User) fixture.create(1) precreated_bulletin = Bulletin.objects.all()[0] precreated_bulletin.assigned_user = User.objects.get(id=2) precreated_bulletin.save() url = '/api/v1/bulletin/{0}/?format=json{1}'.format( precreated_bulletin.id, self.auth_string) put_data = create_put_data(4) response = self.api_client.put(url, data=put_data) assigned_user_id = retrieve_user_id(response) self.assertEqual(assigned_user_id, 2) self.test_user_util.add_user_to_group('chief-data-analyst') response = self.api_client.put(url, data=put_data) assigned_user_id = retrieve_user_id(response) self.assertEqual(assigned_user_id, 1) def check_dehydrated_data(self, response): """ Test that returned data contains required dehydrated fields. New fields should be added to this method as they are added to the API to ensure that consistency between front and back end. """ dehydrate_keys = [ 'bulletin_comments', 'bulletin_imported_comments', 'bulletin_locations', 'bulletin_labels', 'bulletin_times', 'bulletin_sources', 'most_recent_status_bulletin', 'count_actors', 'actor_roles_status', 'actors', 'actors_role' ] content = json.loads(response.content) self.assertEqual( all( test in content for test in dehydrate_keys ), True )