]> gitweb.factorcode.org Git - factor.git/blob - basis/smtp/smtp-tests.factor
scryfall: parse mtga deck format
[factor.git] / basis / smtp / smtp-tests.factor
1 USING: accessors assocs combinators concurrency.promises
2 continuations fry io.sockets io.sockets.secure io.streams.string
3 kernel namespaces sequences smtp smtp.private smtp.server
4 sorting system tools.test ;
5 IN: smtp.tests
6
7 : with-test-smtp-config ( quot -- )
8     [
9         <promise> "p" set
10         "p" get mock-smtp-server
11
12         default-smtp-config
13             "localhost" "p" get ?promise <inet> >>server
14             no-auth >>auth
15             os unix? [ t >>tls? ] when
16         \ smtp-config
17     ] dip with-variable ; inline
18
19 { 0 0 } [ [ ] with-smtp-connection ] must-infer-as
20
21 [ "hello\nworld" validate-address ] must-fail
22
23 { "slava@factorcode.org" }
24 [ "slava@factorcode.org" validate-address ] unit-test
25
26 { "aGVsbG8Kd29ybGQ=\r\n.\r\n" } [
27     T{ email { body "hello\nworld" } } [ send-body ] with-string-writer
28 ] unit-test
29
30 [ { "500 syntax error" } <response> check-response ]
31 [ smtp-error? ] must-fail-with
32
33 { } [ { "220 success" } <response> check-response ] unit-test
34
35 { T{ response f 220 { "220 success" } } } [
36     "220 success" [ receive-response ] with-string-reader
37 ] unit-test
38
39 {
40     T{ response f 220 {
41         "220-a multiline response"
42         "250-another line"
43         "220 the end"
44     } }
45 } [
46     "220-a multiline response\r\n250-another line\r\n220 the end"
47     [ receive-response ] with-string-reader
48 ] unit-test
49
50 { } [
51     "220-a multiline response\r\n250-another line\r\n220 the end"
52     [ get-ok ] with-string-reader
53 ] unit-test
54
55 [
56     "Subject:\r\nsecurity hole" validate-header
57 ] must-fail
58
59 {
60     {
61         { "Content-Transfer-Encoding" "base64" }
62         { "Content-Type" "text/plain; charset=UTF-8" }
63         { "From" "Doug <erg@factorcode.org>" }
64         { "MIME-Version" "1.0" }
65         { "Subject" "Factor rules" }
66         { "To" "Slava <slava@factorcode.org>, Ed <dharmatech@factorcode.org>" }
67     }
68     { "slava@factorcode.org" "dharmatech@factorcode.org" }
69     "erg@factorcode.org"
70 } [
71     [
72         <email>
73             "Factor rules" >>subject
74             {
75                 "Slava <slava@factorcode.org>"
76                 "Ed <dharmatech@factorcode.org>"
77             } >>to
78             "Doug <erg@factorcode.org>" >>from
79         {
80             [
81                 email>headers sort-keys [
82                     drop { "Date" "Message-Id" } member? not
83                 ] assoc-filter
84             ]
85             [ to>> [ extract-email ] map ]
86             [ from>> extract-email ]
87             ! To get the smtp server to clean up itself
88             [ '[ _ send-email ] ignore-errors ]
89         } cleave
90     ] with-test-smtp-config
91 ] unit-test
92
93 { } [
94     <secure-config> f >>verify [
95         [
96             <email>
97                 "Hi guys\nBye guys" >>body
98                 "Factor rules" >>subject
99                 {
100                     "Slava <slava@factorcode.org>"
101                     "Ed <dharmatech@factorcode.org>"
102                 } >>to
103                 "Doug <erg@factorcode.org>" >>from
104             send-email
105         ] with-test-smtp-config
106     ] with-secure-context
107 ] unit-test