class EventTest(EventBaseTest): """Tests for hosts """ def setUp(self): super(EventTest, self).setUp() self.source_host = Host(name='Host', ipv4='1.2.3.4', user=self.user,timezone = 'Asia/Kolkata') self.source_host.save() event_type = EventType(name='INFO', user=self.user) event_type.save() event_data = { 'message': 'Message', 'short_message': 'short message', 'event_type': event_type, 'timestamp': '%s' % str(datetime.datetime.now()), 'source_host': self.source_host } self.event = Event.objects.create(**event_data) def test_event_detail(self): """Get event's details """ url = reverse('event_detail', kwargs={'object_id': self.event.pk}) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertIn('object', response.context) self.assertIn('check_form', response.context) def test_event_list(self): """Get events list """ response = self.client.get(reverse('events_list')) self.assertEqual(response.status_code, 200) self.assertIn('events', response.context) def test_shared_event_detail(self): """ Only a user that has access to a source host should be able to see event's details """ other_user = self.create_user('other', 'otherpassword') self.source_host.user = other_user self.source_host.save() url = reverse('event_detail', kwargs={'object_id': self.event.pk}) response = self.client.get(url) self.assertEqual(response.status_code, 404) self.source_host.share(self.user) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.source_host.user = self.user self.source_host.save()
class ShareTest(TestCase): """Tests for class-based permissions system """ def setUp(self): self.owner = self.create_user('owner', 'pass') self.friend = self.create_user('friend', 'pass') self.host = Host(name='Host', ipv4='1.2.3.4', user=self.owner) self.host.save() def create_user(self, username, password, email=None): """Creates and returns User object """ if not email: email = '*****@*****.**' % username user = User.objects.create_user(username, email, password) user.save() return user def test_has_access(self): """By default only the owner should has an access to the object """ self.assertEqual(self.host.has_access(self.owner), True) self.assertEqual(self.host.has_access(self.friend), False) def test_share(self): """The share() method grants user an access to the object """ self.assertEqual(self.host.has_access(self.friend), False) self.host.share(self.friend) self.assertEqual(self.host.has_access(self.friend), True) def test_revoke(self): """The revoke() method revokes user an access to the object """ self.host.share(self.friend) self.assertEqual(self.host.has_access(self.friend), True) self.host.revoke(self.friend) self.assertEqual(self.host.has_access(self.friend), False) def test_can_edit(self): """ The can_edit() method should return True if user has permission to edit the object; otherwise it should return False """ self.assertEqual(self.host.can_edit(self.owner), True) self.assertEqual(self.host.can_edit(self.friend), False) def test_grant_edit(self): """ The share() method grants permission to edit if the 'edit' parameter was set to True """ self.host.share(self.friend) self.assertEqual(self.host.can_edit(self.friend), False) self.host.share(self.friend, edit=True) self.assertEqual(self.host.can_edit(self.friend), True) def test_revoke_edit(self): """ The share() method revokes permission to edit if the 'edit' parameter was set to False """ self.host.share(self.friend, edit=True) self.assertEqual(self.host.can_edit(self.friend), True) self.host.share(self.friend, edit=False) self.assertEqual(self.host.can_edit(self.friend), False) def test_shared_objects(self): """ The shared_objects() method should return list of objects owned or shared by the user """ self.assertIn(self.host, Host.shared_objects(self.owner)) self.assertNotIn(self.host, Host.shared_objects(self.friend)) self.host.share(self.friend) self.assertIn(self.host, Host.shared_objects(self.friend)) def test_sharing_users(self): """ The sharing_users() method should return list of users who share the object """ self.assertNotIn(self.friend, self.host.sharing_users()) self.host.share(self.friend) self.assertIn(self.friend, self.host.sharing_users())