Ejemplo n.º 1
0
def test_messaging_model(db):
    __configuration__ = '''
    messaging:
      default_sender: [email protected]
      default_messenger: restfulpy.mockup.MockupMessenger
    '''

    settings.merge(__configuration__)
    session = db()

    mockup_messenger = create_messenger()

    message = Email(to='*****@*****.**',
                    subject='Test Subject',
                    body={'msg': 'Hello'})

    session.add(message)
    session.commit()

    message.do_({})

    assert mockup_messenger.last_message == {
        'body': {
            'msg': 'Hello'
        },
        'subject': 'Test Subject',
        'to': '*****@*****.**'
    }
    def test_reset_password(self):
        session = self.create_session()
        messanger = create_messenger()
        email = '*****@*****.**'
        password = '******'

        hash_old_password = session.query(Member).one().password

        with self.given('Ask reset password token',
                        '/apiv1/resetpasswordtokens',
                        'ASK',
                        form=dict(email=email)):
            assert status == 200

            task = ResetPasswordEmail.pop()
            task.do_(None)

            reset_password_token =\
                messanger.last_message['body']['reset_password_token']

        with self.given('Reset your CAS account password',
                        '/apiv1/passwords',
                        'RESET',
                        form=dict(password=password,
                                  resetPasswordToken=reset_password_token)):
            assert status == 200

            hash_new_password = session.query(Member).one().password
            assert hash_new_password != hash_old_password

            when('Trying to pass a short password',
                 form=Update(password='******'))
            assert status == '702 Invalid Password Length'

            when('Trying to a pass long password',
                 form=Update(password='******'))
            assert status == '702 Invalid Password Length'

            when('Request without password parameter', form=Remove('password'))
            assert status == '702 Invalid Password Length'

            when('The token has been damaged',
                 form=Update(resetPasswordToken='token'))
            assert status == '611 Malformed Token'

            when('Trying to pass with empty form', form={})
            assert status == '400 Empty Form'
    def test_ask_reset_password_tokens(self):
        messanger = create_messenger()
        email = '*****@*****.**'

        with self.given('Ask a reset password token',
                        '/apiv1/resetpasswordtokens',
                        'ASK',
                        form=dict(email=email)):
            assert status == 200
            assert response.json['email'] == email

            task = ResetPasswordEmail.pop()
            task.do_(None)

            assert messanger.last_message['to'] == email

            assert settings.reset_password.callback_url == \
                messanger.last_message['body']['reset_password_callback_url']

            assert messanger.last_message['subject'] ==\
                'Reset your CAS account password'

            when('Email not contain @', form=Update(email='userexample.com'))
            assert status == '701 Invalid Email Format'

            when('Email not contain dot', form=Update(email='user@examplecom'))
            assert status == '701 Invalid Email Format'

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

            when('Email not contains any domain',
                 form=Update(email='[email protected]'))
            assert status == '701 Invalid Email Format'

            when('Email address is already registered',
                 form=Update(email='*****@*****.**'))
            assert status == 200
            assert response.json['email'] == '*****@*****.**'

            when('Request without email parametes',
                 form=given_form - 'email' + dict(a='a'))
            assert status == '701 Invalid Email Format'

            when('Trying to pass with empty form', form={})
            assert status == '400 Empty Form'
Ejemplo n.º 4
0
    def test_claim_email_ownership(self):
        messanger = create_messenger()
        email = '*****@*****.**'

        with self.given('Claim a email',
                        '/apiv1/emails',
                        'CLAIM',
                        form=dict(email=email)):
            assert response.status == 200
            assert 'email' in response.json
            assert response.json['email'] == email

            task = RegisterEmail.pop()
            task.do_(None)

            assert messanger.last_message['to'] == email

            assert settings.registeration.callback_url == \
                messanger.last_message['body']['registeration_callback_url']

            assert messanger.last_message['subject'] == \
                'Register your CAS account'

            when('Email not contain @', form=Update(email='userexample.com'))
            assert status == '701 Invalid Email Format'

            when('Email not contain dot', form=Update(email='user@examplecom'))
            assert status == '701 Invalid Email Format'

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

            when('Email not contains any domain',
                 form=Update(email='[email protected]'))
            assert status == '701 Invalid Email Format'

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

            when('Request without email parametes',
                 form=given_form - 'email' + dict(a='a'))
            assert status == '701 Invalid Email Format'

            when('Trying to pass with empty form', form={})
            assert status == '400 Empty Form'
    def test_messaging_model(self):
        mockup_messenger = create_messenger()

        # noinspection PyArgumentList
        message = Welcome(to='*****@*****.**',
                          subject='Test Subject',
                          body={'msg': 'Hello'})

        DBSession.add(message)
        DBSession.commit()

        message.do_({})

        # noinspection PyUnresolvedReferences
        self.assertDictEqual(
            mockup_messenger.last_message, {
                'body': {
                    'msg': 'Hello'
                },
                'subject': 'Test Subject',
                'to': '*****@*****.**'
            })
Ejemplo n.º 6
0
    def test_register_member(self):
        messanger = create_messenger()
        email = '*****@*****.**'
        title = 'nickname'
        password = '******'

        with self.given(
            'Claim a email',
            '/apiv1/emails',
            'CLAIM',
            form=dict(email=email)
        ):
            assert status == 200
            assert 'email' in response.json
            assert response.json['email'] == email

            task = RegisterEmail.pop()
            task.do_(None)
            registeration_token = \
                messanger.last_message['body']['registeration_token']

        with self.given(
            'Register a member',
            '/apiv1/members',
            'REGISTER',
            form=dict(
                email=email,
                title=title,
                password=password,
                ownershipToken=registeration_token
            )
        ):
            assert status == 200
            assert response.json['title'] == title
            assert response.json['email'] == email
            assert 'id' in response.json
            assert 'X-New-JWT-Token' in response.headers

            when('Invalid password min length', form=Update(password='******'))
            assert status == '702 Invalid Password Length'

            when(
                'Invalid password max length',
                form=Update(password='******')
            )
            assert status == '702 Invalid Password Length'

            when(
                'Invalid title format',
                form=Update(password='******', title='1username')
            )
            assert status == '705 Invalid Title Format'

            when ('Duplicate title', form=Update(title='username'))
            assert status == '604 Title Is Already Registered'

            when ('Duplicate Email', form=Update(title='user_name'))
            assert status == '601 Email Address Is Already Registered'

            when (
                'The toekn has been damaged',
                form=Update(title='user_name', ownershipToken='token')
            )
            assert status == '611 Malformed Token'

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