Beispiel #1
0
    def test_kick_member(self):
        self.login('*****@*****.**')

        with cas_mockup_server(), self.given(
                f'Kick a member from a room',
                f'/apiv1/rooms/id:{self.room.id}',
                f'KICK',
                form=dict(memberId=self.user2.reference_id)):
            assert status == 200
            assert len(response.json['memberIds']) == 1

            when('Member not a member of the room',
                 form=Update(memberId=self.user3.reference_id))
            assert status == '617 Not A Member'

            when('Member not found', form=Update(memberId=5))
            assert status == '611 Member Not Found'

            when('Try to pass without memberId', form=Remove('memberId'))
            assert status == '709 Member Id Is Required'

            when('Member id is invalid', form=Update(memberId='user1'))
            assert status == '705 Invalid Member Id'

            when('Request with bad room id', url_parameters=Update(id='room'))
            assert status == 404

            when('Try to pass an Unauthorized request', authorization=None)
            assert status == 401
Beispiel #2
0
    def test_get_user_by_id(self):
        self.login('*****@*****.**')

        with cas_mockup_server(), self.given(
            f'Get a message by id',
            f'/apiv1/messages/id:{self.message1.id}',
            f'GET',
        ):
            assert status == 200
            assert response.json['body'] == 'This is message 1'

            when(
                'Get The message sent by another user in the same room',
                url_parameters=Update(id=f'{self.message3.id}')
            )
            assert status == 200
            assert response.json['body'] == 'This is message 3'

            when('Invalid message id', url_parameters=Update(id='message1'))
            assert status == 404

            when('Message not found', url_parameters=Update(id=0))
            assert status == 404

            when('Try to pass unauthorize request', authorization=None)
            assert status == 401
Beispiel #3
0
    def test_list(self):
        self.login('*****@*****.**')

        with cas_mockup_server(), self.given(
                'List envelops',
                '/apiv1/envelops',
                'LIST',
        ):
            assert status == 200
            assert len(response.json) == 4

            when('Sort envelops', query=dict(sort='body'))
            assert response.json[0]['body'] == self.envelop1.body

            when('Reverse sorting body content by alphabet',
                 query=dict(sort='-body'))
            assert response.json[0]['body'] == self.envelop4.body

            when('Filter envelops',
                 query=dict(sort='id', body=self.envelop1.body))
            assert response.json[0]['body'] == self.envelop1.body

            when('List envelops except one of them',
                 query=dict(sort='id', body=f'!{self.envelop1.body}'))
            assert response.json[0]['body'] == self.envelop2.body

            when('Envelop pagination', query=dict(sort='id', take=1, skip=2))
            assert response.json[0]['body'] == self.envelop3.body

            when('Manipulate sorting and pagination',
                 query=dict(sort='-body', take=1, skip=2))
            assert response.json[0]['body'] == self.envelop2.body

            when('Request is not authorized', authorization=None)
            assert status == 401
Beispiel #4
0
    def test_creat_token(self):
        self.login('*****@*****.**')

        with cas_mockup_server(), self.given(
                'Try to create a direct with a user',
                '/apiv1/directs',
                'CREATE',
                form=dict(userId=3)):
            assert status == 200
            assert response.json['type'] == 'direct'

            when('The user not exists', form=Update(userId=5))
            assert status == '611 Member Not Found'

            when('Try to pass invalid user id in the form',
                 form=Update(userId='Invalid'))
            assert status == '705 Invalid Member Id'

            when('Try to pass empty form', form=None)
            assert status == '710 Empty Form'

            when('Blocked user tries to create a direct',
                 form=Update(userId=1))
            assert status == '613 Not Allowed To Create Direct With This Member'

            self.logout()
            self.login('*****@*****.**')

            when('Try to create a direct with a blocked user',
                 form=Update(userId=self.user1.id),
                 authorization=self._authentication_token)
            assert status == '613 Not Allowed To Create Direct With This Member'

            when('Try to pass an unauthorized request', authorization=None)
            assert status == 401
Beispiel #5
0
    def test_add_user_to_contact(self):
        self.login('*****@*****.**')

        with cas_mockup_server(), self.given(
                'Add a user to contacts',
                '/apiv1/contacts',
                'ADD',
                form=dict(userId=3),
        ):
            assert status == 200
            session = self.create_session()
            user = session.query(Member).filter(Member.id == 1).one()
            assert len(user.contacts) == 2

            when('The user id already added to contact', form=Update(userId=2))
            assert status == '603 Already Added To Contacts'

            when('Try to add not existing user', form=Update(userId=6))
            assert status == '611 Member Not Found'

            when('Try to request with invalid user id',
                 form=Update(userId='invalid'))
            assert status == '705 Invalid Member Id'

            when('Request without issuing userId', form=Remove('userId'))
            assert status == '709 Member Id Is Required'

            when('Trying to pass the unauthorized request', authorization=None)
            assert status == 401
Beispiel #6
0
    def test_mention(self):
        self.login(self.member1.email)

        with cas_mockup_server(), self.given(
                'Mention a target',
                f'/apiv1/members/member_id: {self.member2.id}/mentions',
                'MENTION',
                json=dict(body='abc', originTargetId=self.room.id)):
            assert status == 200
            assert response.json['body'] == 'abc'

            when('Origin target id is null',
                 json=given | dict(originTargetId=None))
            assert status == '718 Origin Target Id Is Null'

            when('Origin target id is not in form',
                 json=given - 'originTargetId')
            assert status == '717 Origin Target Id Not In Form'

            when('Origin target id is not found',
                 json=given | dict(originTargetId=0))
            assert status == '622 Target Not Found'

            when('Body length is more than limit',
                 json=given | dict(body=(65536 + 1) * 'a'))
            assert status == '702 Must be less than 65536 charecters'

            when('There is no direct between mentioned and mentioner member',
                 url_parameters=dict(member_id=self.member3.id))
            assert response.json['body'] == 'abc'

            when('User is logged out', authorization=None)
            assert status == 401
Beispiel #7
0
    def test_list_subscribe_target(self):
        self.login('*****@*****.**')

        with cas_mockup_server(), self.given(
                'List targets a user subscribe to',
                '/apiv1/subscribetargets',
                'LIST',
        ):
            assert status == 200
            assert len(response.json) == 3

            when('Try to sort the response', query=dict(sort='id'))
            assert len(response.json) == 3
            assert response.json[0]['id'] == 1

            when('Sorting the response descending', query=dict(sort='-id'))
            assert len(response.json) == 3
            assert response.json[0]['id'] == 4

            when('testing pagination', query=dict(sort='id', take=1, skip=1))
            assert len(response.json) == 1
            assert response.json[0]['id'] == 2

            when('Sorting befor pagination',
                 query=dict(sort='-id', take=1, skip=1))
            assert len(response.json) == 1
            assert response.json[0]['id'] == 2

            when('Filtering the answer', query=dict(id=self.room1.id))
            assert len(response.json) == 1
            assert response.json[0]['title'] == 'room1'

            when('Try to pass an Unauthorized request', authorization=None)
            assert status == 401
Beispiel #8
0
    def test_mention(self):
        self.login('*****@*****.**')

        with cas_mockup_server(), self.given(
                'Mention a target',
                f'/apiv1/targets/{self.room.id}/mentions',
                'MENTION',
                json=dict(body='abc', originTargetId=self.room.id)):
            assert status == 200
            assert response.json['body'] == 'abc'

            when('Origin target id is null',
                 json=given | dict(originTargetId=None))
            assert status == '718 Origin Target Id Is Null'

            when('Origin target id is not in form',
                 json=given - 'originTargetId')
            assert status == '717 Origin Target Id Not In Form'

            when('Origin target id is not found',
                 json=given | dict(originTargetId=0))
            assert status == '622 Target Not Found'

            when('Body length is more than limit',
                 json=given | dict(body=(65536 + 1) * 'a'))
            assert status == '702 Must be less than 65536 charecters'

            when('User is logged out', authorization=None)
            assert status == 401
Beispiel #9
0
    def test_delete_the_message(self):
        self.login('*****@*****.**')

        with cas_mockup_server(), self.given('Try to delete a message',
                                             '/apiv1/messages/id:1', 'DELETE'):
            assert status == 200
            assert response.json['body'] == 'This message is deleted'
            assert response.json['removedAt'] is not None

            when('Delete a message with attachment',
                 url_parameters=Update(id=self.message3.id))
            assert status == 200
            assert response.json['attachment'] is None

            when('Delete the same message')
            assert status == '616 Message Already Deleted'

            when('Try to delete someone else messages',
                 url_parameters=Update(id=self.message2.id))
            assert status == 403

            when('The message not exists', url_parameters=Update(id=0))
            assert status == '614 Message Not Found'

            when('Trying to pass using invalid message id',
                 url_parameters=Update(id='Invalid'))
            assert status == '707 Invalid MessageId'

            self.logout()
            self.login('*****@*****.**')
            when('Not allowed to delete the message',
                 url_parameters=Update(id=self.message2.id),
                 authorization=self._authentication_token)
            assert status == 403
Beispiel #10
0
    def test_patch(self):
        self.login('*****@*****.**')

        with cas_mockup_server(), self.given(
                'Testing the patch method on APIv1',
                verb='PATCH',
                url='/apiv1',
                json=[
                    dict(op='MENTION',
                         path=f'targets/{self.room.id}/mentions',
                         value={
                             'body': 'sample message',
                             'originTargetId': self.room.id
                         }),
                    dict(op='MENTION',
                         path=f'members/{self.user2.id}/mentions',
                         value={
                             'body': 'abc',
                             'originTargetId': self.room.id
                         }),
                ]):
            assert status == 200
            assert len(response.json) == 2
            assert response.json[0]['id'] is not None
            assert response.json[1]['id'] is not None

            when(
                'One of requests response faces non 200 OK',
                json=[dict(op='MENTION', path='targets/0/mentions', value={})])
            assert status == 404
Beispiel #11
0
    def test_login_with_cas(self):
        token = JwtPrincipal(
            dict(email='*****@*****.**', title='user2',
                 referenceId=2)).dump().decode()
        self._authentication_token = token

        with cas_mockup_server():
            settings.merge(f'''
                oauth:
                  member:
                    url: {settings.tokenizer.url}/apiv1/members
                    verb: get
            ''')
            with self.given(title='Try to access an authorized resource',
                            description='Members are got from the cas',
                            url='/apiv1/resources',
                            headers={'X-Oauth2-Access-Token: access token2'}):
                assert status == 200
                mismathc_token = JwtPrincipal(
                    dict(email='*****@*****.**',
                         title='user3',
                         referenceId=3)).dump().decode()

                when('Token not match the CAS member',
                     authorization=mismathc_token)
                assert status == 400

                when('Try to pass with bad token',
                     authorization='Invalid Token')
                assert status == 400

                when('Try to access an unauthorized resource',
                     authorization=None)
                assert status == 401

                member_token = JwtPrincipal(
                    dict(email='*****@*****.**',
                         title='user1',
                         referenceId=1)).dump().decode()
                when('Member exist in database',
                     authorization=member_token,
                     headers=Remove('X-Oauth2-Access-Token'))
                assert status == 200

                with cas_server_status('503 Service Not Available'):
                    when('CAS server is not available')
                    assert status == '800 CAS Server Not Available'

                with cas_server_status('500 Internal Service Error'):
                    when('CAS server faces with internal error')
                    assert status == '801 CAS Server Internal Error'

                with cas_server_status('404 Not Found'):
                    when('CAS server is not found')
                    assert status == '617 CAS Server Not Found'
Beispiel #12
0
    def test_search_user(self):
        self.login('*****@*****.**')

        with cas_mockup_server(), self.given(
                'Search for a user',
                '/apiv1/members',
                'SEARCH',
                form=dict(query='Use'),
        ):
            assert status == 200
            assert response.json[0]['title'] == self.user2.title
            assert len(response.json) == 2

            when('Search using email', form=Update(query='exam'))
            assert status == 200
            assert len(response.json) == 1

            when('Search without query parameter', form=Remove('query'))
            assert status == '708 Search Query Is Required'

            when(
                'Search string must be less than 20 charecters',
                form=Update(
                    query= \
                        'The search string should be less than 20 charecters'
                )
            )
            assert status == '702 Must Be Less Than 20 Charecters'

            when('Try to sort the respone', query=dict(sort='id'))
            assert len(response.json) == 2
            assert response.json[0]['id'] == 1

            when('Trying ro sort the response in descend ordering',
                 query=dict(sort='-id'))
            assert response.json[0]['id'] == 2

            when('Filtering the response', query=dict(title='user2'))
            assert len(response.json) == 1
            assert response.json[0]['title'] == 'user2'

            when('Trying to filter the response ignoring the title',
                 query=dict(title='!user2'))
            assert len(response.json) == 1
            assert response.json[0]['title'] != 'user2'

            when('Testing pagination', query=dict(take=1, skip=1))
            assert len(response.json) == 1
            assert response.json[0]['title'] == self.user1.title

            when('Sort before pagination',
                 query=dict(sort='-id', take=3, skip=1))
            assert len(response.json) == 1
            assert response.json[0]['title'] == 'user1'
Beispiel #13
0
    def test_reply_a_message(self):
        self.login('*****@*****.**')

        with cas_mockup_server(), self.given(
                f'Reply message 1',
                f'/apiv1/messages/id:{self.message1.id}',
                f'REPLY',
                multipart=dict(body='This is a reply to message1', )):
            assert status == 200
            assert response.json['replyRoot'] == self.message1.id
            assert response.json['replyTo']['body'] == 'This is message 1'
            assert len(self.room.messages) == 3

            when('Requested message not found', url_parameters=Update(id=4))
            assert status == 404

            when('Request a message with invalid message id',
                 url_parameters=Update(id='message1'))
            assert status == 404

            when('Try to reply with unsopported media type',
                 multipart=Update(mimetype='video/3gpp'))
            assert status == 415

            when('Try to send reply with long text',
                 multipart=Update(body=(65536 + 1) * 'a'))
            assert status == '702 Must be less than 65536 charecters'

            when('Remove body from the form', multipart=Remove('body'))
            assert status == '712 Message Body Required'

            when('Requested message is already deleted',
                 url_parameters=Update(id=self.message2.id))
            assert status == '616 Message Already Deleted'

            with open(IMAGE_PATH, 'rb') as f:
                when('Replay message with attachment',
                     multipart=Update(attachment=io.BytesIO(f.read())))
                assert status == 200

            with open(MAXIMUM_IMAGE_PATH, 'rb') as f:
                when('Attachment is more than maximum length',
                     multipart=Update(attachment=io.BytesIO(f.read())))
                assert status == 413

            settings.attachements.messages.files.max_length = 800
            with open(DLL_PATH, 'rb') as f:
                when('Replay a message with unsupported media type attachment',
                     multipart=Update(attachment=io.BytesIO(f.read())))
                assert status == '415 Unsupported Media Type'

            when('Try to pass an unauthorized request', authorization=None)
            assert status == 401
Beispiel #14
0
    def test_logout_a_user(self):
        self.login('*****@*****.**')

        with cas_mockup_server(), self.given(
            'Log out a user',
            '/apiv1/tokens',
            'INVALIDATE',
        ):
            assert status == 200

            when('Try to access some authorize source', authorization=None)
            assert status == 401
Beispiel #15
0
    def test_request_with_query_string(self):
        self.login('*****@*****.**')

        with cas_mockup_server(), self.given('Test request using query string',
                                             '/apiv1/members',
                                             'SEARCH',
                                             query=dict(query='user')):
            assert status == 200
            assert len(response.json) == 2

            when('An unauthorized search', authorization=None)
            assert status == 401
Beispiel #16
0
    def test_request_with_query_string(self):
        self.login(self.user1.email)

        with cas_mockup_server(), self.given(
                'Test request using query string',
                f'/apiv1/targets/id: {self.room1.id}/messages',
                'SEARCH',
                query=dict(query='Sec')):
            assert status == 200
            assert len(response.json) == 1

            when('An unauthorized search', authorization=None)
            assert status == 401
Beispiel #17
0
    def test_send_message_as_auditlog(self):
        self.login(self.user1.email)
        body = dict(action='Create', attribute='a', new=1, old=2)
        mimetype = 'application/x-auditlog'

        with cas_mockup_server(), self.given(
                None,
                f'/apiv1/targets/id:{self.room.id}/messages',
                f'SEND',
                json=dict(body=ujson.dumps(body), mimetype=mimetype)):
            assert status == 200
            assert response.json['body'] == body
            assert response.json['mimetype'] == mimetype
Beispiel #18
0
    def test_search_message(self):
        self.login(self.user1.email)

        with cas_mockup_server(), self.given(
                'Search for a message by submitting form',
                f'/apiv1/targets/id: {self.room1.id}/messages',
                'SEARCH',
                form=dict(query='Fir'),
        ):
            assert status == 200
            assert len(response.json) == 1
            assert response.json[0]['body'] == self.message1.body

            when('Search without query parameter', form=given - 'query')
            assert status == '715 Form Query Or Query String Is Required'

            when('Search string must be less than 20 charecters',
                 form=given | dict(query=(50 + 1) * 'a'))
            assert status == '702 Must Be Less Than 50 Characters'

            when('Try to sort the response',
                 query=dict(sort='id'),
                 form=given | dict(query='message'))
            assert len(response.json) == 3
            assert response.json[0]['id'] < response.json[1]['id']

            when('Trying ro sort the response in descend ordering',
                 query=dict(sort='-id'),
                 form=given | dict(query='message'))
            assert len(response.json) == 3
            assert response.json[0]['id'] > response.json[1]['id']

            when('Filtering the response', query=dict(id=self.message1.id))
            assert len(response.json) == 1
            assert response.json[0]['id'] == self.message1.id

            when('Trying to filter the response ignoring the title',
                 query=dict(title=f'!{self.message1.id}'),
                 form=given | dict(query='message'))
            assert len(response.json) == 3

            when('Testing pagination',
                 query=dict(take=1, skip=1),
                 form=given | dict(query='message'))
            assert len(response.json) == 1

            when('Sort before pagination',
                 query=dict(sort='-id', take=2, skip=1),
                 form=given | dict(query='message'))
            assert len(response.json) == 2
            assert response.json[0]['id'] > response.json[1]['id']
Beispiel #19
0
    def test_list_messages_of_target(self):
        self.login('*****@*****.**')

        with cas_mockup_server(), self.given(
                'List messages of a target',
                '/apiv1/targets/id:1/messages',
                'LIST',
        ):
            assert status == 200
            assert len(response.json) == 5
            assert response.json[0]['body'] == self.message4.body
            assert response.json[0]['isMine'] is False

            assert response.json[2]['body'] == self.message1.body
            assert response.json[2]['isMine'] is True

            when('Try to send form in the request',
                 form=dict(parameter='parameter'))
            assert status == '711 Form Not Allowed'

            when('Try to sort the response', query=dict(sort='id'))
            assert len(response.json) == 5
            assert response.json[0]['id'] == 1

            when('Sorting the response descending', query=dict(sort='-id'))
            assert response.json[0]['id'] == 5

            when('Testing pagination', query=dict(take=1, skip=1))
            assert len(response.json) == 1
            assert response.json[0]['body'] == self.message6.body

            when('Sorting befor pagination',
                 query=dict(sort='-id', take=2, skip=1))
            assert len(response.json) == 2
            assert response.json[0]['id'] == 4

            when('Filtering the response', query=dict(id=self.message1.id))
            assert len(response.json) == 1
            assert response.json[0]['body'] == self.message1.body

            when('Filtering message by isMine', query=dict(isMine='true'))
            assert status == 200
            assert len(response.json) == 3
            assert response.json[0]['body'] == 'This is message 1'

            when('Try to pass an Unauthorized request', authorization=None)
            assert status == 401
Beispiel #20
0
    def test_list_members_of_target(self):
        self.login(self.user1.email)

        with cas_mockup_server(), self.given(
            'List members of a target',
            f'/apiv1/targets/id: {self.room1.id}/members',
            'LIST',
        ):
            assert status == 200
            assert len(response.json) == 3

            when(
                'Target is not found',
                url_parameters=dict(id=0)
            )
            assert status == 404

            when(
                'There is parameter in form',
                form=dict(parameter='parameter')
            )
            assert status == '711 Form Not Allowed'

            when('Try to sort the response', query=dict(sort='id'))
            assert len(response.json) == 3
            assert response.json[0]['id'] == 1

            when('Sorting the response descending', query=dict(sort='-id'))
            assert response.json[0]['id'] == 3

            when('Testing pagination', query=dict(take=1, skip=1))
            assert len(response.json) == 1
            assert response.json[0]['title'] == self.user3.title

            when(
                'Sorting befor pagination',
                query=dict(sort='-id', take=2, skip=1)
            )
            assert len(response.json) == 2
            assert response.json[0]['id'] == 2

            when('Filtering the response', query=dict(id=self.user3.id))
            assert len(response.json) == 1
            assert response.json[0]['title'] == self.user3.title

            when('Try to pass an Unauthorized request', authorization=None)
            assert status == 401
Beispiel #21
0
    def test_edit_the_message(self):
        self.login(self.user1.email)

        with cas_mockup_server(), self.given(
            f'Try to edit a message',
            f'/apiv1/messages/id:{self.message1.id}',
            f'EDIT',
            form=dict(body='Message 1 is edited')
        ):
            assert status == 200
            assert response.json['body'] == 'Message 1 is edited'
            assert response.json['id'] == 1
            assert response.json['attachment'] is not None

            when('The message not exists', url_parameters=Update(id=0))
            assert status == 404

            when(
                'Trying to pass using invalid message id',
                url_parameters=Update(id='not-integer')
            )
            assert status == 404

            when(
                'Try to send long text',
                form=Update(body=(65536 + 1) * 'a')
            )
            assert status == '702 Must be less than 65536 charecters'

            when(
                'Try to edit a deleted message',
                url_parameters=Update(id=self.message3.id)
            )
            assert status == '616 Message Already Deleted'

            self.logout()
            self.login(self.user2.email)

            when(
                'Not allowed to edit the message',
                authorization=self._authentication_token,
            )
            assert status == 403

            when('Try to pass an unauthorized request', authorization=None)
            assert status == 401
Beispiel #22
0
    def test_create_user(self):
        token = JwtPrincipal(
            dict(email='*****@*****.**', title='user1',
                 referenceId=2)).dump().decode()

        with cas_mockup_server(), self.given(
                'Create a member',
                '/apiv1/members',
                'ENSURE',
                headers=dict(authorization=token,
                             x_oauth2_access_token='access token1'),
                form=dict(title='example')):
            assert status == 200
            assert response.json['title'] == 'user1'

            when('Access token is not in headers',
                 headers=Remove('x_oauth2_access_token'))
            assert status == 400
Beispiel #23
0
    def test_get_user_by_id(self):
        self.login('*****@*****.**')

        with cas_mockup_server(), self.given(
                'Get a user by her or his id',
                '/apiv1/members/id:1',
                'GET',
        ):
            assert status == 200
            assert response.json['title'] == 'user1'

            when('Member not found', url_parameters=Update(id='3'))
            assert status == 404

            when('Ivalid use id', url_parameters=Update(id='user1'))
            assert status == 404

            when('Try to pass unauthorize request', authorization=None)
            assert status == 401
Beispiel #24
0
    def test_send_message_to_target(self):
        self.login(self.user1.email)

        with cas_mockup_server(), maestro_mockup_server(), self.given(
                f'Send a message to a target',
                f'/apiv1/targets/id:{self.room.id}/messages',
                f'SEND',
                form=dict(body='hello world!')):
            assert status == 200
            assert response.json['body'] == 'hello world!'
            assert response.json['isMine'] is True
            assert response.json['mimetype'] == 'text/plain'
            assert response.json[
                'senderReferenceId'] == self.user1.reference_id

            when('Invalid target id', url_parameters=Update(id='Invalid'))
            assert status == '706 Invalid Target Id'

            when('Target does not exist', url_parameters=Update(id=0))
            assert status == '404 Target Not Exists'

            when('Try to send unsopported media type',
                 form=Update(mimetype='video/3gpp'))
            assert status == 415

            when('Try to send long text', form=Update(body=(65536 + 1) * 'a'))
            assert status == '702 Must be less than 65536 charecters'

            when('Remove body from the form', form=Remove('body'))
            assert status == 400

            when('Try to pass an unauthorized request', authorization=None)
            assert status == 401

            settings.webhooks.sent.timeout = 0.1
            when('Request to Dolphin is timeout')
            assert status == 200

            settings.webhooks.sent.timeout = 30
            settings.webhooks.sent.url = 'http://localhost:1'
            when('Connection is failed')
            assert status == 200
Beispiel #25
0
    def test_claim_email(self):
        with cas_mockup_server(), self.given(
                'claim a user',
                '/apiv1/emails',
                'CLAIM',
                form=dict(email='*****@*****.**')):

            assert response.status == '200 OK'

            when('The email is repeted',
                 form=Update(email='*****@*****.**'))
            assert response.status == '601 The requested email address is ' \
                'already registered.'

            when('The email format is invalid',
                 form=Update(email='already.example.com'))
            assert response.status == '701 Invalid email format.'

            when('Request without email', form=Remove('email'))
            assert response.status == 400
Beispiel #26
0
    def test_list_rooms_of_user(self):
        self.login('*****@*****.**')

        with cas_mockup_server(), self.given(
                'List all the rooms a user owns',
                '/apiv1/rooms',
                'LIST',
        ):
            assert status == 200
            assert len(response.json) == 2
            assert response.json[0]['title'] == 'room1'
            assert response.json[0]['ownerId'] == self.user1.id

            self.logout()
            self.login('*****@*****.**')
            when('List the rooms for another user',
                 authorization=self._authentication_token)
            assert status == 200
            assert len(response.json) == 1
            assert response.json[0]['title'] == 'room3'
            assert response.json[0]['ownerId'] == self.user2.id
Beispiel #27
0
    def test_list_contacts(self):
        self.login('*****@*****.**')

        with cas_mockup_server(), self.given(
                'List a user contacts',
                '/apiv1/contacts',
                'LIST',
        ):
            assert status == 200
            assert len(response.json) == 2

            when('Try to sort the response', query=dict(sort='title'))
            assert len(response.json) == 2
            assert response.json[0]['title'] == 'contact1'

            when('Try to sort the response in descending order',
                 query=dict(sort='-title'))
            assert response.json[0]['title'] == 'contact2'

            when('Try to filter the response using title',
                 query=dict(title='contact2'))
            assert len(response.json) == 1
            assert response.json[0]['title'] == 'contact2'

            when('Try to filter the response ignoring a title',
                 query=dict(title='!contact2'))
            assert len(response.json) == 1
            assert response.json[0]['title'] != 'contact2'

            when('Testing pagination', query=dict(take=1, skip=1))
            assert len(response.json) == 1
            assert response.json[0]['title'] == 'contact2'

            when('Test sorting before pagination',
                 query=dict(sort='-title', take=1, skip=1))
            assert response.json[0]['title'] == 'contact1'

            when('An unauthorized request', authorization=None)
            assert status == 401
Beispiel #28
0
    def test_add_user_to_room(self):
        self.login('*****@*****.**')

        with cas_mockup_server(), self.given(
                'Add to a room',
                '/apiv1/rooms/id:1',
                'ADD',
                form=dict(userId=self.user1.reference_id),
        ):
            assert status == 200
            assert len(response.json['memberIds']) == 2

            when('Already added to the room',
                 form=Update(userId=self.room_member.reference_id))
            assert status == '604 Already Added To Target'

            when('Member not exists', form=Update(userId=10))
            assert status == '611 Member Not Found'

            when('Not allowed to add this person to any room',
                 form=Update(userId=self.never.reference_id))
            assert status == '602 Not Allowed To Add This Person To Any Room'

            when('Room not exist', url_parameters=Update(id='2'))
            assert status == '612 Room Not Found'

            self.logout()
            self.login('*****@*****.**')
            when('Blocked by the target user',
                 form=Update(userId=self.blocker.reference_id),
                 authorization=self._authentication_token)
            assert status == '601 Not Allowed To Add Member To Any Room'

            self.logout()
            self.login('*****@*****.**')
            when('The blocker can not add the user he blocked',
                 form=Update(userId=4),
                 authorization=self._authentication_token)
            assert status == '601 Not Allowed To Add Member To Any Room'
Beispiel #29
0
    def test_attach_file_to_message(self):
        self.login(self.user1.email)

        with cas_mockup_server(), open(IMAGE_PATH, 'rb') as f, self.given(
                f'Send a message to a target',
                f'/apiv1/targets/id:{self.room.id}/messages',
                f'SEND',
                multipart=dict(body='hello world!',
                               mimetype='image/png',
                               attachment=io.BytesIO(f.read()))):
            assert status == 200
            assert response.json['body'] == 'hello world!'
            assert response.json['isMine'] is True
            assert 'attachment' in response.json

            with open(TEXT_PATH, 'rb') as f:
                when('Mime type does not match content type',
                     multipart=Update(attachment=io.BytesIO(f.read())))
                assert status == 200

            with open(MAXIMUM_IMAGE_PATH, 'rb') as f:
                when('Image size is more than maximum length',
                     multipart=Update(mimetype='image/jpeg',
                                      attachment=io.BytesIO(f.read())))
                assert status == 413

            settings.attachements.messages.files.max_length = 800
            with open(EXECUTABLE_PATH, 'rb') as f:
                when('Image size is more than maximum length',
                     multipart=Update(mimetype='image/jpeg',
                                      attachment=io.BytesIO(f.read())))
                assert status == '415 Unsupported Media Type'

            with open(DLL_PATH, 'rb') as f:
                when('Image size is more than maximum length',
                     multipart=Update(mimetype='image/jpeg',
                                      attachment=io.BytesIO(f.read())))
                assert status == '415 Unsupported Media Type'
Beispiel #30
0
    def test_subscribe_rooms(self):
        self.login('*****@*****.**')
        rooms = (str(i) + ', ' for i in range(1, 102))
        rooms_string = ''.join(rooms)

        with cas_mockup_server(), self.given(
                'Subscribe multiple rooms',
                f'/apiv1/rooms?id=IN({self.room1.id}, {self.room2.id})',
                'SUBSCRIBE',
        ):
            assert status == 200
            assert len(response.json) == 2
            assert response.json[0]['title'] == 'room1'
            assert response.json[0]['ownerId'] == self.user2.id

            when('There is form parameter', form=dict(parameter='abc'))
            assert status == '711 Form Not Allowed'

            when('The number of rooms to subscribe is more than limit',
                 query=dict(id=f'IN({rooms_string})'))
            assert status == '716 Maximum 5 Rooms To Subscribe At A Time'

            when('Try to pass unauthorize request', authorization=None)
            assert status == 401