2016-04-20 10:21:40 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2015, 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.
|
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
import json
|
|
|
|
|
2016-04-20 10:21:40 -04:00
|
|
|
from mock import Mock
|
2018-07-19 06:03:33 -04:00
|
|
|
from six import PY3
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2018-07-17 06:43:18 -04:00
|
|
|
from twisted.test.proto_helpers import MemoryReactorClock
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2018-07-17 06:43:18 -04:00
|
|
|
from synapse.http.server import JsonResource
|
2018-07-19 06:03:33 -04:00
|
|
|
from synapse.rest.client.v1_only.register import register_servlets
|
2018-07-17 06:43:18 -04:00
|
|
|
from synapse.util import Clock
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2016-04-20 10:21:40 -04:00
|
|
|
from tests import unittest
|
2018-08-15 09:43:41 -04:00
|
|
|
from tests.server import make_request, render, setup_test_homeserver
|
2016-04-20 10:21:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
class CreateUserServletTestCase(unittest.TestCase):
|
2018-07-17 06:43:18 -04:00
|
|
|
"""
|
|
|
|
Tests for CreateUserRestServlet.
|
|
|
|
"""
|
2018-08-10 09:54:09 -04:00
|
|
|
|
2018-07-19 06:03:33 -04:00
|
|
|
if PY3:
|
|
|
|
skip = "Not ported to Python 3."
|
2016-04-20 10:21:40 -04:00
|
|
|
|
|
|
|
def setUp(self):
|
2016-10-04 16:32:58 -04:00
|
|
|
self.registration_handler = Mock()
|
2016-04-20 10:21:40 -04:00
|
|
|
|
2016-10-04 16:32:58 -04:00
|
|
|
self.appservice = Mock(sender="@as:test")
|
|
|
|
self.datastore = Mock(
|
|
|
|
get_app_service_by_token=Mock(return_value=self.appservice)
|
2016-04-20 10:21:40 -04:00
|
|
|
)
|
|
|
|
|
2018-07-17 06:43:18 -04:00
|
|
|
handlers = Mock(registration_handler=self.registration_handler)
|
|
|
|
self.clock = MemoryReactorClock()
|
|
|
|
self.hs_clock = Clock(self.clock)
|
|
|
|
|
|
|
|
self.hs = self.hs = setup_test_homeserver(
|
2018-08-13 02:47:46 -04:00
|
|
|
self.addCleanup, http_client=None, clock=self.hs_clock, reactor=self.clock
|
2016-04-20 10:21:40 -04:00
|
|
|
)
|
2016-10-04 16:32:58 -04:00
|
|
|
self.hs.get_datastore = Mock(return_value=self.datastore)
|
|
|
|
self.hs.get_handlers = Mock(return_value=handlers)
|
2016-04-20 10:21:40 -04:00
|
|
|
|
|
|
|
def test_POST_createuser_with_valid_user(self):
|
2018-07-17 06:43:18 -04:00
|
|
|
|
|
|
|
res = JsonResource(self.hs)
|
|
|
|
register_servlets(self.hs, res)
|
|
|
|
|
|
|
|
request_data = json.dumps(
|
|
|
|
{
|
|
|
|
"localpart": "someone",
|
|
|
|
"displayname": "someone interesting",
|
|
|
|
"duration_seconds": 200,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
url = b'/_matrix/client/api/v1/createUser?access_token=i_am_an_app_service'
|
|
|
|
|
2016-04-20 10:21:40 -04:00
|
|
|
user_id = "@someone:interesting"
|
|
|
|
token = "my token"
|
|
|
|
|
|
|
|
self.registration_handler.get_or_create_user = Mock(
|
|
|
|
return_value=(user_id, token)
|
|
|
|
)
|
|
|
|
|
2018-07-17 06:43:18 -04:00
|
|
|
request, channel = make_request(b"POST", url, request_data)
|
2018-08-15 09:43:41 -04:00
|
|
|
render(request, res, self.clock)
|
2018-07-17 06:43:18 -04:00
|
|
|
|
|
|
|
self.assertEquals(channel.result["code"], b"200")
|
2016-04-20 10:21:40 -04:00
|
|
|
|
|
|
|
det_data = {
|
|
|
|
"user_id": user_id,
|
|
|
|
"access_token": token,
|
2018-07-17 06:43:18 -04:00
|
|
|
"home_server": self.hs.hostname,
|
2016-04-20 10:21:40 -04:00
|
|
|
}
|
2018-07-17 06:43:18 -04:00
|
|
|
self.assertDictContainsSubset(det_data, json.loads(channel.result["body"]))
|