<commit_before>#!/usr/bin/env python
# -*- encoding: utf-8 -*-
#
# Copyright 2014 Telefónica Investigación y Desarrollo, S.A.U
#
# This file is part of FI-WARE project.
#
# 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.
#
# For those usages not covered by the Apache version 2.0 License please
# contact with <EMAIL>
#

__author__ = 'gjp'
from django.test import TestCase
from mockito import mock, when
from mock import MagicMock
from keystoneclient.v2_0 import client
from keystoneclient.exceptions import Unauthorized, InternalServerError, AuthorizationFailure, ConnectionRefused
from keystoneclient.v2_0.tokens import Token
from requests import Response

from fiware_cloto.cloto.manager import AuthorizationManager
from fiware_cloto.cloto.constants import ACCEPT_HEADER, JSON_TYPE, X_AUTH_TOKEN_HEADER, TOKENS_PATH_V2,\
     TOKENS_PATH_V3, X_SUBJECT_TOKEN_HEADER, AUTH_API_V2, AUTH_API_V3, HTTP_RESPONSE_CODE_OK,\
    HTTP_RESPONSE_CODE_UNAUTHORIZED
from django.utils import timezone


class MySession(MagicMock):
    # Mock of a keystone session

    def Session(self, auth, timeout):
        # Mock of the Keystone session generator
        session_mocked = mock()
        if auth.auth_url != "http://172.16.58.3:35357/v2.0" and auth.auth_url != "http://130.206.80.61:35357/v3":
            raise ConnectionRefused("Unable to establish connection to %s", auth.auth_url)
        if auth.password == "<PASSWORD>" or auth.password == None:
            authToken = "<PASSWORD>"
            when(session_mocked).get_token().thenReturn(authToken)
        else:
            raise AuthorizationFailure(u'{"error": {"message": '
                                       u'"Could not find token: <PASSWORD>",'
                                       u' "code": 404, "title": "Not Found"}}')

        return session_mocked


class AuthorizationManagerTests(TestCase):
    def setUp(self):
        self.user = "user"
        self.token = "ff<PASSWORD>"
        self.token_not_expired = "aa<PASSWORD>"
        self.token_other_tenant = "gg<PASSWORD>"
        self.token_not_found = "0<PASSWORD>"
        self.token_not_authorized = "bbbbbbba8d69418c95f0009dda9bc1852"
        self.fakeToken = "<PASSWORD>"
        self.authToken = "7<PASSWORD>"
        self.url = "http://172.16.58.3:35357/v2.0"
        self.url_v3 = "http://172.16.58.3:35357/v3"
        self.url_noResponse = "http://127.0.0.1:35357/v2.0"
        self.url_server_error = "http://serverWithErrors.com:35357/v2.0"
        self.tenantId = "6571e3422ad84f7d828ce2f30373b3d4"
        self.a = AuthorizationManager.AuthorizationManager(self.url, AUTH_API_V2)
        self.mockedClient = client
        self.auth = mock()
        self.tokenM = mock()
        self.session_mocked = mock()
        self.session_client = MySession()
        manager = mock()
        self.requestsMock = mock()
        response = Response()
        response.status_code = HTTP_RESPONSE_CODE_OK
        response._content = '{"access":{"token":{"expires":"2015-07-09T15:16:07Z",' \
            '"id":{"token":"<PASSWORD>",' \
            '"tenant":"6571e3422ad84f7d828ce2f30373b3d4","name":"<EMAIL>",' \
            '"access_token":' \
            '"<KEY>",' \
            '"expires":"2014-09-13T07:23:51.000Z"}' \
            ',"tenant":{"description":"Tenant from IDM","enabled":true,' \
            '"id":"6571e3422ad84f7d828ce2f30373b3d4","name":"user"}},' \
            '"user":{"username":"user","roles_links":[],"id":"user",' \
            '"roles":[{"id":"8db87ccbca3b4d1ba4814c3bb0d63aab","name":"Member"}],"name":"user"}}}'

        response_not_expired = Response()
        response_not_expired.status_code = HTTP_RESPONSE_CODE_OK
        response_not_expired._content = '{"access":{"token":{"expires":"2115-07-09T15:16:07Z",' \
            '"id":{"token":"<KEY>",' \
            '"tenant":"6571e3422ad84f7d828ce2f30373b3d4","name":"<EMAIL>",' \
            '"access_token":' \
            '"<KEY>",' \
            '"expires":"2114-09-13T07:23:51.000Z"}' \
            ',"tenant":{"description":"Tenant from IDM","enabled":true,' \
            '"id":"6571e3422ad84f7d828ce2f30373b3d4","name":"user"}},' \
            '"user":{"username":"user","roles_links":[],"id":"user",' \
            '"roles":[{"id":"8db87ccbca3b4d1ba4814c3bb0d63aab","name":"Member"}],"name":"user"}}}'

        response_v3 = Response()
        response_v3.status_code = HTTP_RESPONSE_CODE_OK
        response_v3._content = '{"token": {"methods": ["password"], "roles": ' \
                               '[{"id": "ff01eb8a8d69418c95f0009dda9bc1852",' \
                               ' "name": "owner"}], "expires_at": "2015-05-26T13:01:49.632762Z", ' \
                               '"project": {"domain": {"id": "default", "name": "Default"},' \
                               ' "id": "6571e3422ad84f7d828ce2f30373b3d4", "name": "<EMAIL>"},' \
                               '"user": {"domain": {"id": "default","name": "Default"},"id": "geonexus", ' \
                               '"name": "<EMAIL>"},' \
                               '"audit_ids": ["XREoG4obSW69erG3fVjvjQ"], "issued_at": "2016-03-08T10:06:04.653858Z" }}'

        response_v3_fail = Response()
        response_v3_fail.status_code = HTTP_RESPONSE_CODE_UNAUTHORIZED
        response_v3_fail._content = '{"error": {"message": "The request you have made requires authentication.",' \
                                    ' "code": 401, "title": "Unauthorized"}}'

        response2 = Response()
        response2.status_code = HTTP_RESPONSE_CODE_OK
        response2._content = '{"access":{"token":{"expires":"2115-07-09T15:16:07Z",' \
            '"id":{"token":"<KEY>",' \
            '"tenant":"1111e3422ad84f7d828ce2f30373b3d4","name":"<EMAIL>",' \
            '"access_token":' \
            '"<KEY>",' \
            '"expires":"2114-09-13T07:23:51.000Z"}' \
            ',"tenant":{"description":"Different Tenant from IDM","enabled":true,' \
            '"id":"1111e3422ad84f7d828ce<PASSWORD>","name":"user"}},' \
            '"user":{"username":"user","roles_links":[],"id":"user",' \
            '"roles":[{"id":"8db87ccbca3b4d1ba4814c3bb0d63aab","name":"Member"}],"name":"user"}}}'

        response_not_found = Response()
        response_not_found.status_code = HTTP_RESPONSE_CODE_UNAUTHORIZED
        response_not_found._content = 'User token not found'
        response_not_found._content = '{"unauthorized": {"message": {"error": {"message": "Could not find token: '\
                                      + self.token_not_found + '", "code": 404, "title": "Not Found"}}, "code": 401}}'

        response_not_authorized = Response()
        response_not_authorized.status_code = HTTP_RESPONSE_CODE_OK
        response_not_authorized._content = 'Service not authorized'

        dic_valid = {"token": {"id": self.authToken, "tenant": {"enabled": True,
                        "description": "Default tenant", "name": "admin", "id": "6571e3422ad84f7d828ce2f30373b3d4"}}}
        dic_invalid = {"token": {"id": self.authToken, "tenant": {"enabled": True,
                        "description": "Default tenant", "name": "admin", "id": "anotherTenantId"}}}
        verification_expected = Token(manager, dic_valid)
        verification_expected_fail = Token(manager, dic_invalid)
        self.auth.__setattr__("auth_token", self.authToken)
        self.auth.__setattr__("tokens", self.tokenM)
        when(self.session_mocked).get_token().thenReturn(self.authToken)
        when(self.tokenM).authenticate(token=self.token, tenant_id=self.tenantId).thenReturn(verification_expected)
        when(self.tokenM).authenticate(token=self.fakeToken, tenant_id=self.tenantId)\
            .thenReturn(verification_expected_fail)
        when(self.mockedClient).Client(username="admin", password="<PASSWORD>", auth_url=self.url)\
            .thenReturn(self.auth)
        when(self.mockedClient).Client(username="admin", password="<PASSWORD>", auth_url=self.url_v3)\
            .thenReturn(self.auth)
        when(self.mockedClient).Client(token=self.authToken, endpoint=self.url).thenReturn(self.auth)<commit_msg>removing real ip address from tests improving JSON content<commit_after>#!/usr/bin/env python
# -*- encoding: utf-8 -*-
#
# Copyright 2014 Telefónica Investigación y Desarrollo, S.A.U
#
# This file is part of FI-WARE project.
#
# 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.
#
# For those usages not covered by the Apache version 2.0 License please
# contact with <EMAIL>.es
#

__author__ = 'gjp'
from django.test import TestCase
from mockito import mock, when
from mock import MagicMock
from keystoneclient.v2_0 import client
from keystoneclient.exceptions import Unauthorized, InternalServerError, AuthorizationFailure, ConnectionRefused
from keystoneclient.v2_0.tokens import Token
from requests import Response

from fiware_cloto.cloto.manager import AuthorizationManager
from fiware_cloto.cloto.constants import ACCEPT_HEADER, JSON_TYPE, X_AUTH_TOKEN_HEADER, TOKENS_PATH_V2,\
     TOKENS_PATH_V3, X_SUBJECT_TOKEN_HEADER, AUTH_API_V2, AUTH_API_V3, HTTP_RESPONSE_CODE_OK,\
    HTTP_RESPONSE_CODE_UNAUTHORIZED
from django.utils import timezone


class MySession(MagicMock):
    # Mock of a keystone session

    def Session(self, auth, timeout):
        # Mock of the Keystone session generator
        session_mocked = mock()
        if auth.auth_url != "http://hosturl:35357/v2.0" and auth.auth_url != "http://hosturl:35357/v3":
            raise ConnectionRefused("Unable to establish connection to %s", auth.auth_url)
        if auth.password == "<PASSWORD>" or auth.password == None:
            authToken = "<PASSWORD>"
            when(session_mocked).get_token().thenReturn(authToken)
        else:
            raise AuthorizationFailure(u'{"error": {"message": '
                                       u'"Could not find token: <PASSWORD>",'
                                       u' "code": 404, "title": "Not Found"}}')

        return session_mocked


class AuthorizationManagerTests(TestCase):
    def setUp(self):
        self.user = "user"
        self.token = "ff01eb8a8d69418c95f0009dda9bc1852"
        self.token_not_expired = "<KEY>"
        self.token_other_tenant = "<KEY>"
        self.token_not_found = "<KEY>"
        self.token_not_authorized = "<KEY>"
        self.fakeToken = "<PASSWORD>"
        self.authToken = "<PASSWORD>"
        self.url = "http://hosturl:35357/v2.0"
        self.url_v3 = "http://hosturl:35357/v3"
        self.url_noResponse = "http://127.0.0.1:35357/v2.0"
        self.url_server_error = "http://serverWithErrors.com:35357/v2.0"
        self.tenantId = "6571e3422ad84f7d828ce2f30373b3d4"
        self.a = AuthorizationManager.AuthorizationManager(self.url, AUTH_API_V2)
        self.mockedClient = client
        self.auth = mock()
        self.tokenM = mock()
        self.session_mocked = mock()
        self.session_client = MySession()
        manager = mock()
        self.requestsMock = mock()
        response = Response()
        response.status_code = HTTP_RESPONSE_CODE_OK
        response._content = '''{"access":{"token":{"expires":"2015-07-09T15:16:07Z",
            "id":{"token":"ff01eb8a8d69418c95f0009dda9bc1852",
            "tenant":"6571e3422ad84f7d828ce2f30373b3d4","name":"<EMAIL>",
            "access_token":
            "<KEY>",
            "expires":"2014-09-13T07:23:51.000Z"},
            "tenant":{"description":"Tenant from IDM","enabled":true,
            "id":"6571e3422ad84f7d828ce2f30373b3d4","name":"user"}},
            "user":{"username":"user","roles_links":[],"id":"user",
            "roles":[{"id":"<KEY>","name":"Member"}],"name":"user"}}}'''

        response_not_expired = Response()
        response_not_expired.status_code = HTTP_RESPONSE_CODE_OK
        response_not_expired._content = '''{"access":{"token":{"expires":"2115-07-09T15:16:07Z",
            "id":{"token":"<KEY>",
            "tenant":"6571e3422ad84f7d828ce2f30373b3d4","name":"<EMAIL>",
            "access_token":
            "<KEY>",
            "expires":"2114-09-13T07:23:51.000Z"}
            ,"tenant":{"description":"Tenant from IDM","enabled":true,
            "id":"6571e3422ad84f7d828ce2f30373b3d4","name":"user"}},
            "user":{"username":"user","roles_links":[],"id":"user",
            "roles":[{"id":"<KEY>","name":"Member"}],"name":"user"}}}'''

        response_v3 = Response()
        response_v3.status_code = HTTP_RESPONSE_CODE_OK
        response_v3._content = '''{"token": {"methods": ["password"], "roles":
                               [{"id": "ff01eb8a8d69418c95f0009dda9bc1852",
                               "name": "owner"}], "expires_at": "2015-05-26T13:01:49.632762Z",
                               "project": {"domain": {"id": "default", "name": "Default"},
                               "id": "6571e3422ad84f7d828ce2f30373b3d4", "name": "<EMAIL>"},
                               "user": {"domain": {"id": "default","name": "Default"},"id": "geonexus",
                               "name": "<EMAIL>"},
                               "audit_ids": ["XREoG4obSW69erG3fVjvjQ"], "issued_at": "2016-03-08T10:06:04.653858Z" }}'''

        response_v3_fail = Response()
        response_v3_fail.status_code = HTTP_RESPONSE_CODE_UNAUTHORIZED
        response_v3_fail._content = '''{"error": {"message": "The request you have made requires authentication.",
                                    "code": 401, "title": "Unauthorized"}}'''

        response2 = Response()
        response2.status_code = HTTP_RESPONSE_CODE_OK
        response2._content = '''{"access":{"token":{"expires":"2115-07-09T15:16:07Z",
            "id":{"token":"<KEY>",
            "tenant":"1111e3422ad84f7d828ce2f30373b3d4","name":"<EMAIL>",
            "access_token":
            "<KEY>",
            "expires":"2114-09-13T07:23:51.000Z"}
            ,"tenant":{"description":"Different Tenant from IDM","enabled":true,
            "id":"1111e3422ad84f7d828ce2<PASSWORD>","name":"user"}},
            "user":{"username":"user","roles_links":[],"id":"user",
            "roles":[{"id":"8db87ccbca3b4d1ba4814c3bb0d63aab","name":"Member"}],"name":"user"}}}'''

        response_not_found = Response()
        response_not_found.status_code = HTTP_RESPONSE_CODE_UNAUTHORIZED
        response_not_found._content = 'User token not found'
        response_not_found._content = '{"unauthorized": {"message": {"error": {"message": "Could not find token: '\
                                      + self.token_not_found + '", "code": 404, "title": "Not Found"}}, "code": 401}}'

        response_not_authorized = Response()
        response_not_authorized.status_code = HTTP_RESPONSE_CODE_OK
        response_not_authorized._content = 'Service not authorized'

        dic_valid = {"token": {"id": self.authToken, "tenant": {"enabled": True,
                        "description": "Default tenant", "name": "admin", "id": "6571e3422ad84f7d828ce2f30373b3d4"}}}
        dic_invalid = {"token": {"id": self.authToken, "tenant": {"enabled": True,
                        "description": "Default tenant", "name": "admin", "id": "anotherTenantId"}}}
        verification_expected = Token(manager, dic_valid)
        verification_expected_fail = Token(manager, dic_invalid)
        self.auth.__setattr__("auth_token", self.authToken)
        self.auth.__setattr__("tokens", self.tokenM)
        when(self.session_mocked).get_token().thenReturn(self.authToken)
        when(self.tokenM).authenticate(token=self.token, tenant_id=self.tenantId).thenReturn(verification_expected)
        when(self.tokenM).authenticate(token=self.fakeToken, tenant_id=self.tenantId)\
            .thenReturn(verification_expected_fail)
        when(self.mockedClient).Client(username="admin", password="<PASSWORD>", auth_url=self.url)\
            .thenReturn(self.auth)
        when(self.mockedClient).Client(username="admin", password="<PASSWORD>", auth_url=self.url_v3)\
            .thenReturn(self.auth)
        when(self.mockedClient).Client(token=self.authToken, endpoint=self.url).thenReturn(self.auth)
{'fraction_non_alphanumeric': 0.15205158264947244, 'fraction_numerical': 0.05990621336459555, 'mean_word_length': 3.0049295774647886, 'pattern_counts': {'":': 232, '<': 52, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3008453', 'n_tokens_mistral': 5954, 'n_tokens_neox': 5436, 'n_words': 941}