def test_record_to_dict_nulls(self): now = datetime.utcnow() data = { 'extra': {'foo': 'bar'}, 'message': None, 'method': 'POST', 'project_id': None, 'remote_address': '127.0.0.1', 'resource': '/test', 'response_status': 200, 'user_id': None } record = AuditRecord(**data) record.record_id = 'ID' record.timestamp = now expected = { 'id': 'ID', 'href': '/v1/audit-log/ID', 'message': None, 'method': 'POST', 'project': None, 'user': None, 'remote_address': '127.0.0.1', 'resource': '/test', 'response_status': 200, 'timestamp': now, 'extra': {'foo': 'bar'}, } result = audit_log._record_to_dict(record, 'USER', 'PROJECT') self.assertEquals(result, expected)
def test_all_nones(self): record = AuditRecord(user_id=None, project_id=None) audit_log._record_to_dict(record, None, None).AndReturn('REPLY') self.mox.ReplayAll() reply = audit_log.record_to_view(record) self.assertEquals('REPLY', reply)
def test_has_project_name(self): record = AuditRecord(project_id='PID', user_id=None) audit_log._record_to_dict(record, None, 'project')\ .AndReturn('REPLY') self.mox.ReplayAll() reply = audit_log.record_to_view(record, None, 'project') self.assertEquals('REPLY', reply)
def test_project_not_found(self): record = AuditRecord(project_id='PID', user_id=None) self.iadm.tenants.get('PID').AndRaise(osc_exc.NotFound('failure')) audit_log._record_to_dict(record, None, None)\ .AndReturn('REPLY') self.mox.ReplayAll() reply = audit_log.record_to_view(record) self.assertEquals('REPLY', reply)
def test_record_to_dict_works(self): now = datetime.utcnow() data = { 'extra': { 'foo': 'bar' }, 'message': 'OK', 'method': 'POST', 'project_id': 'PID', 'remote_address': '127.0.0.1', 'resource': '/test', 'response_status': 200, 'user_id': 'UID' } record = AuditRecord(**data) record.record_id = 'ID' record.timestamp = now expected = { 'id': 'ID', 'href': '/v1/audit-log/ID', 'message': 'OK', 'method': 'POST', 'project': { 'href': '/v1/projects/PID', 'id': 'PID', 'name': 'PROJECT' }, 'user': { 'href': '/v1/users/UID', 'id': 'UID', 'name': 'USER' }, 'remote_address': '127.0.0.1', 'resource': '/test', 'response_status': 200, 'timestamp': now, 'extra': { 'foo': 'bar' }, } result = audit_log._record_to_dict(record, 'USER', 'PROJECT') self.assertEquals(result, expected)
def test_get_project_name(self): record = AuditRecord(project_id='PID', user_id=None) project = doubles.make(self.mox, doubles.Tenant, name='PROJECT') self.iadm.tenants.get('PID').AndReturn(project) audit_log._record_to_dict(record, None, 'PROJECT')\ .AndReturn('REPLY') self.mox.ReplayAll() reply = audit_log.record_to_view(record) self.assertEquals('REPLY', reply)
def test_get_user_name(self): record = AuditRecord(user_id='UID', project_id=None) user = doubles.make(self.mox, doubles.User, name='USERNAME') self.iadm.users.get('UID').AndReturn(user) audit_log._record_to_dict(record, 'USERNAME', None)\ .AndReturn('REPLY') self.mox.ReplayAll() reply = audit_log.record_to_view(record) self.assertEquals('REPLY', reply)