def test_getting_list_of_clients_subscribed_events(self):
     """Fetch a list of a clients subscribed events."""
     # Create an event that the client is not subscribed to
     r = self.client.post('/events',
                          data=json.dumps(not_subscribed_event),
                          content_type='application/json')
     d1 = json.loads(r.data.decode('utf-8'))
     # Create some events in the database that the client is subscribed to
     data = json.dumps(subscribed_event)
     r = self.client.post('/events',
                          data=data,
                          content_type='application/json')
     d2 = json.loads(r.data.decode('utf-8'))
     r = self.client.post('/events',
                          data=data,
                          content_type='application/json')
     d3 = json.loads(r.data.decode('utf-8'))
     r = self.client.post('/events',
                          data=data,
                          content_type='application/json')
     d4 = json.loads(r.data.decode('utf-8'))
     # Check the server for new events
     response = self.client.get('/clients/%s/subscribedevents' % self.id)
     resp = json.loads(response.data.decode('utf-8'))
     # Check that we get the correct response
     testutil.assertEqual(
         self, {
             response.status_code: 200,
             resp['status']: 200,
             resp['message']: 'OK',
             d4['event']['id']: resp['events'][0]['id'],
             d3['event']['id']: resp['events'][1]['id'],
             d2['event']['id']: resp['events'][2]['id'],
         })
Exemple #2
0
 def test_getting_a_list_of_apikeys(self):
     """Get a list of API keys."""
     response = self.client.post('/apikeys',
                                 data=json.dumps({'permission': 'write'}),
                                 content_type='application/json')
     a1 = json.loads(response.data.decode('utf-8'))
     response = self.client.post('/apikeys',
                                 data=json.dumps({'permission': 'write'}),
                                 content_type='application/json')
     a2 = json.loads(response.data.decode('utf-8'))
     response = self.client.post('/apikeys',
                                 data=json.dumps({'permission': 'write'}),
                                 content_type='application/json')
     a3 = json.loads(response.data.decode('utf-8'))
     response = self.client.get('/apikeys')
     resp = json.loads(response.data.decode('utf-8'))
     testutil.assertEqual(
         self, {
             response.status_code: 200,
             resp['status']: 200,
             resp['message']: 'OK',
             a3['apikey']['id']: resp['apikeys'][0]['id'],
             a2['apikey']['id']: resp['apikeys'][1]['id'],
             a1['apikey']['id']: resp['apikeys'][2]['id'],
         })
Exemple #3
0
 def test_getting_a_client_by_id_that_does_not_exist(self):
     """Test getting a client that doesn't exist."""
     response = self.client.get('/clients/0')
     resp = json.loads(response.data.decode('utf-8'))
     testutil.assertEqual(
         self, {
             response.status_code: 404,
             resp['status']: 404,
             resp['message']: 'Client Not Found',
         })
Exemple #4
0
 def test_getting_a_specific_apikey_that_does_not_exist(self):
     """Get a specific API key from an ID that doesn't exist."""
     response = self.client.get('/apikeys/0')
     resp = json.loads(response.data.decode('utf-8'))
     testutil.assertEqual(
         self, {
             response.status_code: 404,
             resp['status']: 404,
             resp['message']: 'API Key Not Found'
         })
Exemple #5
0
 def test_denied_access_without_authentication(self):
     """Check for events without being authenticated."""
     response = self.client.get('/events')
     resp = json.loads(response.data.decode('utf-8'))
     # Check that we get the correct response
     testutil.assertEqual(
         self, {
             response.status_code: 401,
             resp['status']: 401,
             resp['message']: 'Not Authenticated'
         })
Exemple #6
0
 def test_404_on_non_existant_event(self):
     """Try to fetch an event that doesn't exist."""
     response = self.client.get('/events/0')
     resp = json.loads(response.data.decode('utf-8'))
     # Check that we get the correct response
     testutil.assertEqual(
         self, {
             response.status_code: 404,
             resp['status']: 404,
             resp['message']: 'Event Not Found'
         })
Exemple #7
0
 def test_cannot_get_a_list_of_apikeys_when_write(self):
     """Test that you can't fetch api keys with write permissions."""
     response = self.client.get('/apikeys')
     resp = json.loads(response.data.decode('utf-8'))
     # Check that we get the correct response
     testutil.assertEqual(
         self, {
             response.status_code: 403,
             resp['status']: 403,
             resp['message']: 'Insufficient Permissions',
         })
Exemple #8
0
 def test_access_events_when_none_are_created(self):
     """Fetch a list of events, when there are none."""
     response = self.client.get('/events')
     resp = json.loads(response.data.decode('utf-8'))
     # Check that we get the correct response
     testutil.assertEqual(
         self, {
             response.status_code: 200,
             resp['status']: 200,
             resp['message']: 'OK',
         })
     self.assertCountEqual(resp['events'], [])
 def test_getting_list_of_clients_events_with_non_made(self):
     """Fetch a list of a clients events, when there are none."""
     # Create an event that the client is not subscribed to
     response = self.client.get('/clients/1/events')
     resp = json.loads(response.data.decode('utf-8'))
     # Check that we get the correct response
     testutil.assertEqual(
         self, {
             response.status_code: 200,
             resp['status']: 200,
             resp['message']: 'OK',
         })
     self.assertCountEqual(resp['events'], [])
Exemple #10
0
    def test_cannot_getting_a_specific_apikey_when_read(self):
        """
        Test that you can't fetch a specific api key with read permissions.

        """
        response = self.client.get('/apikeys/1')
        resp = json.loads(response.data.decode('utf-8'))
        # Check that we get the correct response
        testutil.assertEqual(
            self, {
                response.status_code: 403,
                resp['status']: 403,
                resp['message']: 'Insufficient Permissions',
            })
    def test_getting_subscribedevents_from_client_that_does_not_exist(self):
        """
        Test getting a list of subscribedevents from a client that doesn't
        exist.

        """
        response = self.client.get('/clients/0/subscribedevents')
        resp = json.loads(response.data.decode('utf-8'))
        testutil.assertEqual(
            self, {
                response.status_code: 404,
                resp['status']: 404,
                resp['message']: 'Client Not Found',
            })
Exemple #12
0
 def test_cannot_create_an_event_when_read(self):
     """Test that you can't create an event with read permission."""
     # Post the request to the test server
     response = self.client.post('/events',
                                 data=json.dumps(test_event),
                                 content_type='application/json')
     resp = json.loads(response.data.decode('utf-8'))
     # Check that we get the correct response
     testutil.assertEqual(
         self, {
             response.status_code: 403,
             resp['status']: 403,
             resp['message']: 'Insufficient Permissions'
         })
Exemple #13
0
 def test_cannot_create_apikey_when_write(self):
     """Test that you can't create an api key with write permissions."""
     # Post the request to the test server
     response = self.client.post('/apikeys',
                                 data=json.dumps({'permission': 'write'}),
                                 content_type='application/json')
     resp = json.loads(response.data.decode('utf-8'))
     # Check that we get the correct response
     testutil.assertEqual(
         self, {
             response.status_code: 403,
             resp['status']: 403,
             resp['message']: 'Insufficient Permissions',
         })
Exemple #14
0
 def test_creating_apikey_with_invalid_permissions(self):
     """Cannot create an API key with invalid permissions."""
     # Post the request to the test server
     response = self.client.post('/apikeys',
                                 data=json.dumps(
                                     {'permission': 'this is invalid'}),
                                 content_type='application/json')
     resp = json.loads(response.data.decode('utf-8'))
     # Check that we get the correct response
     testutil.assertEqual(
         self, {
             response.status_code: 400,
             resp['status']: 400,
             resp['message']: 'Invalid Permissions',
         })
Exemple #15
0
 def test_getting_a_specific_apikey(self):
     """Get a specific API key from ID."""
     response = self.client.get('/apikeys/1')
     resp = json.loads(response.data.decode('utf-8'))
     testutil.assertEqual(
         self, {
             response.status_code: 200,
             resp['status']: 200,
             resp['message']: 'OK',
             resp['apikey']['id']: 1,
             resp['apikey']['permission']: 'admin',
             'apikey' in resp['apikey']: True,
             'created' in resp['apikey']: True,
             'uri' in resp['apikey']: True,
         })
Exemple #16
0
 def test_posting_a_complete_event(self):
     """Create a valid new event."""
     # Post the request to the test server
     response = self.client.post('/events',
                                 data=json.dumps(test_event),
                                 content_type='application/json')
     resp = json.loads(response.data.decode('utf-8'))
     # Check that we get the correct response
     testutil.assertEqual(
         self, {
             response.status_code: 201,
             resp['status']: 201,
             resp['message']: 'Created Event',
             'id' in resp['event']: True,
             'uri' in resp['event']: True
         })
Exemple #17
0
 def test_getting_a_list_of_clients(self):
     """Test getting a list of clients."""
     # Create different clients, by authenticating with them
     self.authenticate_with_server('read', alternate_device='Alarm')
     self.authenticate_with_server('read', alternate_device='Light')
     self.authenticate_with_server('read', alternate_device='Thermostat')
     response = self.client.get('/clients')
     resp = json.loads(response.data.decode('utf-8'))
     testutil.assertEqual(
         self, {
             response.status_code: 200,
             resp['status']: 200,
             resp['message']: 'OK',
             'device' not in resp['clients'][0]: True,
             'location' not in resp['clients'][0]: True,
             'id' in resp['clients'][0]: True,
             'uri' in resp['clients'][0]: True,
         })
Exemple #18
0
 def test_getting_the_client_itself(self):
     """Test getting the client's own information"""
     # Create different clients, by authenticating with them
     self.authenticate_with_server('read', alternate_device='Alarm')
     self.authenticate_with_server('read', alternate_device='Light')
     self.authenticate_with_server('read', alternate_device='Thermostat')
     response = self.client.get('/clients/me')
     resp = json.loads(response.data.decode('utf-8'))
     testutil.assertEqual(
         self, {
             response.status_code: 200,
             resp['status']: 200,
             resp['message']: 'OK',
             resp['client']['device']: 'Thermostat',
             resp['client']['location']: 'Living Room',
             'id' in resp['client']: True,
             'uri' in resp['client']: True,
         })
Exemple #19
0
 def test_client_can_authenticate(self):
     """Authenticate a client."""
     # Use the helper function to authenticate with the server
     response = self.authenticate_with_server('read')
     # Decode the json response string into a python dictionary
     resp = json.loads(response.data.decode('utf-8'))
     # Check that we get the correct response
     testutil.assertEqual(
         self, {
             response.status_code: 201,
             resp['status']: 201,
             resp['message']: 'Authenticated',
             'id' in resp: True,
             'sessionKey' in resp: True,
             resp['scheduled']['slot']: None,
             resp['PINGTimeout']: 240,
             resp['scheduled']['assigned']: False,
             resp['scheduled']['period']: None
         })
Exemple #20
0
 def test_that_apikey_is_required(self):
     """Test that an API key is required for authentication."""
     test_auth = {
         'device': 'Thermostat',
         'location': 'Living Room',
         'subscribe': {}
     }
     response = self.client.post('/auth',
                                 data=json.dumps(test_auth),
                                 content_type='application/json')
     print(response.data)
     resp = json.loads(response.data.decode('utf-8'))
     # Check that we get the correct response
     testutil.assertEqual(
         self, {
             response.status_code: 401,
             resp['status']: 401,
             resp['message']: 'Invalid API Key'
         })
Exemple #21
0
 def test_creating_apikey(self):
     """Create an API key."""
     # Post the request to the test server
     response = self.client.post('/apikeys',
                                 data=json.dumps({'permission': 'write'}),
                                 content_type='application/json')
     resp = json.loads(response.data.decode('utf-8'))
     # Check that we get the correct response
     testutil.assertEqual(
         self, {
             response.status_code: 201,
             resp['status']: 201,
             resp['message']: 'Created API Key',
             resp['apikey']['permission']: 'write',
             'id' in resp['apikey']: True,
             'apikey' in resp['apikey']: True,
             'created' in resp['apikey']: True,
             'uri' in resp['apikey']: True
         })
Exemple #22
0
 def test_getting_a_client_by_id(self):
     """Test getting a specific client."""
     # Create different clients, by authenticating with them
     self.authenticate_with_server('read', alternate_device='Alarm')
     client2 = self.authenticate_with_server('read',
                                             alternate_device='Light')
     self.authenticate_with_server('read', alternate_device='Thermostat')
     client2_resp = json.loads(client2.data.decode('utf-8'))
     response = self.client.get('/clients/' + str(client2_resp['id']))
     resp = json.loads(response.data.decode('utf-8'))
     testutil.assertEqual(
         self, {
             response.status_code: 200,
             resp['status']: 200,
             resp['message']: 'OK',
             resp['client']['device']: 'Light',
             resp['client']['location']: 'Living Room',
             'id' in resp['client']: True,
             'uri' in resp['client']: True,
         })
Exemple #23
0
 def test_getting_list_of_full_events(self):
     """Fetch a list of events with full information"""
     # Create a event that the client has subscribed to
     data = json.dumps(subscribed_event)
     self.client.post('/events', data=data, content_type='application/json')
     response = self.client.get('/events?full=true')
     resp = json.loads(response.data.decode('utf-8'))
     # Check that we actually get the full event in the feed
     testutil.assertEqual(
         self, {
             response.status_code: 200,
             resp['status']: 200,
             resp['message']: 'OK',
             resp['events'][0]['name']: 'Light',
             resp['events'][0]['location']: 'Living Room',
             resp['events'][0]['action']: 'Light Changed',
             resp['events'][0]['value']: 'On',
             'id' in resp['events'][0]: True,
             'uri' in resp['events'][0]: True,
             'time' in resp['events'][0]: True,
         })
Exemple #24
0
 def test_getting_a_specific_event(self):
     """Fetch a specific event from an ID."""
     # Create an event, so we have something to request
     response = self.client.post('/events',
                                 data=json.dumps(test_event),
                                 content_type='application/json')
     event_resp = json.loads(response.data.decode('utf-8'))
     # Get the specific event
     response = self.client.get('/events/' + str(event_resp['event']['id']))
     resp = json.loads(response.data.decode('utf-8'))
     # Check that we get the correct response
     testutil.assertEqual(
         self, {
             response.status_code: 200,
             resp['status']: 200,
             resp['message']: 'OK',
             resp['event']['name']: test_event['event']['name'],
             resp['event']['device']: testutil.DEVICE,
             resp['event']['action']: test_event['event']['action'],
             resp['event']['value']: test_event['event']['value'],
             'id' in resp['event']: True,
             'uri' in resp['event']: True,
             'time' in resp['event']: True
         })