<commit_before>import time
import unittest

from django.contrib.auth import get_user_model

from grd.functional_tests.common import BaseTestCase
from grd.models import Agent, Device, Event


User = get_user_model()


class AllocateTest(BaseTestCase):
    # TODO fixture with a registered device
    def setUp(self):
        super(AllocateTest, self).setUp()
        
        # Initialize registered devices
        data = {
            'device': {
                'id': '//xsr.cat/device/1234',
                'hid': 'XPS13-1111-2222',
                'type': 'Computer',
            },
            'date': '2012-04-10T22:38:20.604391Z',
            'byUser': 'foo',
            'components': [{'id': '1', 'hid': 'DDR3', 'type': 'Monitor'}],
        }
        response = self.client.post('/api/devices/register/', data=data)
        self.assertEqual(201, response.status_code, response.content)
        event_url = response['Location']
        self.device_url = self.client.get(event_url).data['device']
    
    def test_allocate_device(self):
        # Bob wants to allocate a device to Alice.
        data = {
            'date': '2015-09-02T10:00:00.000000Z',
            'byUser': 'Bob',
            'owner': 'http://example.org/user/1',
        }
        response = self.client.post(self.device_url + 'allocate/', data=data)
        self.assertEqual(201, response.status_code, response.content)
        new_event_url = response['Location']
        
        # He checks that the device event includes the new event
        response = self.client.get(self.device_url + 'events/')
        self.assertEqual(200, response.status_code, response.content)
        events = response.data
        self.assertEqual(len(events), 2)
        
        # He checks the last event
        self.assertEventType(new_event_url, 'Allocate')
        
        # validate Event's data
        event = self.client.get(new_event_url).data
        self.assertEqual(event['owner'], data['owner'])
        
        # validate Device's owner
        device = self.client.get(self.device_url).data
        self.assertIn(event['owner'], device['owners'])
        
        # XXX def test_allocate_multiple_users(self):
    
    def test_allocate_twice_same_user_to_device(self):
        # Bob wants to allocate a device to Alice.
        data = {
            'date': '2015-09-02T10:00:00.000000Z',
            'byUser': 'Bob',
            'owner': 'http://example.org/user/alice/',
        }
        response = self.client.post(self.device_url + 'allocate/', data=data)
        self.assertEqual(201, response.status_code, response.content)
        
        # Bob tries to allocate again the device to Alice.
        response = self.client.post(self.device_url + 'allocate/', data=data)<commit_msg>Test `allocate` multiple users.<commit_after>import time
import unittest

from django.contrib.auth import get_user_model

from grd.functional_tests.common import BaseTestCase
from grd.models import Agent, Device, Event


User = get_user_model()


class AllocateTest(BaseTestCase):
    # TODO fixture with a registered device
    def setUp(self):
        super(AllocateTest, self).setUp()
        
        # Initialize registered devices
        data = {
            'device': {
                'id': '//xsr.cat/device/1234',
                'hid': 'XPS13-1111-2222',
                'type': 'Computer',
            },
            'date': '2012-04-10T22:38:20.604391Z',
            'byUser': 'foo',
            'components': [{'id': '1', 'hid': 'DDR3', 'type': 'Monitor'}],
        }
        response = self.client.post('/api/devices/register/', data=data)
        self.assertEqual(201, response.status_code, response.content)
        event_url = response['Location']
        self.device_url = self.client.get(event_url).data['device']
    
    def test_allocate_device(self):
        # Bob wants to allocate a device to Alice.
        data = {
            'date': '2015-09-02T10:00:00.000000Z',
            'byUser': 'Bob',
            'owner': 'http://example.org/user/1',
        }
        response = self.client.post(self.device_url + 'allocate/', data=data)
        self.assertEqual(201, response.status_code, response.content)
        new_event_url = response['Location']
        
        # He checks that the device event includes the new event
        response = self.client.get(self.device_url + 'events/')
        self.assertEqual(200, response.status_code, response.content)
        events = response.data
        self.assertEqual(len(events), 2)
        
        # He checks the last event
        self.assertEventType(new_event_url, 'Allocate')
        
        # validate Event's data
        event = self.client.get(new_event_url).data
        self.assertEqual(event['owner'], data['owner'])
        
        # validate Device's owner
        device = self.client.get(self.device_url).data
        self.assertIn(event['owner'], device['owners'])
        
    def test_allocate_multiple_users(self):
        # Bob wants to allocate the device to Alice.
        data = {
            'date': '2015-09-02T10:00:00.000000Z',
            'byUser': 'Bob',
            'owner': 'http://example.org/user/alice/',
        }
        response = self.client.post(self.device_url + 'allocate/', data=data)
        self.assertEqual(201, response.status_code, response.content)
        
        # Bob also wants to allocate the device to Brian.
        data2 = {
            'date': '2015-09-02T10:10:00.000000Z',
            'byUser': 'Bob',
            'owner': 'http://example.org/user/brian/',
        }
        response = self.client.post(self.device_url + 'allocate/', data=data2)
        self.assertEqual(201, response.status_code, response.content)
        
        # Both are Device's owners
        device = self.client.get(self.device_url).data
        self.assertIn(data['owner'], device['owners'])
        self.assertIn(data2['owner'], device['owners'])
    
    def test_allocate_twice_same_user_to_device(self):
        # Bob wants to allocate a device to Alice.
        data = {
            'date': '2015-09-02T10:00:00.000000Z',
            'byUser': 'Bob',
            'owner': 'http://example.org/user/alice/',
        }
        response = self.client.post(self.device_url + 'allocate/', data=data)
        self.assertEqual(201, response.status_code, response.content)
        
        # Bob tries to allocate again the device to Alice.
        response = self.client.post(self.device_url + 'allocate/', data=data)
{'fraction_non_alphanumeric': 0.12934481697939096, 'fraction_numerical': 0.0352199323285143, 'mean_word_length': 2.2417746759720836, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2481164', 'n_tokens_mistral': 2067, 'n_tokens_neox': 1887, 'n_words': 398}