App Engine Python SDK version 1.8.1
[gae.git] / python / php / sdk / google / appengine / api / mail / MessageTest.php
blobe51863a7a73f57553bcdc5d23abac52156177969
1 <?php
2 /**
3 * Copyright 2007 Google Inc.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 /**
18 * Tests for Mail API on App Engine.
22 require_once 'google/appengine/api/mail_service_pb.php';
23 require_once 'google/appengine/api/mail/Message.php';
24 require_once 'google/appengine/runtime/ApplicationError.php';
25 require_once 'google/appengine/testing/ApiProxyTestBase.php';
27 use google\appengine\base\VoidProto;
28 use google\appengine\api\mail\Message;
29 use google\appengine\MailMessage;
30 use google\appengine\MailServiceError\ErrorCode;
31 use google\appengine\runtime\ApplicationError;
32 use google\appengine\testing\ApiProxyTestBase;
34 class MessageTest extends ApiProxyTestBase {
35 public function testConstructorBadValues() {
36 $options = array("fakeSet" => "test");
37 $this->setExpectedException(
38 "InvalidArgumentException",
39 "Message received an invalid option: fakeSet");
40 $message = new Message($options);
43 public function testConstructNonString() {
44 $options = "test";
45 $this->setExpectedException(
46 "InvalidArgumentException", "Message expects an array, not string");
47 $message = new Message($options);
50 public function testAddAttachmentError() {
51 $message = new Message();
52 $this->setExpectedException(
53 "InvalidArgumentException", "'exe' is a blacklisted file extension.");
54 $message->addAttachment("file.exe", "data");
57 public function testAddAttachmentFilenameTypeError() {
58 $message = new Message();
59 $this->setExpectedException(
60 "InvalidArgumentException",
61 "Filename must be a string but was type integer");
62 $message->addAttachment(7, "data");
65 private function setupAttachmentTest($input) {
66 $message = new Message();
67 $error_message = "";
69 try {
70 $message->addAttachmentArray($input);
71 } catch(InvalidArgumentException $e) {
72 $error_message = $e->getMessage();
75 return $error_message;
78 public function testAddAttachmentArrayError() {
79 $this->assertEquals(
80 $this->setupAttachmentTest("string"),
81 "Input is not an array (Actual type: string).");
83 $attach_arr = array("data.gif" => "data",
84 "data.exe" => "data");
85 $this->assertEquals(
86 $this->setupAttachmentTest($attach_arr),
87 "'exe' is a blacklisted file extension.");
90 private function setupValidEmailTest($email_input) {
91 $message = new Message();
92 $error_message = "";
94 try {
95 $message->addTo($email_input);
96 } catch(InvalidArgumentException $e) {
97 $error_message = $e->getMessage();
100 return $error_message;
103 public function testCheckValidEmails() {
104 $this->assertEquals($this->setupValidEmailTest("invalid.email"),
105 "Invalid 'to' recipient: invalid.email");
107 $array_emails = array("test@test.com", "invalid.email");
108 $this->assertEquals($this->setupValidEmailTest($array_emails),
109 "Invalid 'to' recipient: invalid.email");
112 public function testAddHeaderNonWhitelisted() {
113 $message = new Message();
114 $this->setExpectedException(
115 "InvalidArgumentException",
116 "Input header 'invalid-header: data' is not whitelisted for use with" .
117 " the Google App Engine Mail Service.");
118 $message->addHeader("invalid-header", "data");
121 public function testAddHeaderWrongKeyType() {
122 $message = new Message();
123 $this->setExpectedException(
124 "InvalidArgumentException",
125 "Header key is not a string (Actual type: array).");
126 $message->addHeader(array("not-string"), "data");
129 public function testAddHeaderArray() {
130 $message = new Message();
131 $this->setExpectedException(
132 "InvalidArgumentException",
133 "Input is not an array (Actual type: string).");
134 $message->addHeaderArray("string");
137 public function testSetHtmlBody() {
138 $message = new Message();
139 $this->setExpectedException(
140 "InvalidArgumentException",
141 "HTML text given is not a string (Actual type: array).");
142 $message->setHtmlBody(array("text"));
145 public function testSetReplyTo() {
146 $message = new Message();
147 $this->setExpectedException(
148 "InvalidArgumentException", "Invalid reply-to: invalid.email");
149 $message->setReplyTo("invalid.email");
152 public function testSetSender() {
153 $message = new Message();
154 $this->setExpectedException(
155 "InvalidArgumentException", "Invalid sender: invalid.email");
156 $message->setSender("invalid.email");
159 public function testSetSubject() {
160 $message = new Message();
161 $this->setExpectedException(
162 "InvalidArgumentException",
163 "Subject given is not a string (Actual type: array).");
164 $message->setSubject(array("test"));
167 public function testSetTextBody() {
168 $message = new Message();
169 $this->setExpectedException(
170 "InvalidArgumentException",
171 "Plain text given is not a string (Actual type: array).");
172 $message->setTextBody(array("text"));
175 public function testSendNoSender() {
176 $message = new Message();
177 $this->setExpectedException(
178 "InvalidArgumentException", "Required field sender is not provided.");
179 $message->send();
182 public function testSendNoRecipient() {
183 $message = new Message();
184 $this->setExpectedException(
185 "InvalidArgumentException",
186 "Neither to, cc or bcc is set - at least one is required.");
187 $message->setSender("test@example.com");
188 $message->send();
191 public function testSendNoSubject() {
192 $message = new Message();
193 $this->setExpectedException(
194 "InvalidArgumentException",
195 "Required field subject is not provided.");
196 $message->setSender("test@example.com");
197 $message->addTo("example@test.com");
198 $message->send();
201 public function testSendNoBody() {
202 $message = new Message();
203 $this->setExpectedException(
204 "InvalidArgumentException",
205 "Neither a plain-text nor HTML body is provided - at least one is " .
206 "required.");
207 $message->setSender("test@example.com");
208 $message->addTo("example@test.com");
209 $message->setSubject("test");
210 $message->send();
214 * Setup a basic message and message proto with:
215 * - sender
216 * - to
217 * - subject
218 * - text body
220 private function setupMessageSimple($message, $message_proto) {
221 $message->setSender("test@example.com");
222 $message_proto->setSender("test@example.com");
224 $message->addTo("example@test.com");
225 $message_proto->addTo("example@test.com");
227 $message->setSubject("test");
228 $message_proto->setSubject("test");
230 $message->setTextBody("text body");
231 $message_proto->setTextbody("text body");
235 * Tests various message elements:
236 * - everything in setupMessageSimple()
237 * - to (array)
238 * - bcc, bcc (array)
239 * - cc, cc (array)
240 * - html body
241 * - reply to
243 public function testSucceedFields() {
244 $message = new Message();
245 $message_proto = new MailMessage();
246 $this->setupMessageSimple($message, $message_proto);
248 $message->addTo(array("b@test.com", "c@test.com"));
249 $message_proto->addTo("b@test.com");
250 $message_proto->addTo("c@test.com");
252 $message->addBcc("a@example.com");
253 $message_proto->addBcc("a@example.com");
255 $message->addBcc(array("b@example.com", "c@example.com"));
256 $message_proto->addBcc("b@example.com");
257 $message_proto->addBcc("c@example.com");
259 $message->addCc("d@example.com");
260 $message_proto->addCc("d@example.com");
262 $message->addCc(array("e@example.com", "f@example.com"));
263 $message_proto->addCc("e@example.com");
264 $message_proto->addCc("f@example.com");
266 $message->setHtmlBody("text body");
267 $message_proto->setHtmlbody("text body");
269 $message->setReplyTo("reply@example.com");
270 $message_proto->setReplyto("reply@example.com");
272 $response = new VoidProto();
273 $this->apiProxyMock->expectCall('mail', 'Send', $message_proto, $response);
274 $message->send();
275 $this->apiProxyMock->verify();
278 public function testSucceedWithOptionsArray() {
279 $headers = array('in-reply-to' => 'data',
280 'list-id' => 'data2',
281 'references' => 'data3');
282 $attachments = array('test.gif' => 'data',
283 't.jpg' => 'data2',
284 'z.png' => 'data3');
285 $options = array('sender' => 'test@example.com',
286 'replyto' => 'test@example.com',
287 'to' => array('b@test.com', 'c@test.com'),
288 'cc' => array('d@test.com', 'e@test.com'),
289 'bcc' => array('f@test.com', 'g@test.com'),
290 'subject' => 'test',
291 'textBody' => 'text body',
292 'htmlBody' => 'html body',
293 'header' => $headers,
294 'attachment' => $attachments);
296 $message = new Message($options);
297 $message_proto = new MailMessage();
299 $message_proto->setSender("test@example.com");
300 $message_proto->setReplyto("test@example.com");
301 $message_proto->addTo("b@test.com");
302 $message_proto->addTo("c@test.com");
303 $message_proto->addCc("d@test.com");
304 $message_proto->addCc("e@test.com");
305 $message_proto->addBcc("f@test.com");
306 $message_proto->addBcc("g@test.com");
307 $message_proto->setSubject("test");
308 $message_proto->setTextbody("text body");
309 $message_proto->setHtmlbody("html body");
311 $header = $message_proto->addHeader();
312 $header->setName("in-reply-to");
313 $header->setValue("data");
314 $header = $message_proto->addHeader();
315 $header->setName("list-id");
316 $header->setValue("data2");
317 $header = $message_proto->addHeader();
318 $header->setName("references");
319 $header->setValue("data3");
321 $attach = $message_proto->addAttachment();
322 $attach->setFilename("test.gif");
323 $attach->setData("data");
324 $attach = $message_proto->addAttachment();
325 $attach->setFilename("t.jpg");
326 $attach->setData("data2");
327 $attach = $message_proto->addAttachment();
328 $attach->setFilename("z.png");
329 $attach->setData("data3");
331 $response = new VoidProto();
332 $this->apiProxyMock->expectCall('mail', 'Send', $message_proto, $response);
333 $message->send();
334 $this->apiProxyMock->verify();
337 public function testSucceedWithAttachments() {
338 $message = new Message();
339 $message_proto = new MailMessage();
340 $this->setupMessageSimple($message, $message_proto);
342 $message->addAttachment("test.gif", "data");
343 $attach = $message_proto->addAttachment();
344 $attach->setFilename("test.gif");
345 $attach->setData("data");
347 $multi_attach = array("t" => "data2",
348 "z." => "data3");
349 $message->addAttachmentArray($multi_attach);
350 $attach = $message_proto->addAttachment();
351 $attach->setFilename("t");
352 $attach->setData("data2");
353 $attach = $message_proto->addAttachment();
354 $attach->setFilename("z.");
355 $attach->setData("data3");
357 $response = new VoidProto();
358 $this->apiProxyMock->expectCall('mail', 'Send', $message_proto, $response);
359 $message->send();
360 $this->apiProxyMock->verify();
363 public function testSucceedWithHeaders() {
364 $message = new Message();
365 $message_proto = new MailMessage();
366 $this->setupMessageSimple($message, $message_proto);
368 $message->addHeader("in-reply-to", "data");
369 $header = $message_proto->addHeader();
370 $header->setName("in-reply-to");
371 $header->setValue("data");
373 $multi_header = array("list-id" => "data2", "references" => "data3");
374 $message->addHeaderArray($multi_header);
375 $header = $message_proto->addHeader();
376 $header->setName("list-id");
377 $header->setValue("data2");
378 $header = $message_proto->addHeader();
379 $header->setName("references");
380 $header->setValue("data3");
382 $response = new VoidProto();
383 $this->apiProxyMock->expectCall('mail', 'Send', $message_proto, $response);
384 $message->send();
385 $this->apiProxyMock->verify();
388 public function testRpcApplicationErrorInternalError() {
389 $message = new Message();
390 $message_proto = new MailMessage();
391 $this->setupMessageSimple($message, $message_proto);
393 $exception = new ApplicationError(ErrorCode::INTERNAL_ERROR, "test");
394 $this->setExpectedException("RuntimeException", "test");
396 $this->apiProxyMock->expectCall('mail', 'Send', $message_proto, $exception);
397 $message->send();
398 $this->apiProxyMock->verify();
401 public function testRpcApplicationErrorBadRequest() {
402 $message = new Message();
403 $message_proto = new MailMessage();
404 $this->setupMessageSimple($message, $message_proto);
406 $exception = new ApplicationError(ErrorCode::BAD_REQUEST, "test");
407 $this->setExpectedException("RuntimeException", "test");
409 $this->apiProxyMock->expectCall('mail', 'Send', $message_proto, $exception);
410 $message->send();
411 $this->apiProxyMock->verify();
414 public function testRpcApplicationErrorUnauthorizedSender() {
415 $message = new Message();
416 $message_proto = new MailMessage();
417 $this->setupMessageSimple($message, $message_proto);
419 $exception = new ApplicationError(ErrorCode::UNAUTHORIZED_SENDER, "test");
420 $this->setExpectedException(
421 "InvalidArgumentException",
422 "Mail Service Error: Sender (test@example.com) is not an ".
423 "authorized email address.");
425 $this->apiProxyMock->expectCall('mail', 'Send', $message_proto, $exception);
427 $message->send();
428 $this->apiProxyMock->verify();
431 public function testRpcApplicationErrorInvalidAttachmentType() {
432 $message = new Message();
433 $message_proto = new MailMessage();
434 $this->setupMessageSimple($message, $message_proto);
436 $exception = new ApplicationError(ErrorCode::INVALID_ATTACHMENT_TYPE,
437 "test");
438 $this->setExpectedException(
439 "InvalidArgumentException",
440 "Mail Service Error: Invalid attachment type.");
442 $this->apiProxyMock->expectCall('mail', 'Send', $message_proto, $exception);
444 $message->send();
445 $this->apiProxyMock->verify();
448 public function testRpcApplicationErrorInvalidHeaderName() {
449 $message = new Message();
450 $message_proto = new MailMessage();
451 $this->setupMessageSimple($message, $message_proto);
453 $exception = new ApplicationError(ErrorCode::INVALID_HEADER_NAME, "test");
454 $this->setExpectedException(
455 "InvalidArgumentException",
456 "Mail Service Error: Invalid header name.");
458 $this->apiProxyMock->expectCall('mail', 'Send', $message_proto, $exception);
460 $message->send();
461 $this->apiProxyMock->verify();