-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for token-based authentication; added get_subscribers f…
…unction to MultiTenantCumulocityApp.
- Loading branch information
Showing
5 changed files
with
95 additions
and
16 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
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,54 @@ | ||
# Copyright (c) 2020 Software AG, | ||
# Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, | ||
# and/or its subsidiaries and/or its affiliates and/or their licensors. | ||
# Use, reproduction, transfer, publication or disclosure is prohibited except | ||
# as specifically provided for in your License Agreement with Software AG. | ||
|
||
import pytest | ||
import requests | ||
|
||
from c8y_api import CumulocityRestApi, HTTPBearerAuth | ||
from c8y_api.app import SimpleCumulocityApp | ||
from c8y_api.model import ManagedObject | ||
|
||
|
||
@pytest.fixture(name='token_app') | ||
def fix_token_app(test_environment): | ||
"""Provide a token-based REST API instance.""" | ||
# First, create an instance for basic auth | ||
c8y = SimpleCumulocityApp() | ||
# Submit auth request | ||
form_data = { | ||
'grant_type': 'PASSWORD', | ||
'username': c8y.auth.username, | ||
'password': c8y.auth.password | ||
} | ||
r = requests.post(url=c8y.base_url + '/tenant/oauth', data=form_data) | ||
# Obtain token from response | ||
assert r.status_code == 200 | ||
cookie = r.headers['Set-Cookie'] | ||
# split by ; to separate parts, then map a=b items to dictionary | ||
cookie_parts = {x[0]:x[1] for x in [c.split('=') for c in cookie.split(';')] if len(x) == 2} | ||
auth_token = cookie_parts['authorization'] | ||
assert auth_token | ||
# build token-based app | ||
return CumulocityRestApi( | ||
base_url=c8y.base_url, | ||
tenant_id=c8y.tenant_id, | ||
auth=HTTPBearerAuth(auth_token) | ||
) | ||
|
||
|
||
def test_token_based_app_headers(token_app): | ||
"""Verify that a token-based app only features a 'Bearer' auth header.""" | ||
response = token_app.session.get("https://httpbin.org/headers") | ||
auth_header = response.json()['headers']['Authorization'] | ||
assert auth_header.startswith('Bearer') | ||
|
||
|
||
def test_token_based_app(token_app): | ||
"""Verify that a token-based app can be used for all kind of requests.""" | ||
mo = ManagedObject(token_app, name='test-object', type='test-object-type').create() | ||
mo['new_Fragment'] = {} | ||
mo.update() | ||
mo.delete() |
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