|
| 1 | +# Copyright 2020 Google Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Test cases for the firebase_admin.tenant_mgt module.""" |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +import firebase_admin |
| 20 | +from firebase_admin import exceptions |
| 21 | +from firebase_admin import tenant_mgt |
| 22 | +from tests import testutils |
| 23 | + |
| 24 | + |
| 25 | +GET_TENANT_RESPONSE = """{ |
| 26 | + "name": "projects/mock-project-id/tenants/tenant-id", |
| 27 | + "displayName": "Test Tenant", |
| 28 | + "allowPasswordSignup": true, |
| 29 | + "enableEmailLinkSignin": true |
| 30 | +}""" |
| 31 | + |
| 32 | +TENANT_NOT_FOUND_RESPONSE = """{ |
| 33 | + "error": { |
| 34 | + "message": "TENANT_NOT_FOUND" |
| 35 | + } |
| 36 | +}""" |
| 37 | + |
| 38 | +INVALID_TENANT_IDS = [None, '', 0, 1, True, False, list(), tuple(), dict()] |
| 39 | + |
| 40 | +TENANT_MGT_URL_PREFIX = 'https://identitytoolkit.googleapis.com/v2beta1/projects/mock-project-id' |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture(scope='module') |
| 44 | +def tenant_mgt_app(): |
| 45 | + app = firebase_admin.initialize_app( |
| 46 | + testutils.MockCredential(), name='tenantMgt', options={'projectId': 'mock-project-id'}) |
| 47 | + yield app |
| 48 | + firebase_admin.delete_app(app) |
| 49 | + |
| 50 | + |
| 51 | +def _instrument_tenant_mgt(app, status, payload): |
| 52 | + service = tenant_mgt._get_tenant_mgt_service(app) |
| 53 | + recorder = [] |
| 54 | + service.client.session.mount( |
| 55 | + tenant_mgt._TenantManagementService.TENANT_MGT_URL, |
| 56 | + testutils.MockAdapter(payload, status, recorder)) |
| 57 | + return service, recorder |
| 58 | + |
| 59 | + |
| 60 | +class TestTenant: |
| 61 | + |
| 62 | + @pytest.mark.parametrize('data', [None, 'foo', 0, 1, True, False, list(), tuple(), dict()]) |
| 63 | + def test_invalid_data(self, data): |
| 64 | + with pytest.raises(ValueError): |
| 65 | + tenant_mgt.Tenant(data) |
| 66 | + |
| 67 | + def test_tenant(self): |
| 68 | + data = { |
| 69 | + 'name': 'projects/test-project/tenants/tenant-id', |
| 70 | + 'displayName': 'Test Tenant', |
| 71 | + 'allowPasswordSignup': True, |
| 72 | + 'enableEmailLinkSignin': True, |
| 73 | + } |
| 74 | + tenant = tenant_mgt.Tenant(data) |
| 75 | + assert tenant.tenant_id == 'tenant-id' |
| 76 | + assert tenant.display_name == 'Test Tenant' |
| 77 | + assert tenant.allow_password_sign_up is True |
| 78 | + assert tenant.enable_email_link_sign_in is True |
| 79 | + |
| 80 | + def test_tenant_optional_params(self): |
| 81 | + data = { |
| 82 | + 'name': 'projects/test-project/tenants/tenant-id', |
| 83 | + } |
| 84 | + tenant = tenant_mgt.Tenant(data) |
| 85 | + assert tenant.tenant_id == 'tenant-id' |
| 86 | + assert tenant.display_name is None |
| 87 | + assert tenant.allow_password_sign_up is False |
| 88 | + assert tenant.enable_email_link_sign_in is False |
| 89 | + |
| 90 | + |
| 91 | +class TestGetTenant: |
| 92 | + |
| 93 | + @pytest.mark.parametrize('tenant_id', INVALID_TENANT_IDS) |
| 94 | + def test_invalid_tenant_id(self, tenant_id): |
| 95 | + with pytest.raises(ValueError): |
| 96 | + tenant_mgt.delete_tenant(tenant_id) |
| 97 | + |
| 98 | + def test_get_tenant(self, tenant_mgt_app): |
| 99 | + _, recorder = _instrument_tenant_mgt(tenant_mgt_app, 200, GET_TENANT_RESPONSE) |
| 100 | + tenant = tenant_mgt.get_tenant('tenant-id', app=tenant_mgt_app) |
| 101 | + assert tenant.tenant_id == 'tenant-id' |
| 102 | + assert tenant.display_name == 'Test Tenant' |
| 103 | + assert tenant.allow_password_sign_up is True |
| 104 | + assert tenant.enable_email_link_sign_in is True |
| 105 | + |
| 106 | + assert len(recorder) == 1 |
| 107 | + req = recorder[0] |
| 108 | + assert req.method == 'GET' |
| 109 | + assert req.url == '{0}/tenants/tenant-id'.format(TENANT_MGT_URL_PREFIX) |
| 110 | + |
| 111 | + def test_tenant_not_found(self, tenant_mgt_app): |
| 112 | + _instrument_tenant_mgt(tenant_mgt_app, 500, TENANT_NOT_FOUND_RESPONSE) |
| 113 | + with pytest.raises(tenant_mgt.TenantNotFoundError) as excinfo: |
| 114 | + tenant_mgt.get_tenant('tenant-id', app=tenant_mgt_app) |
| 115 | + |
| 116 | + error_msg = 'No tenant found for the given identifier (TENANT_NOT_FOUND).' |
| 117 | + assert excinfo.value.code == exceptions.NOT_FOUND |
| 118 | + assert str(excinfo.value) == error_msg |
| 119 | + assert excinfo.value.http_response is not None |
| 120 | + assert excinfo.value.cause is not None |
| 121 | + |
| 122 | + |
| 123 | +class TestDeleteTenant: |
| 124 | + |
| 125 | + @pytest.mark.parametrize('tenant_id', INVALID_TENANT_IDS) |
| 126 | + def test_invalid_tenant_id(self, tenant_id): |
| 127 | + with pytest.raises(ValueError): |
| 128 | + tenant_mgt.delete_tenant(tenant_id) |
| 129 | + |
| 130 | + def test_delete_tenant(self, tenant_mgt_app): |
| 131 | + _, recorder = _instrument_tenant_mgt(tenant_mgt_app, 200, '{}') |
| 132 | + tenant_mgt.delete_tenant('tenant-id', app=tenant_mgt_app) |
| 133 | + |
| 134 | + assert len(recorder) == 1 |
| 135 | + req = recorder[0] |
| 136 | + assert req.method == 'DELETE' |
| 137 | + assert req.url == '{0}/tenants/tenant-id'.format(TENANT_MGT_URL_PREFIX) |
| 138 | + |
| 139 | + def test_tenant_not_found(self, tenant_mgt_app): |
| 140 | + _instrument_tenant_mgt(tenant_mgt_app, 500, TENANT_NOT_FOUND_RESPONSE) |
| 141 | + with pytest.raises(tenant_mgt.TenantNotFoundError) as excinfo: |
| 142 | + tenant_mgt.delete_tenant('tenant-id', app=tenant_mgt_app) |
| 143 | + |
| 144 | + error_msg = 'No tenant found for the given identifier (TENANT_NOT_FOUND).' |
| 145 | + assert excinfo.value.code == exceptions.NOT_FOUND |
| 146 | + assert str(excinfo.value) == error_msg |
| 147 | + assert excinfo.value.http_response is not None |
| 148 | + assert excinfo.value.cause is not None |
0 commit comments