Skip to content

Commit

Permalink
[tests][apps] adding unit tests for gsuite app(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryandeivert committed Oct 27, 2017
1 parent 3878e8c commit e362b65
Show file tree
Hide file tree
Showing 3 changed files with 259 additions and 0 deletions.
225 changes: 225 additions & 0 deletions tests/unit/app_integrations/test_apps/test_gsuite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
"""
Copyright 2017-present, Airbnb Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# pylint: disable=abstract-class-instantiated,protected-access,no-self-use,abstract-method,attribute-defined-outside-init
import json
from mock import Mock, mock_open, patch

from apiclient import errors
from nose.tools import assert_equal, assert_false, assert_items_equal, assert_true, raises

from app_integrations.apps.gsuite import GSuiteReportsApp #, DuoAdminApp, DuoAuthApp
from app_integrations.config import AppConfig

from tests.unit.app_integrations.test_helpers import (
get_valid_config_dict,
MockSSMClient
)

@patch.object(GSuiteReportsApp, '_type', Mock(return_value='admin'))
@patch.object(GSuiteReportsApp, 'type', Mock(return_value='type'))
@patch.object(AppConfig, 'SSM_CLIENT', MockSSMClient())
class TestGSuiteReportsApp(object):
"""Test class for the GSuiteReportsApp"""

# Remove all abstractmethods so we can instantiate GSuiteReportsApp for testing
@patch.object(GSuiteReportsApp, '__abstractmethods__', frozenset())
def setup(self):
"""Setup before each method"""
self._app = GSuiteReportsApp(AppConfig(get_valid_config_dict('gsuite_admin')))

def test_sleep(self):
"""GSuiteReportsApp - Sleep Seconds"""
assert_equal(self._app._sleep_seconds(), 0)

def test_required_auth_info(self):
"""GSuiteReportsApp - Required Auth Info"""
assert_items_equal(self._app.required_auth_info().keys(), {'keyfile'})

@patch('app_integrations.apps.gsuite.ServiceAccountCredentials.from_json_keyfile_dict',
Mock(return_value=True))
def test_keyfile_validator(self):
"""GSuiteReportsApp - Keyfile Validation, Success"""
validation_function = self._app.required_auth_info()['keyfile']['format']
data = {'test': 'keydata'}
mocker = mock_open(read_data=json.dumps(data))
with patch('__builtin__.open', mocker):
loaded_keydata = validation_function('fakepath')
assert_equal(loaded_keydata, data)

@patch('app_integrations.apps.gsuite.ServiceAccountCredentials.from_json_keyfile_dict')
def test_keyfile_validator_failure(self, cred_mock):
"""GSuiteReportsApp - Keyfile Validation, Failure"""
validation_function = self._app.required_auth_info()['keyfile']['format']
cred_mock.return_value = False
mocker = mock_open(read_data=json.dumps({'test': 'keydata'}))
with patch('__builtin__.open', mocker):
assert_false(validation_function('fakepath'))
cred_mock.assert_called()

@patch('app_integrations.apps.gsuite.ServiceAccountCredentials.from_json_keyfile_dict')
def test_keyfile_validator_bad_json(self, cred_mock):
"""GSuiteReportsApp - Keyfile Validation, Bad JSON"""
validation_function = self._app.required_auth_info()['keyfile']['format']
mocker = mock_open(read_data='invalid json')
with patch('__builtin__.open', mocker):
assert_false(validation_function('fakepath'))
cred_mock.assert_not_called()

@patch('app_integrations.apps.gsuite.ServiceAccountCredentials.from_json_keyfile_dict',
Mock(return_value=True))
def test_load_credentials(self):
"""GSuiteReportsApp - Load Credentials, Success"""
assert_true(self._app._load_credentials('fakedata'))

@patch('app_integrations.apps.gsuite.ServiceAccountCredentials.from_json_keyfile_dict')
def test_load_credentials_bad(self, cred_mock):
"""GSuiteReportsApp - Load Credentials, ValueError"""
cred_mock.side_effect = ValueError('Bad things happened')
assert_false(self._app._load_credentials('fakedata'))

@patch('app_integrations.apps.gsuite.GSuiteReportsApp._load_credentials',
Mock(return_value=True))
@patch('app_integrations.apps.gsuite.discovery.build')
def test_create_service(self, build_mock):
"""GSuiteReportsApp - Create Service, Success"""
build_mock.return_value.activities.return_value = True
assert_true(self._app._create_service())

@patch('logging.Logger.debug')
def test_create_service_exists(self, log_mock):
"""GSuiteReportsApp - Create Service, Exists"""
self._app._activities_service = True
assert_true(self._app._create_service())
log_mock.assert_called_with('Service already instantiated for %s', 'type')

@patch('app_integrations.apps.gsuite.GSuiteReportsApp._load_credentials',
Mock(return_value=False))
def test_create_service_fail_creds(self):
"""GSuiteReportsApp - Create Service, Credential Failure"""
assert_false(self._app._create_service())

@patch('app_integrations.apps.gsuite.GSuiteReportsApp._load_credentials',
Mock(return_value=True))
@patch('app_integrations.apps.gsuite.discovery.build')
def test_create_service_fail_resource(self, build_mock):
"""GSuiteReportsApp - Create Service, Resource Failure"""
build_mock.side_effect = errors.Error('This is bad')
assert_false(self._app._create_service())

def test_gather_logs(self):
"""GSuiteReportsApp - Gather Logs, Success"""
with patch.object(self._app, '_activities_service') as service_mock:
payload = {
'kind': 'reports#auditActivities',
'nextPageToken': 'the next page\'s token',
'items': self._get_sample_logs(10)
}
service_mock.list.return_value.execute.return_value = payload

assert_equal(len(self._app._gather_logs()), 10)
assert_equal(self._app._last_timestamp, '2011-06-17T15:39:18.460Z')

@patch('app_integrations.apps.gsuite.GSuiteReportsApp._create_service',
Mock(return_value=True))
@patch('logging.Logger.exception')
def test_gather_logs_http_error(self, log_mock):
"""GSuiteReportsApp - Gather Logs, HTTP Error"""
with patch.object(self._app, '_activities_service') as service_mock:
error = errors.HttpError('response', bytes('bad'))
service_mock.list.return_value.execute.side_effect = error
assert_false(self._app._gather_logs())
log_mock.assert_called_with('Failed to execute activities listing')


@patch('app_integrations.apps.gsuite.GSuiteReportsApp._load_credentials',
Mock(return_value=False))
def test_gather_logs_no_service(self):
"""GSuiteReportsApp - Gather Logs, No Service"""
with patch.object(self._app, '_activities_service') as service_mock:
self._app._activities_service = False
assert_false(self._app._gather_logs())
service_mock.list.assert_not_called()

@patch('app_integrations.apps.gsuite.GSuiteReportsApp._create_service',
Mock(return_value=True))
@patch('logging.Logger.error')
def test_gather_logs_no_results(self, log_mock):
"""GSuiteReportsApp - Gather Logs, No Results From API"""
with patch.object(self._app, '_activities_service') as service_mock:
service_mock.list.return_value.execute.return_value = None
assert_false(self._app._gather_logs())
log_mock.assert_called_with('No results received from the G Suite API request for %s',
'type')

@patch('app_integrations.apps.gsuite.GSuiteReportsApp._create_service',
Mock(return_value=True))
@patch('logging.Logger.info')
def test_gather_logs_empty_items(self, log_mock):
"""GSuiteReportsApp - Gather Logs, Empty Activities List"""
with patch.object(self._app, '_activities_service') as service_mock:
payload = {
'kind': 'reports#auditActivities',
'nextPageToken': 'the next page\'s token',
'items': []
}
service_mock.list.return_value.execute.return_value = payload
assert_false(self._app._gather_logs())
log_mock.assert_called_with('No logs in response from G Suite API request for %s',
'type')

@staticmethod
def _get_sample_logs(count):
"""Helper function for returning sample gsuite (admin) logs"""
return [{
'kind': 'audit#activity',
'id': {
'time': '2011-06-17T15:39:18.460Z',
'uniqueQualifier': 'report\'s unique ID',
'applicationName': 'admin',
'customerId': 'C03az79cb'
},
'actor': {
'callerType': 'USER',
'email': 'liz@example.com',
'profileId': 'user\'s unique G Suite profile ID',
'key': 'consumer key of requestor in OAuth 2LO requests'
},
'ownerDomain': 'example.com',
'ipAddress': 'user\'s IP address',
'events': [
{
'type': 'GROUP_SETTINGS',
'name': 'CHANGE_GROUP_SETTING',
'parameters': [
{
'name': 'SETTING_NAME',
'value': 'WHO_CAN_JOIN',
'intValue': 'integer value of parameter',
'boolValue': 'boolean value of parameter'
}
]
}
]
} for _ in range(count)]


@raises(NotImplementedError)
def test_type_not_implemented():
"""GSuiteReportsApp - Subclass Type Not Implemented"""
class GSuiteFakeApp(GSuiteReportsApp):
"""Fake GSuiteReports app that should raise a NotImplementedError"""

GSuiteFakeApp._type()
13 changes: 13 additions & 0 deletions tests/unit/app_integrations/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,19 @@ def test_determine_last_timestamp_onelogin(self, time_mock):
time_mock.return_value = 1234567890
assert_equal(self._config._determine_last_time(), '2009-02-13T22:31:30Z')

@patch('time.mktime')
def test_determine_last_timestamp_gsuite(self, time_mock):
"""AppIntegrationConfig - Determine Last Timestamp, GSuite"""
with patch.object(AppConfig, 'SSM_CLIENT', MockSSMClient(app_type='gsuite_admin')):
self._config = AppConfig.load_config(get_mock_context(), None)

# Reset the last timestamp to None
self._config.last_timestamp = None

# Use a mocked current time
time_mock.return_value = 1234567890
assert_equal(self._config._determine_last_time(), '2009-02-13T22:31:30Z')

@patch('logging.Logger.error')
def test_set_item(self, log_mock):
"""AppIntegrationConfig - Set Item, Bad Value"""
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/app_integrations/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,24 @@ def get_auth_info(cls, app_type):
'client_secret': 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'client_id': 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
}
elif app_type in {'gsuite', 'gsuite_admin', 'gsuite_drive',
'gsuite_login', 'gsuite_token'}:
return {
'keyfile': {
'type': 'service_account',
'project_id': 'myapp-123456',
'private_key_id': 'a5427e441234a5f416ab0a2e5d759752ef69fbf1',
'private_key': ('-----BEGIN PRIVATE KEY-----\nVGhpcyBpcyBub3QgcmVhbA==\n'
'-----END PRIVATE KEY-----\n'),
'client_email': 'a-test-200%40myapp-123456.iam.gserviceaccount.com',
'client_id': '316364948779587921167',
'auth_uri': 'https://accounts.google.com/o/oauth2/auth',
'token_uri': 'https://accounts.google.com/o/oauth2/token',
'auth_provider_x509_cert_url': 'https://www.googleapis.com/oauth2/v1/certs',
'client_x509_cert_url': ('https://www.googleapis.com/robot/v1/metadata/x509/'
'a-test-200%40myapp-123456.iam.gserviceaccount.com')
}
}

# Fill this out with future supported apps/services
return {}
Expand Down Expand Up @@ -172,6 +190,9 @@ def get_formatted_timestamp(app_type):
return 1505316432
elif app_type in {'onelogin', 'onelogin_events'}:
return '2017-10-10T22:03:57Z'
elif app_type in {'gsuite', 'gsuite_admin', 'gsuite_drive',
'gsuite_login', 'gsuite_token'}:
return '2017-06-17T15:39:18.460Z'


def get_valid_config_dict(app_type):
Expand Down

0 comments on commit e362b65

Please sign in to comment.