GitLab is used only for code review, issue tracking and project management. Canonical locations for source code are still https://gitweb.torproject.org/ https://git.torproject.org/ and git-rw.torproject.org.

test_email_service.py 5.31 KB
Newer Older
Hiro's avatar
Hiro committed
1 2
#!/usr/bin/env python3
import pytest
Hiro's avatar
Hiro committed
3 4
import hashlib
from datetime import datetime
Hiro's avatar
Hiro committed
5 6 7 8 9 10 11 12 13 14 15
from twisted.trial import unittest
from twisted.internet import defer, reactor
from twisted.internet import task

from . import conftests

class EmailServiceTests(unittest.TestCase):

    # Fail any tests which take longer than 15 seconds.
    timeout = 15
    def setUp(self):
16
        self.settings = conftests.options.parse_settings("en","tests/test.conf.json")
17
        self.sm_client = conftests.Sendmail(self.settings)
18
        self.locales = conftests.strings.get_locales()
19 20 21 22 23 24 25 26 27 28 29 30
        self.links = [
            [
                "https://gitlab.com/thetorproject/gettorbrowser/raw/torbrowser-releases/TorBrowser-9.0.3-osx64_en-US.dmg",
                "osx",
                "en-US",
                "64",
                "9.0.3",
                "gitlab",
                "ACTIVE",
                "TorBrowser-9.0.3-osx64_en-US.dmg"
            ]
        ]
Hiro's avatar
Hiro committed
31 32 33 34 35 36 37

    def tearDown(self):
        print("tearDown()")

    def test_get_interval(self):
        self.assertEqual(self.settings.get("sendmail_interval"), self.sm_client.get_interval())

Hiro's avatar
Hiro committed
38 39 40 41 42
    def test_help_email_parser(self):
        ep = conftests.EmailParser(self.settings, "gettor@torproject.org")
        request = ep.parse("From: \"silvia [hiro]\" <hiro@torproject.org>\n Subject: help\n Reply-To: hiro@torproject.org \nTo: gettor@torproject.org")
        self.assertEqual(request["command"], "help")

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    def test_normalize_msg(self):
        ep = conftests.EmailParser(self.settings, "gettor@torproject.org")
        msg_str = "From: \"silvia [hiro]\" <hiro@torproject.org>\n Subject: help\n Reply-To: hiro@torproject.org \nTo: gettor@torproject.org"
        msg = conftests.message_from_string(msg_str)
        request = ep.normalize(msg)
        self.assertEqual(request, ('silvia [hiro]', 'hiro@torproject.org', '', 'gettor@torproject.org'))

    def test_validate_msg(self):
        ep = conftests.EmailParser(self.settings, "gettor@torproject.org")
        msg_str = "From: \"silvia [hiro]\" <hiro@torproject.org>\n Subject: help\n Reply-To: hiro@torproject.org \nTo: gettor@torproject.org"
        msg = conftests.message_from_string(msg_str)
        request = ep.validate("hiro@torproject.org", msg)
        assert request

    def test_dkim_verify(self):
        ep = conftests.EmailParser(self.settings, "gettor@torproject.org")
        msg_str = "From: \"silvia [hiro]\" <hiro@torproject.org>\n Subject: help\n Reply-To: hiro@torproject.org \nTo: gettor@torproject.org"
        msg = conftests.message_from_string(msg_str)
        request = ep.dkim_verify(msg, "hiro@torproject.org")
        assert request

    def test_build_request(self):
        ep = conftests.EmailParser(self.settings, "gettor@torproject.org")
        msg_str = "From: \"silvia [hiro]\" <hiro@torproject.org>\n Subject: \r\n Reply-To: hiro@torproject.org \nTo: gettor@torproject.org\r\n osx es"
        msg = conftests.message_from_string(msg_str)
68
        ep.locales = ["es", "en"]
69
        request = ep.build_request(msg_str, "hiro@torproject.org")
70 71 72 73
        self.assertEqual(request["command"], "links")
        self.assertEqual(request["platform"], "osx")
        self.assertEqual(request["language"], "es")

Hiro's avatar
Hiro committed
74 75 76 77 78
    def test_too_many_request_exclude(self):
        ep = conftests.EmailParser(self.settings, "gettor@torproject.org")
        hid = "80d7054da0d3826563c7babb5453e18f3e42f932e562c5ab0434aec9df7b0625"
        limit = self.settings.get("email_requests_limit")
        num_requests = 300
Hiro's avatar
Hiro committed
79
        check = ep.too_many_requests(hid, self.settings.get("test_hid"), num_requests, limit)
Hiro's avatar
Hiro committed
80
        self.assertEqual(hid, self.settings.get("test_hid"))
Hiro's avatar
Hiro committed
81 82
        self.assertEqual(check, False)

Hiro's avatar
Hiro committed
83 84
    def test_language_email_parser(self):
        ep = conftests.EmailParser(self.settings, "gettor@torproject.org")
85
        ep.locales = ["en", "ru"]
Hiro's avatar
Hiro committed
86
        request = ep.parse("From: \"silvia [hiro]\" <hiro@torproject.org>\n Subject: \r\n Reply-To: hiro@torproject.org \nTo: gettor@torproject.org\n osx en")
Hiro's avatar
Hiro committed
87 88 89
        self.assertEqual(request["command"], "links")
        self.assertEqual(request["platform"], "osx")
        self.assertEqual(request["language"], "en")
Hiro's avatar
Hiro committed
90

91 92 93 94 95 96
        request = ep.parse("From: \"silvia [hiro]\" <hiro@torproject.org>\n Subject: \r\n Reply-To: hiro@torproject.org \nTo: gettor@torproject.org\n linux ru")
        self.assertEqual(request["command"], "links")
        self.assertEqual(request["platform"], "linux")
        self.assertEqual(request["language"], "ru")


97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    def test_sent_links_message(self):
        ep = self.sm_client
        links = self.links
        link_msg, file = ep.build_link_strings(links, "osx", "en")
        assert "https://gitlab.com/thetorproject/gettorbrowser/raw/torbrowser-releases/TorBrowser-9.0.3-osx64_en-US.dmg" in link_msg
        assert "osx" in link_msg

        self.assertEqual("TorBrowser-9.0.3-osx64_en-US.dmg", file)

    def test_sent_body_message(self):
        ep = self.sm_client
        links = self.links
        link_msg, file = ep.build_link_strings(links, "osx", "en")
        body_msg = ep.build_body_message(link_msg, "osx", file)
        assert "You requested Tor Browser for osx" in body_msg

    def test_help_body_message(self):
        ep = self.sm_client
        help_msg = ep.build_help_body_message()
        assert "This is how you can request a tor browser bundle link" in help_msg

Hiro's avatar
Hiro committed
118 119 120

if __name__ == "__main__":
    unittest.main()