Exemple #1
0
 def custom_guerrilla_mail_session(self, **kwargs):
     """
     get new email -> read the content -> click link
     """
     aSession = GuerrillaMailSession()
     aSession.get_session_state()
     aSession.set_email_address(kwargs['username'])
     return aSession
Exemple #2
0
def read_email(email):
    s = GuerrillaMailSession()

    s.set_email_address(email)

    print(s.get_session_state())

    for email in s.get_email_list():
        if email.subject == "Poliigon: Email Verification":
            print("Got email")

            body = s.get_email(s.get_email_list()[0].guid).body
            link = body[body.index("https://www.poliigon.com"
                                   ):body.index("https://www.poliigon.com") +
                        71]

            return link
Exemple #3
0
class GuerrillaMailApi(object):
    """
    Api for using Guerrilla Mail.
    """
    def __init__(self, user_name):
        self.session = GuerrillaMailSession()
        self.user_name = user_name
        self.session.set_email_address(self.user_name)
        self.user_email = self.session.get_session_state()['email_address']

    def get_email_text(self, pattern):
        """
        Get email text from GuerrillaMail server.

        Checks the latest email from the inbox and searches for pattern in it.
        If pattern is found then email's body is returned otherwise exception
        is raised. Also, if latest email is not from MIT, it raises exception.

        Checks GuerrillaMail server for email at the intervals of 5 seconds.
        If the email is not found after 40 seconds then exception is raised.

        Arguments:
            pattern(str): Pattern to be found in the email's body.

        Returns:
            str: Email's body.
        """
        email_text = ""
        t_end = time.time() + TIME_OUT_LIMIT
        # Run the loop for a pre defined time
        time.sleep(INITIAL_WAIT_TIME)
        while time.time() < t_end:
            try:
                # Check that mail box is not empty
                email_list = self.session.get_email_list()
                if not email_list:
                    # raise an exception that waits 3 seconds
                    # before restarting loop
                    raise MailException
                # Get the email id of last email in the inbox
                email_id = email_list[0].guid
                # if last email is not sent by MIT raise the exception
                email = self.session.get_email(email_id)
                if email.sender != EMAIL_SENDER_ACCOUNT:
                    raise MailException
                # Fetch the email text and stop the loop
                email_text = email.body
                if pattern not in email_text:
                    raise MailException
                break
            except MailException:
                time.sleep(WAIT_TIME)
        if email_text:
            return email_text
        else:
            raise MailException('No Email from ' + EMAIL_SENDER_ACCOUNT)

    def get_target_url_from_text(self, url_matching_string, text_chunk):
        """
        Search and return the target url from text chunk, the url is searched
        on the basis of partial string embedded in url
        Args:
            url_matching_string:
            text_chunk:
        Returns:
            target url:
        """
        pattern = r"(?P<url>http[s]?://[^\s\"]+(/{}/)[^\s\"]+)".format(
            url_matching_string
        )
        regex_result = re.search(pattern, text_chunk)
        if regex_result:
            target_url = regex_result.group("url")
            return target_url.rstrip('.')
        else:
            return 'Target URL not found in the text'

    def get_url_from_email(self, matching_string):
        """
        Connect to the email client
        Get text of target email
        fetch desired url from the email text
        Args:
            user_email:
            matching_string:
        Returns:
            target url:
        """
        email_text = self.get_email_text(
            self.user_email
        )
        return self.get_target_url_from_text(matching_string, email_text)
Exemple #4
0
class GuerrillaMailApi:
    """
    Api for using Guerrilla Mail.
    """
    def __init__(self, user_name):
        self.session = GuerrillaMailSession()
        self.user_name = user_name
        self.session.set_email_address(self.user_name)
        self.user_email = self.session.get_session_state()['email_address']

    def get_email_text(self, pattern):
        """
        Get email text from GuerrillaMail server.

        Checks the latest email from the inbox and searches for pattern in it.
        If pattern is found then email's body is returned otherwise exception
        is raised. Also, if latest email is not from MIT, it raises exception.

        Checks GuerrillaMail server for email at the intervals of 5 seconds.
        If the email is not found after 40 seconds then exception is raised.

        Arguments:
            pattern(str): Pattern to be found in the email's body.

        Returns:
            str: Email's body.
        """
        email_text = ""
        t_end = time.time() + TIME_OUT_LIMIT
        # Run the loop for a pre defined time
        time.sleep(INITIAL_WAIT_TIME)
        while time.time() < t_end:
            try:
                # Check that mail box is not empty
                email_list = self.session.get_email_list()
                if not email_list:
                    # raise an exception that waits 3 seconds
                    # before restarting loop
                    raise MailException
                # Get the email id of last email in the inbox
                email_id = email_list[0].guid
                # if last email is not sent by MIT raise the exception
                email = self.session.get_email(email_id)
                if email.sender != EMAIL_SENDER_ACCOUNT:
                    raise MailException
                # Fetch the email text and stop the loop
                email_text = email.body
                if pattern not in email_text:
                    raise MailException
                break
            except MailException:
                time.sleep(WAIT_TIME)
        if email_text:
            return email_text
        raise MailException('No Email from ' + EMAIL_SENDER_ACCOUNT)

    def get_url_from_email(self, matching_string):
        """
        Connect to the email client
        Get text of target email
        fetch desired url from the email text
        Args:
            matching_string:
        Returns:
            target url:
        """
        email_text = self.get_email_text(self.user_email)
        return get_target_url_from_text(matching_string, email_text)
class GuerrillaMailSessionTest(TestCase):
    def setup_mocks(self, GuerrillaMailClient, **kwargs):
        self.mock_client = Mock()
        GuerrillaMailClient.return_value = self.mock_client
        self.session = GuerrillaMailSession()

    def test_get_email_state_should_extract_email_address_from_response(
            self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.get_email_address.return_value = {
            'email_addr': '*****@*****.**',
            'sid_token': 1
        }
        email_address = self.session.get_session_state()
        expect(email_address).to.equal({'email_address': '*****@*****.**'})

    def test_get_email_state_should_call_client(self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.get_email_address.return_value = {
            'email_addr': '',
            'sid_token': 1
        }
        self.session.get_session_state()
        self.mock_client.get_email_address.assert_called_once_with(
            session_id=None)

    def test_get_session_state_should_call_client_with_session_id_when_set(
            self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.get_email_address.return_value = {'email_addr': ''}
        self.session.session_id = 1
        self.session.get_session_state()
        self.mock_client.get_email_address.assert_called_once_with(
            session_id=1)

    def test_get_session_state_should_update_session_id_when_included_in_response(
            self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.get_email_address.return_value = {
            'email_addr': '',
            'sid_token': 1
        }
        assert self.session.session_id == None
        self.session.get_session_state()
        expect(self.session.session_id).to.equal(1)

    def test_get_session_state_should_not_update_session_id_when_not_included_in_response(
            self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.get_email_address.return_value = {'email_addr': ''}
        self.session.session_id = 1
        self.session.get_session_state()
        expect(self.session.session_id).to.equal(1)

    def test_get_session_state_should_update_email_timestamp(self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.get_email_address.return_value = {
            'email_addr': '',
            'email_timestamp': 1234,
            'sid_token': 1
        }
        assert self.session.email_timestamp == 0
        self.session.get_session_state()
        expect(self.session.email_timestamp).to.equal(1234)

    def test_get_session_state_should_update_email_address(self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.get_email_address.return_value = {
            'email_addr': '*****@*****.**',
            'email_timestamp': 1234,
            'sid_token': 1,
        }
        assert self.session.email_timestamp == 0
        self.session.get_session_state()
        expect(self.session.email_address).to.equal('*****@*****.**')

    def test_get_session_state_should_use_cached_data_when_available_and_current(
            self, **kwargs):
        self.setup_mocks(**kwargs)
        self.session.session_id = 1
        self.session.email_address = '*****@*****.**'
        self.session.email_timestamp = current_timestamp()
        self.session.get_session_state()
        expect(self.mock_client.get_email_address.called).to.equal(False)
        expect(self.mock_client.set_email_address.called).to.equal(False)

    def test_set_email_address_should_return_none(self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.set_email_address.return_value = {
            'email_addr': '*****@*****.**'
        }
        result = self.session.set_email_address('newaddr')
        expect(result).to.be.none

    def test_set_email_address_should_call_client(self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.set_email_address.return_value = {'email_addr': ''}
        self.session.set_email_address('newaddr')
        self.mock_client.set_email_address.assert_called_once_with(
            session_id=None, address_local_part='newaddr')

    def test_set_email_address_should_call_client_with_session_id_when_set(
            self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.set_email_address.return_value = {'email_addr': ''}
        self.session.session_id = 1
        self.session.set_email_address('newaddr')
        self.mock_client.set_email_address.assert_called_once_with(
            session_id=1, address_local_part='newaddr')

    def test_set_email_address_should_update_session_id_when_included_in_response(
            self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.set_email_address.return_value = {
            'email_addr': '',
            'sid_token': 1
        }
        assert self.session.session_id == None
        self.session.set_email_address('newaddr')
        expect(self.session.session_id).to.equal(1)

    def test_set_email_address_should_not_update_session_id_when_not_included_in_response(
            self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.set_email_address.return_value = {'email_addr': ''}
        self.session.session_id = 1
        self.session.set_email_address('newaddr')
        expect(self.session.session_id).to.equal(1)

    def test_set_email_address_should_update_email_timestamp(self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.set_email_address.return_value = {
            'email_addr': '',
            'email_timestamp': 1234
        }
        assert self.session.email_timestamp == 0
        self.session.set_email_address('newaddr')
        expect(self.session.email_timestamp).to.equal(1234)

    def test_set_email_address_should_update_email_address(self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.set_email_address.return_value = {
            'email_addr': '*****@*****.**',
            'email_timestamp': 1234
        }
        assert self.session.email_timestamp == 0
        self.session.set_email_address('newaddr')
        expect(self.session.email_address).to.equal('*****@*****.**')

    def test_get_email_list_should_extract_response_list(self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.get_email_list.return_value = {'list': []}
        self.session.session_id = 1
        self.session.email_timestamp = current_timestamp()
        email_list = self.session.get_email_list()
        expect(email_list).to.have.length_of(0)

    def test_get_email_list_should_create_mail_instances_from_response_list(
            self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.get_email_list.return_value = {
            'list': [{
                'mail_id': '1',
                'mail_subject': 'Hello',
                'mail_from': '*****@*****.**',
                'mail_timestamp': '1392501749',
                'mail_read': '0',
                'mail_exerpt': 'Hi there....',
            }]
        }
        self.session.session_id = 1
        self.session.email_timestamp = current_timestamp()
        email_list = self.session.get_email_list()
        email = email_list[0]
        expect(email_list).to.have.length_of(1)
        expect(email).to.have.property('guid').with_value.being.equal('1')
        expect(email).to.have.property('subject').with_value.being.equal(
            'Hello')
        expect(email).to.have.property('sender').with_value.being.equal(
            '*****@*****.**')
        expect(email).to.have.property('datetime').with_value.being.equal(
            datetime(2014, 2, 15, 22, 2, 29, tzinfo=utc))
        expect(email).to.have.property('read').with_value.being.false
        expect(email).to.have.property('exerpt').with_value.being.equal(
            'Hi there....')

    def test_get_email_list_should_call_client(self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.get_email_list.return_value = {'list': []}
        self.session.session_id = 1
        self.session.email_timestamp = current_timestamp()
        self.session.get_email_list()
        self.mock_client.get_email_list.assert_called_once_with(session_id=1,
                                                                offset=0)

    def test_get_email_list_should_call_client_with_session_id_when_set(
            self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.get_email_list.return_value = {'list': []}
        self.session.session_id = 1
        self.session.email_timestamp = current_timestamp()
        self.session.get_email_list()
        self.mock_client.get_email_list.assert_called_once_with(session_id=1,
                                                                offset=0)

    def test_get_email_list_should_update_session_id_when_included_in_response(
            self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.get_email_list.return_value = {
            'list': [],
            'sid_token': 1
        }
        self.session.session_id = 0
        self.session.email_timestamp = current_timestamp()
        self.session.get_email_list()
        expect(self.session.session_id).to.equal(1)

    def test_get_email_list_should_not_update_session_id_when_not_included_in_response(
            self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.get_email_list.return_value = {'list': []}
        self.session.session_id = 1
        self.session.email_timestamp = current_timestamp()
        self.session.get_email_list()
        expect(self.session.session_id).to.equal(1)

    def test_get_email_list_should_not_invoke_get_address_when_session_id_set_and_not_expired(
            self, **kwargs):
        self.setup_mocks(**kwargs)
        self.session.session_id = 1
        self.session.email_timestamp = current_timestamp()
        self.mock_client.get_email_list.return_value = {'list': []}
        self.session.get_email_list()
        expect(self.mock_client.get_email_address.called).to.equal(False)

    def test_get_email_list_should_first_create_session_when_session_id_not_set(
            self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.get_email_list.return_value = {'list': []}
        self.mock_client.get_email_address.return_value = {
            'sid_token': '1',
            'email_addr': ''
        }
        self.session.email_timestamp = current_timestamp()
        assert self.session.session_id == None
        self.session.get_email_list()
        expect(self.session.session_id).to.equal('1')
        self.mock_client.get_email_list.assert_called_once_with(session_id='1',
                                                                offset=0)

    def test_get_email_list_should_first_create_session_and_reuse_address_when_session_id_not_set(
            self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.get_email_list.return_value = {'list': []}
        self.mock_client.set_email_address.return_value = {
            'sid_token': '1',
            'email_addr': ''
        }
        self.session.email_timestamp = current_timestamp()
        self.session.email_address = '*****@*****.**'
        assert self.session.session_id == None
        self.session.get_email_list()
        expect(self.session.session_id).to.equal('1')
        self.mock_client.get_email_list.assert_called_once_with(session_id='1',
                                                                offset=0)

    def test_get_email_list_should_fail_when_session_cannot_be_obtained(
            self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.get_email_address.return_value = {'email_addr': ''}
        assert self.session.session_id == None
        expect(self.session.get_email_list).when.called.to.throw(
            GuerrillaMailException)
        expect(self.mock_client.get_email_list.called).to.equal(False)

    def test_get_email_list_should_refresh_session_when_email_expired(
            self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.get_email_list.return_value = {'list': []}
        self.mock_client.get_email_address.return_value = {
            'email_addr': '',
            'sid_token': '2',
            'email_timestamp': 1234
        }
        self.session.session_id = 1
        self.session.email_timestamp = current_timestamp() - 3600
        self.session.get_email_list()
        expect(self.session.session_id).to.equal('2')
        expect(self.session.email_timestamp).to.equal(1234)
        self.mock_client.get_email_list.assert_called_once_with(session_id='2',
                                                                offset=0)

    def test_get_email_list_should_refresh_session_and_reuse_address_when_email_expired(
            self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.get_email_list.return_value = {'list': []}
        self.mock_client.set_email_address.return_value = {
            'email_addr': '',
            'sid_token': '2',
            'email_timestamp': 1234
        }
        self.session.session_id = 1
        self.session.email_address = '*****@*****.**'
        self.session.email_timestamp = current_timestamp() - 3600
        self.session.get_email_list()
        expect(self.session.session_id).to.equal('2')
        expect(self.session.email_timestamp).to.equal(1234)
        self.mock_client.get_email_list.assert_called_once_with(session_id='2',
                                                                offset=0)

    def test_get_email_list_should_not_refresh_session_when_email_not_expired(
            self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.get_email_list.return_value = {'list': []}
        self.session.session_id = 1
        self.session.email_timestamp = current_timestamp() - 3590
        self.session.get_email_list()
        expect(self.mock_client.get_email_address.called).to.equal(False)

    def test_get_email_should_create_mail_instance_from_client_response_data(
            self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.get_email.return_value = {
            'mail_id': '1',
            'mail_subject': 'Hello',
            'mail_from': '*****@*****.**',
            'mail_timestamp': '1392501749',
            'mail_read': '0',
            'mail_exerpt': 'Hi there....',
            'mail_body': 'Hi there partner',
        }
        email = self.session.get_email('123')
        expect(email).to.have.property('guid').with_value.being.equal('1')
        expect(email).to.have.property('subject').with_value.being.equal(
            'Hello')
        expect(email).to.have.property('sender').with_value.being.equal(
            '*****@*****.**')
        expect(email).to.have.property('datetime').with_value.being.equal(
            datetime(2014, 2, 15, 22, 2, 29, tzinfo=utc))
        expect(email).to.have.property('read').with_value.being.false
        expect(email).to.have.property('exerpt').with_value.being.equal(
            'Hi there....')
        expect(email).to.have.property('body').with_value.being.equal(
            'Hi there partner')

    def test_get_email_should_call_client(self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.get_email.return_value = {}
        self.session.get_email('123')
        self.mock_client.get_email.assert_called_once_with(email_id='123',
                                                           session_id=None)

    def test_get_email_should_call_client_with_session_id_when_set(
            self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.get_email.return_value = {}
        self.session.session_id = 1
        self.session.get_email('123')
        self.mock_client.get_email.assert_called_once_with(email_id='123',
                                                           session_id=1)

    def test_get_email_should_update_session_id_when_included_in_response(
            self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.get_email.return_value = {'sid_token': 1}
        assert self.session.session_id == None
        self.session.get_email('123')
        expect(self.session.session_id).to.equal(1)

    def test_get_email_should_not_update_session_id_when_not_included_in_response(
            self, **kwargs):
        self.setup_mocks(**kwargs)
        self.mock_client.get_email.return_value = {}
        self.session.session_id = 1
        self.session.get_email('123')
        expect(self.session.session_id).to.equal(1)