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'
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'
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'
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
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
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'
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'
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'
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
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
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
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
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'