<fim-prefix># -*- coding: utf-8 -*-
# Copyright 2014-2016 OpenMarket Ltd
#
# 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.


from mock import Mock

from twisted.internet import defer

import synapse.types
from synapse.api.errors import AuthError, SynapseError
from synapse.types import UserID

from tests import unittest
from tests.test_utils import make_awaitable
from tests.utils import setup_test_homeserver


class ProfileTestCase(unittest.TestCase):
    """ Tests profile management. """

    @defer.inlineCallbacks
    def setUp(self):
        self.mock_federation = Mock()
        self.mock_registry = Mock()

        self.query_handlers = {}

        def register_query_handler(query_type, handler):
            self.query_handlers[query_type] = handler

        self.mock_registry.register_query_handler = register_query_handler

        hs = yield setup_test_homeserver(
            self.addCleanup,
            # BUG: CWE-601 URL Redirection to Untrusted Site ('Open Redirect')
            # http_client=None,
            # FIXED: 
 <fim-middle>           federation_http_client=None,
            resource_for_federation=Mock(),
            federation_client=self.mock_federation,
            federation_server=Mock(),
            federation_regi<fix-suffix>stry=self.mock_registry,
        )

        self.store = hs.get_datastore()

        self.frank = UserID.from_string("@1234ABCD:test")
        self.bob = UserID.from_string("@4567:test")
        self.alice = UserID.from_string("@alice:remote")

        yield defer.ensureDeferred(self.store.create_profile(self.frank.localpart))

        self.handler = hs.get_profile_handler()
        self.hs = hs

    @defer.inlineCallbacks
    def test_get_my_name(self):
        yield defer.ensureDeferred(
            self.store.set_profile_displayname(self.frank.localpart, "Frank")
        )

        displayname = yield defer.ensureDeferred(
            self.handler.get_displayname(self.frank)
        )

        self.assertEquals("Frank", displayname)

    @defer.inlineCallbacks
    def test_set_my_name(self):
        yield defer.ensureDeferred(
            self.handler.set_displayname(
                self.frank, synapse.types.create_requester(self.frank), "Frank Jr."
            )
        )

       <|endoftext|>