-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added details to the current_user call in the API client and improvin…
…g e2e tests (#1541) * Adding slightly more details to the username in the API client. * adding one more check for status in e2e tests, to make less flaky * Adding to e2e tests, running analyzers and a more specific search * Adding a small API client test * Moving to a more flexible user object.
- Loading branch information
Showing
11 changed files
with
276 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Copyright 2020 Google Inc. All rights reserved. | ||
# | ||
# 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. | ||
"""Timesketch API client library.""" | ||
import logging | ||
|
||
from . import resource | ||
|
||
|
||
logger = logging.getLogger('timesketch_api.user') | ||
|
||
|
||
class User(resource.BaseResource): | ||
"""User object.""" | ||
|
||
def __init__(self, api): | ||
"""Initializes the user object.""" | ||
self._object_data = None | ||
resource_uri = 'users/me/' | ||
super().__init__(api, resource_uri) | ||
|
||
def _get_data(self): | ||
"""Returns dict from the first object of the resource data.""" | ||
if self._object_data: | ||
return self._object_data | ||
|
||
data = self.data | ||
objects = data.get('objects') | ||
if objects: | ||
self._object_data = objects[0] | ||
else: | ||
self._object_data = {} | ||
|
||
return self._object_data | ||
|
||
@property | ||
def groups(self): | ||
"""Property that returns the groups the user belongs to.""" | ||
data = self._get_data() | ||
groups = data.get('groups', []) | ||
return [x.get('name', '') for x in groups] | ||
|
||
@property | ||
def is_active(self): | ||
"""Property that returns bool indicating whether the user is active.""" | ||
data = self._get_data() | ||
return data.get('active', True) | ||
|
||
@property | ||
def is_admin(self): | ||
"""Property that returns bool indicating whether the user is admin.""" | ||
data = self._get_data() | ||
return data.get('admin', False) | ||
|
||
@property | ||
def username(self): | ||
"""Property that returns back the username of the current user.""" | ||
data = self._get_data() | ||
return data.get('username', 'Unknown') | ||
|
||
def __str__(self): | ||
"""Returns a string representation of the username.""" | ||
user_strings = [self.username] | ||
|
||
if self.is_active: | ||
user_strings.append('[active]') | ||
else: | ||
user_strings.append('[inactive]') | ||
|
||
if self.is_admin: | ||
user_strings.append('<is admin>') | ||
|
||
return ' '.join(user_strings) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Copyright 2020 Google Inc. All rights reserved. | ||
# | ||
# 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. | ||
"""End to end tests of Timesketch client functionality.""" | ||
|
||
from . import interface | ||
from . import manager | ||
|
||
|
||
class ClientTest(interface.BaseEndToEndTest): | ||
"""End to end tests for client functionality.""" | ||
|
||
NAME = 'client_test' | ||
|
||
def test_client(self): | ||
"""Client tests.""" | ||
expected_user = 'test' | ||
user = self.api.current_user | ||
self.assertions.assertEqual(user.username, expected_user) | ||
self.assertions.assertEqual(user.is_admin, False) | ||
self.assertions.assertEqual(user.is_active, True) | ||
|
||
sketch_name = 'Testing' | ||
sketch_description = 'This is truly a foobar' | ||
new_sketch = self.api.create_sketch( | ||
name=sketch_name, description=sketch_description) | ||
|
||
self.assertions.assertEqual(new_sketch.name, sketch_name) | ||
self.assertions.assertEqual( | ||
new_sketch.description, sketch_description) | ||
|
||
first_sketch = self.api.get_sketch(1) | ||
self.assertions.assertEqual( | ||
self.sketch.name, first_sketch.name) | ||
|
||
sketches = list(self.api.list_sketches()) | ||
self.assertions.assertEqual(len(sketches), 2) | ||
|
||
for index in self.api.list_searchindices(): | ||
self.assertions.assertTrue(bool(index.name)) | ||
|
||
|
||
manager.EndToEndTestManager.register_test(ClientTest) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.