예제 #1
0
    def test_register(self):
        form = dict(
            name='new name',
            family='new family',
            email='*****@*****.**',
            password='******',
            role='member',
            description='description',
        )

        with self.given(
                'Register a member',
                '/apiv1/members',
                'REGISTER',
                form=form,
        ):
            assert response.status == 200

            when('Invalid email format', form=Update(email='[email protected]'))
            assert status == '701 Invalid Email Format'

            when('Email address already is registered',
                 form=Update(email='*****@*****.**'))
            assert status == '601 Email Address Is Already Registered'

            when('Request without email parameter', form=Remove('email'))
            assert status == '722 Email Not In Form'

            when('Request without name parameter', form=Remove('name'))
            assert status == '723 Name Not In Form'

            when('Request without family', form=Remove('family'))
            assert 'Family Not In form'
예제 #2
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
예제 #3
0
    def test_register(self):
        form = dict(
            title='new business',
            address='new address',
            area='new area',
            phone='989352117155',
            memberId=1,
        )

        with self.given(
                'Register a business',
                f'/apiv1/businesses',
                'REGISTER',
                form=form,
        ):
            assert response.status == 200

            when('Title address already is registered',
                 form=Update(title='Business title'))
            assert status == '602 Title Is Already Registered'

            when('Request without title parameter', form=Remove('title'))
            assert status == '724 Title Not In Form'

            when('The title format is invalid', form=Update(title='123abc '))
            assert status == '708 Invalid Title Format'

            when('Request without phone parameter', form=Remove('phone'))
            assert status == '725 Phone Not In Form'
예제 #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
예제 #5
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
예제 #6
0
    def test_create_token(self):
        email = self.member.email
        password = '******'

        with self.given('Create a login token',
                        '/apiv1/tokens',
                        'CREATE',
                        form=dict(email=email, password=password)):
            assert status == 200
            assert 'token' in response.json

            when('Invalid password', form=Update(password='******'))
            assert status == '603 Incorrect Email Or Password'

            when('Not exist email', form=Update(email='*****@*****.**'))
            assert status == '603 Incorrect Email Or Password'

            when('Invalid email format', form=Update(email='user.com'))
            assert status == '701 Invalid Email Format'

            when('Trying to pass with empty form', form={})
            assert status == '400 Empty Form'

            when('Email is empty', form=Remove('email'))
            assert status == '722 Email Not In Form'

            when('Passeord is empty', form=Remove('password'))
            assert status == '723 Password Is Not In Form'

            when('Password length is more than 50',
                 form=Update(password=(50 + 1) * 'a'))
            assert status == '706 Title Length Is More Than 50'
예제 #7
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
예제 #8
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
예제 #9
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'
예제 #10
0
    def test_get_access_token(self):
        with oauth_mockup_server():
            with self.given(
                'Try to get an access token from CAS',
                '/apiv1/oauth2/tokens',
                'OBTAIN',
                form=dict(authorizationCode='authorization code')
            ):
                assert status == 200
                assert 'token' in response.json
                assert 'X-New-Jwt-Token' in response.headers

                when(
                    'Trying to pass without the authorization code parameter',
                    form=Remove('authorizationCode')
                )
                assert status == 403

                when(
                    'Trying to pass with damage authorization code',
                    form=Update(authorizationCode='token is damage')
                )
                assert status == 401

                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'
예제 #11
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
예제 #12
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
예제 #13
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
예제 #14
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
예제 #15
0
    def test_create(self):
        email = '*****@*****.**'
        password = '******'

        with self.given('Create a login token',
                        '/apiv1/tokens',
                        'CREATE',
                        form=dict(email=email, password=password)):
            assert status == 200
            assert 'token' in response.json

            when('Invalid password', form=Update(password='******'))
            assert status == '400 Incorrect Email Or Password'

            when('Not exist email', form=Update(email='*****@*****.**'))
            assert status == '400 Incorrect Email Or Password'

            when('Invalid email format', form=Update(email='user.com'))
            assert status == '400 Invalid Email Format'

            when('Trying to pass with empty form', form={})
            assert status == '400 Empty Form'
예제 #16
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'
예제 #17
0
    def test_create_room(self):
        self.login('*****@*****.**')

        with cas_mockup_server(), self.given(
                'Creating a room',
                '/apiv1/rooms',
                'CREATE',
                form=dict(title='example'),
        ):
            assert status == 200
            assert response.json['title'] == 'example'
            assert response.json['ownerId'] == 1
            assert len(response.json['administratorIds']) == 1
            assert len(response.json['memberIds']) == 1

            when('The room title exceed maximum length',
                 form=Update(title='a' * (50 + 1)))
            assert status == '702 Must Be Less Than 50 Charecters'

            when('Title is required', form=Remove('title'))
            assert status == '703 Room Title Is Required'

            when('The room already exist')
            assert status == '615 Room Already Exists'
예제 #18
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'