示例#1
0
 def setUp(self):
     self.user_a = User.objects.create_user('user_a', '*****@*****.**', 'userpassword')
     self.user_a.save()
     
     self.user_b = User.objects.create_user('user_b', '*****@*****.**', 'userpassword')
     self.user_b.save()
     
     self.host = Host(name='Host', ipv4='1.2.3.4', user=self.user_a)
     self.host.save()
示例#2
0
    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))
示例#3
0
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()
示例#4
0
 def setUp(self):
     self.user_a = User.objects.create_user('user_a', '*****@*****.**', 'userpassword')
     self.user_a.save()
     
     self.user_b = User.objects.create_user('user_b', '*****@*****.**', 'userpassword')
     self.user_b.save()
     
     self.host = Host(name='Host', ipv4='1.2.3.4', user=self.user_a)
     self.host.save()
示例#5
0
 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)
示例#6
0
    def setUp(self):
        self.client = Client()
        self.user = User.objects.create_user('user', '*****@*****.**',
                                             'userpassword')
        self.client.login(username='******', password='******')

        self.host = Host(name='Host',
                         description='Description',
                         ipv4='1.2.3.4',
                         ipv6='',
                         user=self.user)
        self.host.save()

        self.network = Network(name='Network',
                               description='Description',
                               user=self.user)
        self.network.save()

        self.net_host = NetworkHost(network=self.network, host=self.host)
        self.net_host.save()
示例#7
0
 def setUp(self):
     self.username = '******'
     self.password = '******'
     
     #set up Django user
     self.user = User(username=self.username)
     self.user.set_password(self.password)
     self.user.save()
     
     #set up Django testing client
     self.client = Client()
     self.client.login(username=self.username, password=self.password)
     
     #set up events types
     for name in ['INFO', 'WARNING', 'CRITICAL', 'ERROR']:
         event_type = EventType(name=name, user=self.user,
                                alert_level=1)
         event_type.save()
     
     #set up some hosts
     for i in xrange(10):
         h = Host(name='host_%i' % i,
                  description='description number %i' % i,
                  ipv4='127.0.0.%i' % (i+1),
                  ipv6='0:0:0:0:0:0:7f00:%i' % (i+1),
                  user=self.user)
         h.save()
         
     types = EventType.objects.all()
     hosts = Host.objects.all()
     for i in xrange(10):
         e = Event(message='event_%i' % i,
             short_message='short message #%i' % i,
             event_type=random.choice(types),
             protocol='SMTP',
             timestamp='%s' % str(datetime.datetime.now()),
             source_host = hosts[i],
             fields_class='Class%i' % i,
             fields_data=''
         )
         e.save()
示例#8
0
    def setUp(self):
        self.username = '******'
        self.password = '******'

        #set up Django user
        self.user = User(username=self.username)
        self.user.set_password(self.password)
        self.user.save()

        #set up Django testing client
        self.client = Client()
        self.client.login(username=self.username, password=self.password)

        #set up events types
        for name in ['INFO', 'WARNING', 'CRITICAL', 'ERROR']:
            event_type = EventType(name=name, user=self.user, alert_level=1)
            event_type.save()

        #set up some hosts
        for i in xrange(10):
            h = Host(name='host_%i' % i,
                     description='description number %i' % i,
                     ipv4='127.0.0.%i' % (i + 1),
                     ipv6='0:0:0:0:0:0:7f00:%i' % (i + 1),
                     user=self.user)
            h.save()

        types = EventType.objects.all()
        hosts = Host.objects.all()
        for i in xrange(10):
            e = Event(message='event_%i' % i,
                      short_message='short message #%i' % i,
                      event_type=random.choice(types),
                      protocol='SMTP',
                      timestamp='%s' % str(datetime.datetime.now()),
                      source_host=hosts[i],
                      fields_class='Class%i' % i,
                      fields_data='')
            e.save()
示例#9
0
 def setUp(self):
     self.client = Client()
     self.user = User.objects.create_user('user', '*****@*****.**',
                                          'userpassword')
     self.client.login(username='******', password='******')
     
     self.host = Host(name='Host', description='Description',
                      ipv4='1.2.3.4', ipv6='', user=self.user)
     self.host.save()
     
     self.network = Network(name='Network', description='Description',
                            user=self.user)
     self.network.save()
     
     self.net_host = NetworkHost(network=self.network, host=self.host)
     self.net_host.save()
示例#10
0
 def setUp(self):
     self.client = Client()
     self.user = User.objects.create_user('user', '*****@*****.**', 'userpassword')
     self.user.save()
     self.client.login(username='******', password='******')
     
     self.source_host = Host(name='Host', ipv4='1.2.3.4', user=self.user)
     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(**event_data)
     self.event.save()
示例#11
0
def get_event_data(request, event_dict):
    """
    Creates dictionary with parameters for Event's __init__ method. If needed
    function also creates host and event type and saves them. If function
    cannot find obligatory fields in event_dict, it raises EventParseError
    exception.
    """
    required_fields = [
        'timestamp', 'protocol', 'fields_class', 'event_type', 'description',
        'short_description'
    ]
    base_fields = required_fields + [
        'is_report', 'hostname', 'source_host_ipv6', 'source_host_ipv4'
    ]

    # make sure that event_dict contains all fields we need
    # (also make sure that these fields aren't empty)
    for field_name in required_fields:
        if field_name not in event_dict:
            raise EventParseError("Following field is not specified: %s" \
                                    % field_name)
        if not event_dict[field_name]:
            raise EventParseError("Following field must not be empty: %s" \
                                    % field_name)

    message = event_dict['description']
    short_message = event_dict['short_description']
    timestamp = event_dict['timestamp']
    protocol = event_dict['protocol']
    event_type_name = event_dict['event_type']
    fields_class = event_dict['fields_class']

    ipv4 = event_dict.get('source_host_ipv4')
    ipv6 = event_dict.get('source_host_ipv6')
    hostname = event_dict.get('hostname')

    try:
        if hostname:
            source_host = Host.objects.get(name=hostname, user=request.user)
        else:
            if ipv4 and ipv6:
                source_host = Host.objects.get(ipv4=ipv4,
                                               ipv6=ipv6,
                                               user=request.user)
            elif ipv4:
                source_host = Host.objects.get(ipv4=ipv4, user=request.user)
            elif ipv6:
                source_host = Host.objects.get(ipv6=ipv6, user=request.user)
            else:
                source_host = None
    except Host.DoesNotExist:
        source_host = Host(name=hostname,
                           ipv4=ipv4,
                           ipv6=ipv6,
                           user=request.user)
        source_host.save()

    try:
        event_type = EventType.objects.get(name=event_type_name,
                                           user=request.user)
    except EventType.DoesNotExist:
        event_type = EventType(name=event_type_name, user=request.user)
        event_type.save()

    fields_data_dict = {}
    for field in event_dict:
        if field not in base_fields:
            fields_data_dict[field] = event_dict[field]
    fields_data = json.dumps(fields_data_dict)

    event_data = {
        'message': message,
        'short_message': short_message,
        'timestamp': datetime.datetime.fromtimestamp(float(timestamp)),
        'protocol': protocol,
        'fields_class': fields_class,
        'fields_data': fields_data,
        'source_host': source_host,
        'event_type': event_type
    }

    return event_data
示例#12
0
class ReportMetaTest(TestCase):
    """Tests for reports meta"""
    
    def setUp(self):
        self.client = Client()
        self.user = User.objects.create_user('user', '*****@*****.**',
                                             'userpassword')
        self.client.login(username='******', password='******')
        
        self.host = Host(name='Host', description='Description',
                         ipv4='1.2.3.4', ipv6='', user=self.user)
        self.host.save()
        
        self.network = Network(name='Network', description='Description',
                               user=self.user)
        self.network.save()
        
        self.net_host = NetworkHost(network=self.network, host=self.host)
        self.net_host.save()
        
    def test_reportmeta_host_list(self):
        for i in xrange(10):
            report_meta = ReportMeta(name='report %i' % i, description='description',
                                     period=1,
                                     object_type=ContentType.objects.get_for_model(Host),
                                     object_id=self.host.pk, user=self.user)
            report_meta.save()
        response = self.client.get('/report/list/host/')
        self.assertEqual(response.status_code, 200)
        
    def test_reportmeta_network_list(self):
        for i in xrange(10):
            report_meta = ReportMeta(name='report %i' % i, description='description',
                                     period=1,
                                     object_type=ContentType.objects.get_for_model(Network),
                                     object_id=self.network.pk, user=self.user)
            report_meta.save()
        response = self.client.get('/report/list/network/')
        self.assertEqual(response.status_code, 200)
    
    def test_reportmeta_detail(self):
        report_meta = ReportMeta(name='host report', description='description',
                                 period=1,
                                 object_type=ContentType.objects.get_for_model(Host),
                                 object_id=self.host.pk, user=self.user)
        report_meta.save()
        response = self.client.get('/report/%i/' % report_meta.pk)
        self.assertEqual(response.status_code, 200)
        
        report_meta = ReportMeta(name='network report', description='description',
                                 period=1,
                                 object_type=ContentType.objects.get_for_model(Network),
                                 object_id=self.network.pk, user=self.user)
        report_meta.save()
        response = self.client.get('/report/%i/' % report_meta.pk)
        self.assertEqual(response.status_code, 200)
    
    def test_reportmeta_create(self):
        reportmeta_host_data = {
            'name': 'New host report',
            'description': 'New host report description',
            'period': 1,
            'send_hour': 12,
            'send_day_week': 1,
            'send_day_month': 20,
            'object_type': ContentType.objects.get_for_model(Host).pk,
            'object_id': self.host.pk,
            'user': self.user.pk
        }
        
        reportmeta_network_data = {
            'name': 'New network report',
            'description': 'New network report description',
            'period': 1,
            'send_hour': 12,
            'send_day_week': 1,
            'send_day_month': 20,
            'object_type': ContentType.objects.get_for_model(Network).pk,
            'object_id': self.network.pk,
            'user': self.user.pk
        }
        
        response = self.client.post('/report/new/host/', reportmeta_host_data)
        self.assertEqual(response.status_code, 302)
        
        response = self.client.post('/report/new/network/', reportmeta_network_data)
        self.assertEqual(response.status_code, 302)
    
    def test_reportmeta_update(self):
        reportmeta_host = ReportMeta(name='host report', description='description',
                                     period=1,
                                 object_type=ContentType.objects.get_for_model(Host),
                                 object_id=self.host.pk, user=self.user)
        reportmeta_host.save()
        
        reportmeta_net = ReportMeta(name='network report', description='description',
                            period=1,
                            object_type=ContentType.objects.get_for_model(Network),
                            object_id=self.network.pk, user=self.user)
        reportmeta_net.save()
        
        reportmeta_host_data = {
            'name': 'Updated host report',
            'description': 'Updated host report description',
            'period': 1,
            'send_hour': 12,
            'send_day_week': 1,
            'send_day_month': 20,
            'object_type': ContentType.objects.get_for_model(Host).pk,
            'object_id': self.host.pk,
            'user': self.user.pk
        }
        
        reportmeta_network_data = {
            'name': 'Updated network report',
            'description': 'Updated network report description',
            'period': 1,
            'send_hour': 12,
            'send_day_week': 1,
            'send_day_month': 20,
            'object_type': ContentType.objects.get_for_model(Network).pk,
            'object_id': self.network.pk,
            'user': self.user.pk
        }
        
        response = self.client.post('/report/edit/%i/' % reportmeta_host.pk, reportmeta_host_data)
        self.assertEqual(response.status_code, 302)
        response = self.client.post('/report/edit/%i/' % reportmeta_net.pk, reportmeta_network_data)
        self.assertEqual(response.status_code, 302)
    
    def test_reportmeta_delete(self):
        ct_host = ContentType.objects.get_for_model(Host)
        ct_network = ContentType.objects.get_for_model(Network)
        reportmeta_host = ReportMeta(name='host report', description='description',
                                     period=1, object_type=ct_host,
                                     object_id=self.host.pk, user=self.user)
        reportmeta_host.save()
        reportmeta_host_pk = reportmeta_host.pk
        
        reportmeta_net = ReportMeta(name='network report', description='description',
                                    period=1, object_type=ct_network,
                                    object_id=self.network.pk, user=self.user)
        reportmeta_net.save()
        reportmeta_net_pk = reportmeta_net.pk
        
        response = self.client.post('/report/delete/%i/' % reportmeta_host.pk)
        self.assertEqual(response.status_code, 302)
        self.assertRaises(ReportMeta.DoesNotExist, ReportMeta.objects.get,
                          **{'object_id': reportmeta_host_pk,
                             'object_type': ct_host})

        response = self.client.post('/report/delete/%i/' % reportmeta_net.pk)
        self.assertEqual(response.status_code, 302)
        self.assertRaises(ReportMeta.DoesNotExist, ReportMeta.objects.get,
                          **{'object_id': reportmeta_net_pk,
                             'object_type': ct_network})
示例#13
0
def get_event_data(request, event_dict):
    """
    Creates dictionary with parameters for Event's __init__ method. If needed
    function also creates host and event type and saves them. If function
    cannot find obligatory fields in event_dict, it raises EventParseError
    exception.
    """
    required_fields = ['timestamp', 'protocol', 'fields_class', 'event_type',
                       'description', 'short_description']
    base_fields = required_fields + ['is_report', 'hostname',
                                     'source_host_ipv6', 'source_host_ipv4']
    
    # make sure that event_dict contains all fields we need
    # (also make sure that these fields aren't empty)
    for field_name in required_fields:
        if field_name not in event_dict:
            raise EventParseError("Following field is not specified: %s" \
                                    % field_name)
        if not event_dict[field_name]:
            raise EventParseError("Following field must not be empty: %s" \
                                    % field_name)
        
    message = event_dict['description']
    short_message = event_dict['short_description']
    timestamp = event_dict['timestamp']
    protocol = event_dict['protocol']
    event_type_name = event_dict['event_type']
    fields_class = event_dict['fields_class']
    
    ipv4 = event_dict.get('source_host_ipv4')
    ipv6 = event_dict.get('source_host_ipv6')
    hostname = event_dict.get('hostname')
    
    try:
        if hostname:
            source_host = Host.objects.get(name=hostname, user=request.user)
        else:
            if ipv4 and ipv6:
                source_host = Host.objects.get(ipv4=ipv4, ipv6=ipv6,
                                               user=request.user)
            elif ipv4:
                source_host = Host.objects.get(ipv4=ipv4, user=request.user)
            elif ipv6:
                source_host = Host.objects.get(ipv6=ipv6, user=request.user)
            else:
                source_host = None
    except Host.DoesNotExist:
        source_host = Host(name=hostname, ipv4=ipv4, ipv6=ipv6,
                           user=request.user)
        source_host.save()
    
    try:
        event_type = EventType.objects.get(name=event_type_name,
                                           user=request.user)
    except EventType.DoesNotExist:
        event_type = EventType(name=event_type_name, user=request.user)
        event_type.save()
        
    fields_data_dict = {}
    for field in event_dict:
        if field not in base_fields:
            fields_data_dict[field] = event_dict[field]
    fields_data = json.dumps(fields_data_dict)
        
    event_data = {
        'message': message,
        'short_message': short_message,
        'timestamp': datetime.datetime.fromtimestamp(float(timestamp)),
        'protocol': protocol,
        'fields_class': fields_class,
        'fields_data': fields_data,
        'source_host': source_host,
        'event_type': event_type
    }
    
    return event_data
示例#14
0
class PermissionsTest(TestCase):
    """Tests for permissions
    """
    
    def setUp(self):
        self.user_a = User.objects.create_user('user_a', '*****@*****.**', 'userpassword')
        self.user_a.save()
        
        self.user_b = User.objects.create_user('user_b', '*****@*****.**', 'userpassword')
        self.user_b.save()
        
        self.host = Host(name='Host', ipv4='1.2.3.4', user=self.user_a)
        self.host.save()
        
    def test_user_access(self):
        access = user_has_access(self.host, self.user_a)
        self.assertEqual(access, True)
        
        access = user_has_access(self.host, self.user_b)
        self.assertEqual(access, False)
        
    def test_grant_access(self):
        access = user_has_access(self.host, self.user_b)
        self.assertEqual(access, False)
        
        grant_access(self.host, self.user_b)
        access = user_has_access(self.host, self.user_b)
        self.assertEqual(access, True)
        
    def test_revoke_access(self):
        grant_access(self.host, self.user_b)
        access = user_has_access(self.host, self.user_b)
        self.assertEqual(access, True)
        
        revoke_access(self.host, self.user_b)
        access = user_has_access(self.host, self.user_b)
        self.assertEqual(access, False)
        
    def test_can_edit(self):
        edit = user_can_edit(self.host, self.user_a)
        self.assertEqual(edit, True)
        
        revoke_access(self.host, self.user_b)
        edit = user_can_edit(self.host, self.user_b)
        self.assertEqual(edit, False)
        
        grant_access(self.host, self.user_b)
        edit = user_can_edit(self.host, self.user_b)
        self.assertEqual(edit, True)
        
    def test_revoke_edit(self):
        grant_access(self.host, self.user_b)
        edit = user_can_edit(self.host, self.user_b)
        self.assertEqual(edit, True)
        
        revoke_edit(self.host, self.user_b)
        edit = user_can_edit(self.host, self.user_b)
        self.assertEqual(edit, False)
        
        revoke_access(self.host, self.user_b)
        
    def test_grant_edit(self):
        grant_access(self.host, self.user_b)
        edit = user_can_edit(self.host, self.user_b)
        self.assertEqual(edit, True)
        
        revoke_edit(self.host, self.user_b)
        edit = user_can_edit(self.host, self.user_b)
        self.assertEqual(edit, False)
        
        grant_edit(self.host, self.user_b)
        edit = user_can_edit(self.host, self.user_b)
        self.assertEqual(edit, True)
示例#15
0
class PermissionsTest(TestCase):
    """Tests for permissions system
    """
    
    def setUp(self):
        self.user_a = User.objects.create_user('user_a', '*****@*****.**', 'userpassword')
        self.user_a.save()
        
        self.user_b = User.objects.create_user('user_b', '*****@*****.**', 'userpassword')
        self.user_b.save()
        
        self.host = Host(name='Host', ipv4='1.2.3.4', user=self.user_a)
        self.host.save()
        
    def test_user_access(self):
        access = user_has_access(self.host, self.user_a)
        self.assertEqual(access, True)
        
        access = user_has_access(self.host, self.user_b)
        self.assertEqual(access, False)
        
    def test_grant_access(self):
        access = user_has_access(self.host, self.user_b)
        self.assertEqual(access, False)
        
        grant_access(self.host, self.user_b)
        access = user_has_access(self.host, self.user_b)
        self.assertEqual(access, True)
        
    def test_revoke_access(self):
        grant_access(self.host, self.user_b)
        access = user_has_access(self.host, self.user_b)
        self.assertEqual(access, True)
        
        revoke_access(self.host, self.user_b)
        access = user_has_access(self.host, self.user_b)
        self.assertEqual(access, False)
        
    def test_can_edit(self):
        edit = user_can_edit(self.host, self.user_a)
        self.assertEqual(edit, True)
        
        revoke_access(self.host, self.user_b)
        edit = user_can_edit(self.host, self.user_b)
        self.assertEqual(edit, False)
        
        grant_access(self.host, self.user_b)
        edit = user_can_edit(self.host, self.user_b)
        self.assertEqual(edit, True)
        
    def test_revoke_edit(self):
        grant_access(self.host, self.user_b)
        edit = user_can_edit(self.host, self.user_b)
        self.assertEqual(edit, True)
        
        revoke_edit(self.host, self.user_b)
        edit = user_can_edit(self.host, self.user_b)
        self.assertEqual(edit, False)
        
        revoke_access(self.host, self.user_b)
        
    def test_grant_edit(self):
        grant_access(self.host, self.user_b)
        edit = user_can_edit(self.host, self.user_b)
        self.assertEqual(edit, True)
        
        revoke_edit(self.host, self.user_b)
        edit = user_can_edit(self.host, self.user_b)
        self.assertEqual(edit, False)
        
        grant_edit(self.host, self.user_b)
        edit = user_can_edit(self.host, self.user_b)
        self.assertEqual(edit, True)
示例#16
0
class ReportMetaTest(TestCase):
    """Tests for reports meta"""
    def setUp(self):
        self.client = Client()
        self.user = User.objects.create_user('user', '*****@*****.**',
                                             'userpassword')
        self.client.login(username='******', password='******')

        self.host = Host(name='Host',
                         description='Description',
                         ipv4='1.2.3.4',
                         ipv6='',
                         user=self.user)
        self.host.save()

        self.network = Network(name='Network',
                               description='Description',
                               user=self.user)
        self.network.save()

        self.net_host = NetworkHost(network=self.network, host=self.host)
        self.net_host.save()

    def test_reportmeta_host_list(self):
        for i in xrange(10):
            report_meta = ReportMeta(
                name='report %i' % i,
                description='description',
                period=1,
                object_type=ContentType.objects.get_for_model(Host),
                object_id=self.host.pk,
                user=self.user)
            report_meta.save()
        response = self.client.get('/report/list/host/')
        self.assertEqual(response.status_code, 200)

    def test_reportmeta_network_list(self):
        for i in xrange(10):
            report_meta = ReportMeta(
                name='report %i' % i,
                description='description',
                period=1,
                object_type=ContentType.objects.get_for_model(Network),
                object_id=self.network.pk,
                user=self.user)
            report_meta.save()
        response = self.client.get('/report/list/network/')
        self.assertEqual(response.status_code, 200)

    def test_reportmeta_detail(self):
        report_meta = ReportMeta(
            name='host report',
            description='description',
            period=1,
            object_type=ContentType.objects.get_for_model(Host),
            object_id=self.host.pk,
            user=self.user)
        report_meta.save()
        response = self.client.get('/report/%i/' % report_meta.pk)
        self.assertEqual(response.status_code, 200)

        report_meta = ReportMeta(
            name='network report',
            description='description',
            period=1,
            object_type=ContentType.objects.get_for_model(Network),
            object_id=self.network.pk,
            user=self.user)
        report_meta.save()
        response = self.client.get('/report/%i/' % report_meta.pk)
        self.assertEqual(response.status_code, 200)

    def test_reportmeta_create(self):
        reportmeta_host_data = {
            'name': 'New host report',
            'description': 'New host report description',
            'period': 1,
            'send_hour': 12,
            'send_day_week': 1,
            'send_day_month': 20,
            'object_type': ContentType.objects.get_for_model(Host).pk,
            'object_id': self.host.pk,
            'user': self.user.pk
        }

        reportmeta_network_data = {
            'name': 'New network report',
            'description': 'New network report description',
            'period': 1,
            'send_hour': 12,
            'send_day_week': 1,
            'send_day_month': 20,
            'object_type': ContentType.objects.get_for_model(Network).pk,
            'object_id': self.network.pk,
            'user': self.user.pk
        }

        response = self.client.post('/report/new/host/', reportmeta_host_data)
        self.assertEqual(response.status_code, 302)

        response = self.client.post('/report/new/network/',
                                    reportmeta_network_data)
        self.assertEqual(response.status_code, 302)

    def test_reportmeta_update(self):
        reportmeta_host = ReportMeta(
            name='host report',
            description='description',
            period=1,
            object_type=ContentType.objects.get_for_model(Host),
            object_id=self.host.pk,
            user=self.user)
        reportmeta_host.save()

        reportmeta_net = ReportMeta(
            name='network report',
            description='description',
            period=1,
            object_type=ContentType.objects.get_for_model(Network),
            object_id=self.network.pk,
            user=self.user)
        reportmeta_net.save()

        reportmeta_host_data = {
            'name': 'Updated host report',
            'description': 'Updated host report description',
            'period': 1,
            'send_hour': 12,
            'send_day_week': 1,
            'send_day_month': 20,
            'object_type': ContentType.objects.get_for_model(Host).pk,
            'object_id': self.host.pk,
            'user': self.user.pk
        }

        reportmeta_network_data = {
            'name': 'Updated network report',
            'description': 'Updated network report description',
            'period': 1,
            'send_hour': 12,
            'send_day_week': 1,
            'send_day_month': 20,
            'object_type': ContentType.objects.get_for_model(Network).pk,
            'object_id': self.network.pk,
            'user': self.user.pk
        }

        response = self.client.post('/report/edit/%i/' % reportmeta_host.pk,
                                    reportmeta_host_data)
        self.assertEqual(response.status_code, 302)
        response = self.client.post('/report/edit/%i/' % reportmeta_net.pk,
                                    reportmeta_network_data)
        self.assertEqual(response.status_code, 302)

    def test_reportmeta_delete(self):
        ct_host = ContentType.objects.get_for_model(Host)
        ct_network = ContentType.objects.get_for_model(Network)
        reportmeta_host = ReportMeta(name='host report',
                                     description='description',
                                     period=1,
                                     object_type=ct_host,
                                     object_id=self.host.pk,
                                     user=self.user)
        reportmeta_host.save()
        reportmeta_host_pk = reportmeta_host.pk

        reportmeta_net = ReportMeta(name='network report',
                                    description='description',
                                    period=1,
                                    object_type=ct_network,
                                    object_id=self.network.pk,
                                    user=self.user)
        reportmeta_net.save()
        reportmeta_net_pk = reportmeta_net.pk

        response = self.client.post('/report/delete/%i/' % reportmeta_host.pk)
        self.assertEqual(response.status_code, 302)
        self.assertRaises(
            ReportMeta.DoesNotExist, ReportMeta.objects.get, **{
                'object_id': reportmeta_host_pk,
                'object_type': ct_host
            })

        response = self.client.post('/report/delete/%i/' % reportmeta_net.pk)
        self.assertEqual(response.status_code, 302)
        self.assertRaises(
            ReportMeta.DoesNotExist, ReportMeta.objects.get, **{
                'object_id': reportmeta_net_pk,
                'object_type': ct_network
            })
示例#17
0
class EventTest(TestCase):
    """Tests for hosts
    """
    
    def setUp(self):
        self.client = Client()
        self.user = User.objects.create_user('user', '*****@*****.**', 'userpassword')
        self.user.save()
        self.client.login(username='******', password='******')
        
        self.source_host = Host(name='Host', ipv4='1.2.3.4', user=self.user)
        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(**event_data)
        self.event.save()
        
    def test_event_detail(self):
        """Get event's details
        """
        url = '/event/%i/' % self.event.pk
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

    def test_event_list(self):
        """Get events list
        """
        url = '/event/list/'
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        
    def test_shared_event_detail(self):
        """
        Make Other User the owner of the source host and then:
        
            1. Make sure that User hasn't access to the event
            2. Share source host with User and check if he has access
               to the event.
        """
        other_user =  User.objects.create_user('other', '*****@*****.**',
                                               'otherpassword')
        other_user.save()
        
        self.source_host.user = other_user
        self.source_host.save()
        
        url = '/event/%i/' % self.event.pk
        response = self.client.get(url)
        self.assertEqual(response.status_code, 404)
        
        grant_access(self.source_host, self.user)
        
        url = '/event/%i/' % self.event.pk
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        
        self.source_host.user = self.user
        self.source_host.save()
示例#18
0
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())
示例#19
0
    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()