def test_complaint_get(self): """Test getting venter complaint lists.""" def create_complaint(user, **kwargs): Complaint.objects.create(created_by=user, **kwargs) # Creating 4 dummy complaints, one of which has a "DELETED" status create_complaint(self.user.profile) create_complaint(get_new_user().profile) create_complaint(get_new_user().profile) create_complaint(self.user.profile, status=STATUS_DELETED) url = '/api/venter/complaints' response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 3) url = '/api/venter/complaints?from=0&num=2' response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 2) url = '/api/venter/complaints?filter=me' response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1)
def test_comment_notifications(self): """Test notifications for complaint subscribers when comments are made""" # Creating dummy complaint with one subscriber (the creator) test_creator = get_new_user() complaint = Complaint.objects.create(created_by=test_creator.profile) complaint.subscriptions.add(test_creator.profile) # Comments url url = '/api/venter/complaints/' + str(complaint.id) + '/comments' # First comment on the new complaint, should generate 1 new notification (Total = 1) test_commenter_1 = get_new_user() self.client.force_authenticate(user=test_commenter_1) data = {'text': 'test'} response = self.client.post(url, data, format='json') self.assertEqual(response.status_code, 201) # Get notifications for test_creator. # There should be one notification from the first comment made by test_commenter_1 url = '/api/notifications' self.client.force_authenticate(user=test_creator) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]['actor']['commented_by']['id'], str(test_commenter_1.profile.id)) # Comment url url = '/api/venter/complaints/' + str(complaint.id) + '/comments' # Second comment on the complaint, should generate 2 new notifications. # One new notification each will be made for test_creator and test_commenter_1. # Both will be tested independently. test_commenter_2 = get_new_user() self.client.force_authenticate(user=test_commenter_2) data = {'text': 'test'} response = self.client.post(url, data, format='json') self.assertEqual(response.status_code, 201) # Get notifications for test_creator. # A total of two notifications should exist, one for each comment. # The most recent notification should be by the person who made the corresponding comment url = '/api/notifications' self.client.force_authenticate(user=test_creator) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 2) self.assertEqual(response.data[0]['actor']['commented_by']['id'], str(test_commenter_2.profile.id)) # Get notifications for test_commenter_1. # 1 notification should exist due to the comment made by test_commenter_2 url = '/api/notifications' self.client.force_authenticate(user=test_commenter_1) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]['actor']['commented_by']['id'], str(test_commenter_2.profile.id))
def test_comment(self): """Test all public venter comment APIs.""" # Dummy complaint, created by 'user'. The creator is already subscribed to the complaint complaint = Complaint.objects.create(created_by=self.user.profile) complaint.subscriptions.add(self.user.profile) # Creating a new user and profile to make a comment and test auto-subscription of comments test_commenter_1 = get_new_user() self.client.force_authenticate(user=test_commenter_1) url = '/api/venter/complaints/' + str(complaint.id) + '/comments' data = {'text': 'test'} response = self.client.post(url, data, format='json') self.assertEqual(response.status_code, 201) cid = str(response.data['id']) # Testing whether commenter is auto-subscribed to the complaint, and is the most recent subscriber url = '/api/venter/complaints/' + str(complaint.id) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['is_subscribed'], True) url = '/api/venter/comments/' + cid response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['text'], 'test') data = {'text': 'test2'} response = self.client.put(url, data, format='json') self.assertEqual(response.status_code, 200) self.assertEqual(response.data['text'], 'test2') response = self.client.delete(url, format='json') self.assertEqual(response.status_code, 204) comment = ComplaintComment.objects.create( complaint=complaint, text='test_comment', commented_by=get_new_user().profile ) url = '/api/venter/comments/' + str(comment.id) response = self.client.put(url, data, format='json') self.assertEqual(response.status_code, 403) response = self.client.delete(url, format='json') self.assertEqual(response.status_code, 403)
def test_comment(self): """Test all public venter comment APIs.""" complaint = Complaints.objects.create(created_by=self.user.profile) url = '/api/venter/complaints/' + str(complaint.id) + '/comments' data = {'text': 'test'} response = self.client.post(url, data, format='json') self.assertEqual(response.status_code, 201) cid = str(response.data['id']) url = '/api/venter/comments/' + cid response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['text'], 'test') data = {'text': 'test2'} response = self.client.put(url, data, format='json') self.assertEqual(response.status_code, 200) self.assertEqual(response.data['text'], 'test2') response = self.client.delete(url, format='json') self.assertEqual(response.status_code, 204) comment = Comment.objects.create(complaint=complaint, text='test_comment', commented_by=get_new_user().profile) url = '/api/venter/comments/' + str(comment.id) response = self.client.put(url, data, format='json') self.assertEqual(response.status_code, 403) response = self.client.delete(url, format='json') self.assertEqual(response.status_code, 403)
def setUp(self): # Fake authenticate self.user = get_new_user() self.client.force_authenticate(self.user) # pylint: disable=E1101 self.test_body_1 = Body.objects.create(name="TestBody1") self.test_body_2 = Body.objects.create(name="TestBody2") self.body_1_role = BodyRole.objects.create( name="Body1Role", body=self.test_body_1, permissions='AddE,UpdE,DelE') self.user.profile.roles.add(self.body_1_role) self.update_test_event = Event.objects.create( name='TestEventUpdated', start_time=timezone.now() - timedelta(days=1), end_time=timezone.now() - timedelta(days=1)) url = '/api/events/' + str(self.update_test_event.id) self.update_event_data = self.client.get(url).data self.update_url = '/api/events/' + str(self.update_test_event.id) Event.objects.create(name='TestEvent2', start_time=timezone.now() - timedelta(days=1), end_time=timezone.now() - timedelta(days=1))
def setUp(self): # Fake authenticate self.user = get_new_user() self.client = APIClient() self.client.force_authenticate(self.user) self.insti_role = InstituteRole.objects.create(name='TestInstiRole', permissions=['AddB']) self.user.profile.institute_roles.add(self.insti_role) url = '/api/bodies' data = { 'name': 'TestBody1', 'image_url': 'http://example.com/image.png' } response = self.client.post(url, data, format='json') self.assertEqual(response.status_code, 201) response.close() self.test_body_1_id = response.data['id'] test_body_1 = Body.objects.get(id=self.test_body_1_id) self.body_1_role = BodyRole.objects.create(name="Body1Role", body=test_body_1, permissions='AddE') self.user.profile.roles.add(self.body_1_role) data = { 'name': 'TestBody2', 'image_url': 'http://example.com/image2.png' } response = self.client.post(url, data, format='json') self.assertEqual(response.status_code, 201) response.close() self.test_body_2_id = response.data['id']
def test_upload(self): """Test if logged in users can upload files.""" # Try without authentication url = '/api/upload' data = { "picture": open('./upload/b64img.txt', 'r').read() } response = self.client.post(url, data, format='json') self.assertEqual(response.status_code, 401) # Fake authenticate and try again user = get_new_user() self.client.force_authenticate(user) # pylint: disable=E1101 response = self.client.post(url, data, format='json') self.assertEqual(response.status_code, 201) # Check if extension was guessed right img_id = response.data['id'] img = UploadedImage.objects.get(pk=img_id) self.assertIn('.png', str(img.picture)) # Check __str__ self.assertEqual(str(img), str(img.time_of_creation)) # Test delete url = '/api/upload/' + img_id response = self.client.delete(url, format='json') self.assertEqual(response.status_code, 204)
def setUp(self): # Create bodies body1 = create_body(name="Test WnCC", description="<script> var js = 'XSS'; </script>") body2 = create_body(name="Test MoodI") # Create dummy events event1 = create_event(name="Test Scratch") event2 = create_event(name="Test Scratch Wncc") event3 = create_event(name="Test Aaveg") # Create dummy users for search UserProfile.objects.create(name="Test User1") UserProfile.objects.create(name="Test User2") UserProfile.objects.create(name="Test User3", active=False) # Associate events with bodies event1.bodies.add(body1) event2.bodies.add(body1) event3.bodies.add(body2) # Fake authenticate self.user = get_new_user() self.profile = self.user.profile self.client = APIClient() self.client.force_authenticate(self.user)
def setUp(self): # Fake authenticate self.user = get_new_user() self.client = APIClient() self.client.force_authenticate(self.user) # A different user self.user_2 = get_new_user() # Dummy bodies and events self.body_1 = create_body() self.body_2 = create_body() self.event_1 = create_event() # Body roles self.body_1_role = BodyRole.objects.create( name="Body1Role", body=self.body_1, permissions='VerA,AddE')
def setUp(self): # Fake authenticate self.user = get_new_user() self.client.force_authenticate(self.user) # pylint: disable=E1101 self.body = Body.objects.create(name="Body1") self.bodyrole = BodyRole.objects.create( name="Role", body=self.body, permissions=['Role'], inheritable=True) self.instirole = InstituteRole.objects.create( name="InstiRole", permissions=['RoleB'])
def test_blog(obj, url, count): """Helper for testing authenticated blog endpoints.""" # Try without authentication response = obj.client.get(url) obj.assertEqual(response.status_code, 401) # Try after authentication user = get_new_user() obj.client.force_authenticate(user) # pylint: disable=E1101 response = obj.client.get(url) obj.assertEqual(response.status_code, 200) obj.assertEqual(len(response.data), count)
def test_body_get(self): """Test getting the body with id or str_id.""" body = Body.objects.create(name="Test #Body 123!") # Test GET by plain UUID url = '/api/bodies/' + str(body.id) response = self.client.get(url, format='json') self.assertEqual(response.status_code, 200) self.assertEqual(response.data['name'], body.name) # Test GET with str_id url = '/api/bodies/test-body-123' response = self.client.get(url, format='json') self.assertEqual(response.status_code, 200) self.assertEqual(response.data['name'], body.name) # Change the canonical name body.canonical_name = "Test Canonical" body.save() # Test GET with canonical name url = '/api/bodies/test-canonical' response = self.client.get(url, format='json') self.assertEqual(response.status_code, 200) self.assertEqual(response.data['str_id'], body.str_id) # Add some followers users = [get_new_user().profile for _ in range(10)] for user in users: user.followed_bodies.add(body) # Test follower count and user_follows response = self.client.get(url, format='json') self.assertEqual(response.status_code, 200) self.assertEqual(response.data['followers_count'], 10) self.assertEqual(response.data['user_follows'], False) # Add self as follower and try same thing self.user.profile.followed_bodies.add(body) response = self.client.get(url, format='json') self.assertEqual(response.status_code, 200) self.assertEqual(response.data['followers_count'], 11) self.assertEqual(response.data['user_follows'], True) # Mark a user as inactive and check users[0].active = False users[0].save() self.user.profile.followed_bodies.add(body) response = self.client.get(url, format='json') self.assertEqual(response.status_code, 200) self.assertEqual(response.data['followers_count'], 10) self.assertEqual(response.data['user_follows'], True)
def test_get_user_tags(self): """Test getting list of tags.""" cat1 = create_usertagcategory() cat2 = create_usertagcategory() # Create test users for matching profiles = [get_new_user().profile for x in range(6)] for profile in profiles: profile.department = 'ME' profiles[0].hostel = '1' profiles[1].hostel = '' profiles[1].roll_no = '2' profiles[2].hostel = '3' profiles[3].hostel = '1' profiles[3].department = 'EP' profiles[4].hostel = '2' profiles[4].roll_no = '2' profiles[5].hostel = '1' profiles[5].active = False for profile in profiles: profile.save() # Create tags in 2 categories t1 = create_usertag(cat1, '1') t2 = create_usertag(cat1, '1', target='hostel', secondary_target='roll_no', secondary_regex='2') t3 = create_usertag(cat2, 'ME', target='department') url = '/api/user-tags' response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 2) self.assertEqual(len(response.data[0]['tags']), 2) url = '/api/user-tags/reach' response = self.client.post(url, json.dumps([]), content_type="application/json") self.assertEqual(response.status_code, 200) self.assertGreaterEqual(response.data['count'], 4) data = [t1.id, t2.id, t3.id] response = self.client.post(url, json.dumps(data), content_type="application/json") self.assertEqual(response.status_code, 200) self.assertEqual(response.data['count'], 2)
def setUp(self): # Fake authenticate self.user = get_new_user() self.client.force_authenticate(self.user) # pylint: disable=E1101 self.test_body_1 = Body.objects.create(name="TestBody1") self.body_1_role = BodyRole.objects.create( name="Body1Role", body=self.test_body_1, permissions='AddE,UpdE,DelE') self.user.profile.roles.add(self.body_1_role) self.insti_role = InstituteRole.objects.create( name="InstiRole", permissions='Location') self.reusable_test_location = Location.objects.create( name='ReusableTestLocation', short_name='RTL', reusable=True)
def setUp(self): # Create bodies Body.objects.create(name="Test Body1") Body.objects.create(name="Test Body2") Event.objects.create(name="Test Event1", start_time=timezone.now(), end_time=timezone.now()) Event.objects.create(name="Test Event2 Body1", start_time=timezone.now(), end_time=timezone.now()) Event.objects.create(name="Test Event21", start_time=timezone.now(), end_time=timezone.now()) UserProfile.objects.create(name="Test User1") UserProfile.objects.create(name="Test User2") # Fake authenticate self.user = get_new_user() self.profile = self.user.profile self.client.force_authenticate(self.user) # pylint: disable=E1101
def setUp(self): # Fake authenticate self.user = get_new_user() self.client.force_authenticate(self.user) # pylint: disable=E1101 # Create bodies body1 = Body.objects.create(name="Body1") body2 = Body.objects.create(name="Body2") # Create dummies self.entry1 = NewsEntry.objects.create(title="PEntry1", blog_url="B1URL", body=body1) NewsEntry.objects.create(title="PEntry2", blog_url="B1URL", body=body1) NewsEntry.objects.create(title="TEntry1", blog_url="B2URL", body=body2) NewsEntry.objects.create(title="TEntry2", blog_url="B2URL", body=body2) NewsEntry.objects.create(title="TEntry3", blog_url="B2URL", body=body2)
def test_complaint_get(self): """Test getting venter complaint lists.""" Complaints.objects.create(created_by=self.user.profile) Complaints.objects.create(created_by=get_new_user().profile) Complaints.objects.create(created_by=self.user.profile, status='Deleted') url = '/api/venter/complaints' response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 2) url = '/api/venter/complaints?filter=me' response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1)
def test_delete_body_role(self): """Check we can delete roles.""" bodyrole1 = BodyRole.objects.create(name="Role1", body=self.body, permissions=['Role']) bodyrole2 = BodyRole.objects.create(name="Role2", body=self.body, permissions=['Role']) bodyrole3 = BodyRole.objects.create(name="Role3", body=self.body, permissions=['Role']) # Check without role url = '/api/roles/' + str(bodyrole2.id) response = self.client.delete(url) self.assertEqual(response.status_code, 403) # Check with body role self.user.profile.roles.add(bodyrole1) response = self.client.delete(url) self.assertEqual(response.status_code, 204) # Check with former user url = '/api/roles/' + str(bodyrole3.id) profile = get_new_user().profile frole = UserFormerRole.objects.create(user=profile, role=bodyrole3, year='2019') response = self.client.delete(url) self.assertEqual(response.status_code, 403) frole.delete() response = self.client.delete(url) self.assertEqual(response.status_code, 204) # Remove role self.user.profile.roles.remove(bodyrole1) # Check with institute role self.user.profile.institute_roles.add(self.instirole) url = '/api/roles/' + str(bodyrole1.id) response = self.client.delete(url) self.assertEqual(response.status_code, 204) self.user.profile.institute_roles.remove(self.instirole)
def setUp(self): # Fake authenticate self.user = get_new_user() self.client.force_authenticate(self.user) # pylint: disable=E1101 self.test_body_1 = create_body() self.test_body_2 = create_body() self.body_1_role = BodyRole.objects.create( name="Body1Role", body=self.test_body_1, permissions='AddE,UpdE,DelE') self.user.profile.roles.add(self.body_1_role) self.update_test_event = create_event(-24, -24) url = '/api/events/%s' % self.update_test_event.id self.update_event_data = self.client.get(url).data self.update_url = '/api/events/%s' % self.update_test_event.id create_event(-24, -24)
def setUp(self): # Start mock server self.mock_server = Popen(['python', 'news/test/test_server.py']) # Fake authenticate self.user = get_new_user() self.client.force_authenticate(self.user) # pylint: disable=E1101 # Create bodies self.body1 = Body.objects.create(name="Body1") body2 = Body.objects.create(name="Body2") # Create dummies self.entry1 = NewsEntry.objects.create(title="PEntry1", blog_url="B1URL", body=self.body1) NewsEntry.objects.create(title="PEntry2", blog_url="B1URL", body=self.body1) NewsEntry.objects.create(title="TEntry1", blog_url="B2URL", body=body2) NewsEntry.objects.create(title="TEntry2", blog_url="B2URL", body=body2) NewsEntry.objects.create(title="TEntry3", blog_url="B2URL", body=body2)
def setUp(self): # Create bodies body1 = create_body(name="Test Body1") body2 = create_body(name="Test Body2") # Create dummy events event1 = create_event(name="Test Event1") event2 = create_event(name="Test Event2 Body1") event3 = create_event(name="Test Event21") # Create dummy users for search UserProfile.objects.create(name="Test User1") UserProfile.objects.create(name="Test User2") # Associate events with bodies event1.bodies.add(body1) event2.bodies.add(body1) event3.bodies.add(body2) # Fake authenticate self.user = get_new_user() self.profile = self.user.profile self.client.force_authenticate(self.user) # pylint: disable=E1101
def test_inactive_chore(self): """Test the chore for marking users inactive.""" def refresh(objs): _ = [obj.refresh_from_db() for obj in objs] return objs users = [get_new_user().profile for _ in range(10)] # Check if nothing is affected call_command('mark-users-inactive') self.assertEqual(len([user for user in refresh(users) if user.active]), 10) # Mark two users inactive, one almost inactive users[0].last_ping = users[1].last_ping = timezone.now() - timedelta( days=370) users[2].last_ping = users[3].last_ping = timezone.now() - timedelta( days=360) _ = [user.save() for user in users] # Check if two are inactive now call_command('mark-users-inactive') self.assertEqual(len([user for user in refresh(users) if user.active]), 8)
def setUp(self): # Fake authenticate self.user = get_new_user() self.client.force_authenticate(self.user) # pylint: disable=E1101 self.test_body = Body.objects.create(name="test")
def test_placements_chore(self): """Test the placement blog chore.""" # Clear table BlogEntry.objects.all().delete() # Create blog bodies placement_body = create_body(name=settings.PLACEMENTS_BLOG_BODY) training_body = create_body(name=settings.TRAINING_BLOG_BODY) # Create users to follow blogs second_year = get_new_user() final_year = get_new_user() mentioned_user = get_new_user() mentioned_dual = get_new_user() second_year.profile.followed_bodies.add(training_body) final_year.profile.followed_bodies.add(placement_body) mentioned_user.profile.roll_no = '160010005' mentioned_user.profile.save() mentioned_dual.profile.roll_no = '150040010' mentioned_dual.profile.save() # Start mock server mock_server = Popen(['python', 'news/test/test_server.py']) time.sleep(1) # Run the placement chore call_command('placement_blog_chore') # Check if posts were collected placements = lambda: BlogEntry.objects.all().filter(blog_url=settings.PLACEMENTS_URL) trainings = lambda: BlogEntry.objects.all().filter(blog_url=settings.TRAINING_BLOG_URL) self.assertEqual(placements().count(), 3) self.assertEqual(trainings().count(), 5) self.assertEqual(set(x.guid for x in placements()), set('sample:p:%i' % i for i in range(1, 4))) self.assertEqual(set(x.title for x in placements()), set('Placement Item %i' % i for i in range(1, 4))) # Check if following blogs works self.assertEqual(second_year.notifications.count(), 5) self.assertEqual(final_year.notifications.count(), 3) # Check if mentioned users got a notification self.assertEqual(mentioned_user.notifications.count(), 1) self.assertEqual(mentioned_user.notifications.first().actor.title, 'Mentioning Item') self.assertEqual(mentioned_dual.notifications.count(), 1) self.assertEqual(mentioned_dual.notifications.first().actor.title, 'Placement Item 1') # Update placement blog URL call_command('placement_blog_chore') # Check if new placement blog posts are got self.assertEqual(trainings().all().count(), 5) self.assertEqual(placements().all().count(), 4) self.assertEqual(set(x.guid for x in placements()), set('sample:p:%i' % i for i in range(1, 5))) self.assertEqual(set(x.title for x in placements()), set('Placement Item %i' % i for i in range(1, 5))) # Check if existing ones are updated self.assertEqual(BlogEntry.objects.get(guid='sample:p:1').content, 'Updated') # Check if notification counts are updated self.assertEqual(second_year.notifications.count(), 5) self.assertEqual(final_year.notifications.count(), 4) self.assertEqual(mentioned_user.notifications.count(), 2) self.assertEqual(mentioned_dual.notifications.count(), 2) # Stop server mock_server.terminate()
def setUp(self): self.user = get_new_user() self.client.force_login(self.user)
def setUp(self): # Fake authenticate self.user = get_new_user() self.client.force_login(self.user) self.test_body = Body.objects.create(name="test")
def setUp(self): self.client = APIClient() self.user = get_new_user() self.client.force_authenticate(self.user)