| 1 | // vtest build: !windows |
| 2 | import os |
| 3 | import net.smtp |
| 4 | import time |
| 5 | |
| 6 | // Used to test that a function call returns an error |
| 7 | fn fn_errors(mut c smtp.Client, m smtp.Mail) bool { |
| 8 | c.send(m) or { return true } |
| 9 | return false |
| 10 | } |
| 11 | |
| 12 | fn send_mail(starttls bool) { |
| 13 | client_cfg := smtp.Config{ |
| 14 | server: 'smtp.mailtrap.io' |
| 15 | port: 465 |
| 16 | from: '[email protected]' |
| 17 | username: os.getenv('VSMTP_TEST_USER') |
| 18 | password: os.getenv('VSMTP_TEST_PASS') |
| 19 | starttls: starttls |
| 20 | } |
| 21 | if client_cfg.username == '' && client_cfg.password == '' { |
| 22 | eprintln('Please set VSMTP_TEST_USER and VSMTP_TEST_PASS before running this test') |
| 23 | exit(0) |
| 24 | } |
| 25 | send_cfg := smtp.Mail{ |
| 26 | to: '[email protected]' |
| 27 | subject: 'Hello from V2' |
| 28 | body: 'Plain text' |
| 29 | } |
| 30 | |
| 31 | mut client := smtp.new_client(client_cfg) or { |
| 32 | assert false |
| 33 | return |
| 34 | } |
| 35 | assert true |
| 36 | client.send(send_cfg) or { |
| 37 | assert false |
| 38 | return |
| 39 | } |
| 40 | assert true |
| 41 | client.send(smtp.Mail{ |
| 42 | ...send_cfg |
| 43 | from: '[email protected]' |
| 44 | }) or { |
| 45 | assert false |
| 46 | return |
| 47 | } |
| 48 | client.send(smtp.Mail{ |
| 49 | ...send_cfg |
| 50 | cc: '[email protected],[email protected]' |
| 51 | bcc: '[email protected]' |
| 52 | }) or { |
| 53 | assert false |
| 54 | return |
| 55 | } |
| 56 | client.send(smtp.Mail{ |
| 57 | ...send_cfg |
| 58 | date: time.now().add_days(1000) |
| 59 | }) or { |
| 60 | assert false |
| 61 | return |
| 62 | } |
| 63 | assert true |
| 64 | client.quit() or { |
| 65 | assert false |
| 66 | return |
| 67 | } |
| 68 | assert true |
| 69 | // This call should return an error, since the connection is closed |
| 70 | if !fn_errors(mut client, send_cfg) { |
| 71 | assert false |
| 72 | return |
| 73 | } |
| 74 | client.reconnect() or { |
| 75 | assert false |
| 76 | return |
| 77 | } |
| 78 | client.send(send_cfg) or { |
| 79 | assert false |
| 80 | return |
| 81 | } |
| 82 | assert true |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * |
| 87 | * smtp_test |
| 88 | * Created by: nedimf (07/2020) |
| 89 | */ |
| 90 | fn test_smtp() { |
| 91 | $if !network ? { |
| 92 | return |
| 93 | } |
| 94 | |
| 95 | // Test sending without STARTTLS |
| 96 | send_mail(false) |
| 97 | |
| 98 | // Sleep for 10 seconds to reset the Mailtrap rate limit counter |
| 99 | // See: https://help.mailtrap.io/article/44-features-and-limits#rate-limit |
| 100 | time.sleep(10000 * time.millisecond) |
| 101 | |
| 102 | // Test with STARTTLS |
| 103 | send_mail(true) |
| 104 | } |
| 105 | |
| 106 | fn test_smtp_implicit_ssl() { |
| 107 | $if !network ? { |
| 108 | return |
| 109 | } |
| 110 | |
| 111 | client_cfg := smtp.Config{ |
| 112 | server: 'smtp.gmail.com' |
| 113 | port: 465 |
| 114 | from: '' |
| 115 | username: '' |
| 116 | password: '' |
| 117 | ssl: true |
| 118 | } |
| 119 | |
| 120 | mut client := smtp.new_client(client_cfg) or { |
| 121 | assert false |
| 122 | return |
| 123 | } |
| 124 | |
| 125 | assert client.is_open && client.encrypted |
| 126 | } |
| 127 | |
| 128 | fn test_new_client_rejects_conflicting_tls_modes() { |
| 129 | client_cfg := smtp.Config{ |
| 130 | server: 'smtp.example.com' |
| 131 | ssl: true |
| 132 | starttls: true |
| 133 | } |
| 134 | |
| 135 | smtp.new_client(client_cfg) or { |
| 136 | assert err.msg() == 'Can not use both implicit SSL and STARTTLS' |
| 137 | return |
| 138 | } |
| 139 | |
| 140 | assert false |
| 141 | } |
| 142 | |
| 143 | fn test_smtp_multiple_recipients() { |
| 144 | $if !network ? { |
| 145 | return |
| 146 | } |
| 147 | |
| 148 | assert true |
| 149 | } |
| 150 | |
| 151 | fn test_smtp_body_base64encode() { |
| 152 | $if !network ? { |
| 153 | return |
| 154 | } |
| 155 | |
| 156 | assert true |
| 157 | } |
| 158 | |