Ejemplo n.º 1
0
class DbQueriesTestCaise(TestCase):

    def setUp(self):
        self.queries = DBQueries()
        self.queries.clear_tables()
        self.queries.fill_users()
        self.queries.fill_emails()

    def tearDown(self):
        self.queries.clear_tables()

    def test_user_exists(self):
        self.assertTrue(self.queries.user_exists("Pablo"))
        self.assertFalse(self.queries.user_exists("Hose"))

    def test_authenticate_user(self):
        self.assertTrue(self.queries.authenticate_user("Pablo", "Pikasso"))
        self.assertFalse(self.queries.authenticate_user("Pablo", "qwerty"))

    def test_passw_hash(self):
        passw_hash = hashlib.sha224("qwerty").hexdigest()
        self.assertEqual(self.queries.passw_hash("qwerty"), passw_hash)
        self.assertNotEqual(self.queries.passw_hash("asdfg"), passw_hash)

    def test_get_user_emails(self):
        self.assertEqual(self.queries.get_user_emails("Khil"), ["1 Khil's email"])
        self.assertNotEqual(self.queries.get_user_emails("Pablo"), ["1 Khil's email"])

    def test_get_email(self):
        expected_subject = "Pablo's email"
        expected_body = ";ksjhdflvkahbdcjsdhbfcsjkdavkjsdhvcakjs"
        subject, body = self.queries.get_email(2)
        self.assertEqual(subject, expected_subject)
        self.assertEqual(body, expected_body)
        subject, body = self.queries.get_email(1)
        self.assertNotEqual(subject, expected_subject)
        self.assertNotEqual(body, expected_body)
Ejemplo n.º 2
0
class POP3Handler(SocketServer.BaseRequestHandler):
    """
    The RequestHandler class for our server.

    It is instantiated once per connection to the server, and must
    override the handle() method to implement communication to the
    client.
    """
    ERR = "-ERR\r\n"
    OK = "+OK\r\n"
    def setup(self):
        self.request.sendall("Welcome to POP server, 127.0.0.1!\r\n")
        self.AUTHORIZATION = True
        self.user = None
        self.TRANSACTION = False
        self.queries = DBQueries()
        self.accepted_methods = dict((("USER", self.user_method), ("PASS", self.pass_method),
                                      ("LIST", self.list_method), ("RETR", self.retr_method),
                                      ("QUIT", self.quit_method)))

    def handle(self):
        while self.AUTHORIZATION:
            self.process_request_data()

    def process_request_data(self):
        data = self.request.recv(1024).strip()
        self.data = data.split()
        if self.data:
            POP3_method = self.data[0]
            if POP3_method in self.accepted_methods:
                self.accepted_methods[POP3_method]()
            else:
                self.request.sendall("Method is not accepted!\r\n")

    # implements QUIT POP3 method behavior
    def quit_method(self):
        self.AUTHORIZATION = False
        self.request.sendall("Bye!\r\n")

    # implements USER POP3 method behavior
    def user_method(self):
        if not self.TRANSACTION:
            if len(self.data) > 1:
                self.user = self.data[1]
                if self.queries.user_exists(self.user):
                    self.request.sendall(self.OK)
                    return
        self.request.sendall(self.ERR)

    # implements PASS POP3 method behavior
    def pass_method(self):
        if not self.TRANSACTION:
            if len(self.data) > 1:
                passw = self.data[1]
                if self.user:
                    if self.queries.authenticate_user(self.user, passw):
                        self.TRANSACTION = True
                        self.request.sendall(self.OK)
                        return
        self.request.sendall(self.ERR)

    # implements LIST POP3 method behavior
    def list_method(self):
        if self.TRANSACTION:
            emails = self.queries.get_user_emails(self.user)
            if emails:
                str_list = [self.OK, "You've got {} letters in your mailbox\r\n".format(len(emails)),
                            "\r\n".join(emails), "\r\nFeel free to fetch any with RETR command\r\n"]
                self.request.sendall("".join(str_list))
                return
        self.request.sendall(self.ERR)

    # implements RETR POP3 method behavior
    def retr_method(self):
        if len(self.data) > 1:
            id = self.data[1]
            subject, body = self.queries.get_email(id)
            self.request.sendall("Subject: {subject}\r\n{body}".format(subject=subject, body=body))
            return
        self.request.sendall(self.ERR)