Odyssey
emailPyTests.prg
1 #!/usr/bin/php
2 <?php
3 /**
4  * File: emailPyTests.prg
5  * Purpose: Tests the AWS SES python script
6  */
7 
8 error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
9 
10 // Includes necessary files
11 $sharedLibrary = "/var/www/html/shared/library";
12 require_once("$sharedLibrary/hcuCommon.i");
13 
14 // Command line only takes one argument: the test to run.
15 if (count($argv) >= 2) {
16  switch($argv[1]) {
17  case "verify":
18  TestVerification();
19  break;
20  case "all":
21  $sepLength = GetSeparatorLength();
22 
23  print "\n\nTest Verification\n" . str_repeat("+", $sepLength) . "\n" . str_repeat("=", $sepLength) . "\n\n\n";
24  TestVerification();
25  break;
26  default:
27  print "Test is not valid.\n";
28  }
29 } else {
30  print "Command line only takes one argument: the test to run.\n";
31 }
32 
33 /**
34  * function GetSeparatorLength()
35  * @return the number of characters to spit out for the dividers between tests.
36  */
37 function GetSeparatorLength() {
38  return 40;
39 }
40 
41 /**
42  * function AssertEquals($title, $testThis, $needsToEqual)
43  * Tests if two strings or numbers are equal. Prints out the results.
44  *
45  * @param $title -- the title of the test.
46  * @param $testThis -- the variable to check.
47  * @param $needsToEqual -- the literal that the variable needs to equal for success.
48  */
49 function AssertEquals($title, $testThis, $needsToEqual) {
50  $testThis = trim($testThis);
51  $needsToEqual = trim($needsToEqual);
52  if ($testThis == $needsToEqual) {
53  print "$title was successful.\n";
54  } else {
55  print "$title failed.\n";
56  print "Expected: '$needsToEqual'\n";
57  print "Received: '$testThis'\n";
58  }
59 }
60 
61 /**
62  * function AssertStartsWith($title, $testThis, $needsToEqual)
63  * Tests if one string starts with string. Prints out the results.
64  *
65  * @param $title -- the title of the test.
66  * @param $testThis -- the variable to check.
67  * @param $needsToEqual -- the literal that the variable needs to equal for success.
68  */
69 function AssertStartsWith($title, $testThis, $needsToEqual) {
70  $testThis = trim($testThis);
71  $needsToEqual = trim($needsToEqual);
72  if (substr($testThis, 0, strlen($needsToEqual)) == $needsToEqual) {
73  print "$title was successful.\n";
74  } else {
75  print "$title failed.\n";
76  print "Expected: '$needsToEqual'\n";
77  print "Received: '$testThis'\n";
78  }
79 }
80 
81 /**
82  * function AssertNotContains($title, $testThis, $needsToEqual)
83  * Tests if one string starts with string. Prints out the results.
84  *
85  * @param $title -- the title of the test.
86  * @param $testThis -- the variable to check.
87  * @param $needsToEqual -- the literal that the variable needs to equal for success.
88  */
89 function AssertNotContains($title, $testThis, $contains) {
90  $testThis = trim($testThis);
91  $needsToEqual = trim($needsToEqual);
92  if (strpos($testThis, $contains) === false) {
93  print "$title was successful.\n";
94  } else {
95  print "$title failed.\n";
96  print "Expected: '$needsToEqual'\n";
97  print "Received: '$testThis'\n";
98  }
99 }
100 
101 /**
102  * function RunPyFile($action, $emailList)
103  * Runs the python file.
104  *
105  * @param $action -- the action: "verifyEmails", "getVerification", or "removeEmails."
106  * @param $emailList -- all the emails to run for the command.
107  *
108  * @return the results of the python script decoded into an array.
109  */
110 function RunPyFile($action, $emailList) {
111  $sharedScripts = "/var/www/html/shared/scripts";
112  $pyFile = "$sharedScripts/verifySESEmail.py";
113  $cmd = "python3 $pyFile" . (isset($action) ? " -a " . escapeshellarg($action) : "");
114 
115  $emails = "";
116  if (isset($emailList) && is_array($emailList) && count($emailList) > 0) {
117  foreach($emailList as $email) {
118  if ($emails == "") {
119  $emails = " -e \"" . str_replace('"', '\x22', $email);
120  } else {
121  $emails .= " " . str_replace('"', '\x22', $email);
122  }
123  }
124  $emails .= "\"";
125  }
126  $cmd .= $emails;
127 
128  return HCU_JsonDecode(exec($cmd));
129 }
130 
131 /**
132  * function TestVerification()
133  * Test the python script with each of the three script options.
134  */
135 function TestVerification() {
136 
137  // homecutester1@gmail.com -- verified email.
138  // homecutester2@gmail.com -- failed email permanently (ran the first time only.)
139  // homecutester3@gmail.com -- failed email temporarily. Verify email call will reset it to pending.
140  // homecutester4@gmail.com -- tests removing identity.
141  // homecutester5@gmail.com -- never added.
142 
143  print "\n\nTesting Invalid call...\n" . str_repeat("*", $sepLength) . "\n";
144  $results = RunPyFile(null, null);
145  $testn = 1;
146  AssertEquals("Test " . ($testn ++) . ": invalid call", $results["error"], "verifySESEmail.py -e \"email1 email2 email3 ... emailN\" -a \"verifyEmails\"|\"getVerification\"|\"removeEmails\"");
147 
148  print "\n\nTesting getting statuses of identities...\n" . str_repeat("*", $sepLength) . "\n";
149  $results = RunPyFile("getVerification", array("homecutester1@gmail.com", "homecutester2@gmail.com", "homecutester3@gmail.com", "homecutester4@gmail.com", "homecutester5@gmail.com"));
150  AssertEquals("Test " . ($testn ++) . ": no errors", $results["status"], "000");
151  $verify = $results["response"]["VerificationAttributes"];
152  AssertEquals("Test " . ($testn ++) . ": verification attributes", is_array($verify), true);
153  AssertEquals("Test " . ($testn ++) . ": homecutester1@gmail.com", $verify["homecutester1@gmail.com"]["VerificationStatus"], "Success");
154  AssertEquals("Test " . ($testn ++) . ": homecutester2@gmail.com", $verify["homecutester2@gmail.com"]["VerificationStatus"], "Failed");
155  AssertEquals("Test " . ($testn ++) . ": homecutester3@gmail.com", $verify["homecutester3@gmail.com"]["VerificationStatus"], "Failed");
156  AssertEquals("Test " . ($testn ++) . ": homecutester4@gmail.com", $verify["homecutester4@gmail.com"]["VerificationStatus"], "");
157  AssertEquals("Test " . ($testn ++) . ": homecutester5@gmail.com", $verify["homecutester5@gmail.com"]["VerificationStatus"], "");
158 
159  print "\n\nTesting sending verification emails...\n" . str_repeat("*", $sepLength) . "\n";
160  $results = RunPyFile("verifyEmails", array("homecutester1@gmail.com", "homecutester3@gmail.com", "homecutester4@gmail.com", "homecutest"));
161  AssertEquals("Test " . ($testn ++) . ": invalid email syntax", $results["error"], "homecutest is not valid");
162  AssertEquals("Test " . ($testn ++) . ": returned success from other emails", $results["response"]["homecutester1@gmail.com"]["ResponseMetadata"]["HTTPHeaders"]["content-type"], "text/xml");
163 
164  print "\n\nTesting getting statuses of identities...\n" . str_repeat("*", $sepLength) . "\n";
165  $results = RunPyFile("getVerification", array("homecutester1@gmail.com", "homecutester2@gmail.com", "homecutester3@gmail.com", "homecutester4@gmail.com", "homecutester5@gmail.com"));
166  AssertEquals("Test " . ($testn ++) . ": no errors", $results["status"], "000");
167  $verify = $results["response"]["VerificationAttributes"];
168  AssertEquals("Test " . ($testn ++) . ": verification attributes", is_array($verify), true);
169  AssertEquals("Test " . ($testn ++) . ": homecutester1@gmail.com", $verify["homecutester1@gmail.com"]["VerificationStatus"], "Success");
170  AssertEquals("Test " . ($testn ++) . ": homecutester2@gmail.com", $verify["homecutester2@gmail.com"]["VerificationStatus"], "Failed");
171  AssertEquals("Test " . ($testn ++) . ": homecutester3@gmail.com", $verify["homecutester3@gmail.com"]["VerificationStatus"], "Pending");
172  AssertEquals("Test " . ($testn ++) . ": homecutester4@gmail.com", $verify["homecutester4@gmail.com"]["VerificationStatus"], "Pending");
173  AssertEquals("Test " . ($testn ++) . ": homecutester5@gmail.com", $verify["homecutester5@gmail.com"]["VerificationStatus"], "");
174 
175  print "\n\nTesting removing email identities...\n" . str_repeat("*", $sepLength) . "\n";
176  $results = RunPyFile("removeEmails", array("homecutester4@gmail.com", "homecutester5@gmail.com"));
177  AssertEquals("Test " . ($testn ++) . ": no errors", $results["status"], "000");
178  AssertEquals("Test " . ($testn ++) . ": deleted email identity", $results["response"]["homecutester4@gmail.com"]["ResponseMetadata"]["HTTPHeaders"]["content-type"], "text/xml");
179  AssertEquals("Test " . ($testn ++) . ": deleted nonexistent email identity", $results["response"]["homecutester5@gmail.com"]["ResponseMetadata"]["HTTPHeaders"]["content-type"], "text/xml");
180 
181  print "\n\nTesting getting statuses of identities...\n" . str_repeat("*", $sepLength) . "\n";
182  $results = RunPyFile("getVerification", array("homecutester1@gmail.com", "homecutester2@gmail.com", "homecutester3@gmail.com", "homecutester4@gmail.com", "homecutester5@gmail.com"));
183  AssertEquals("Test " . ($testn ++) . ": no errors", $results["status"], "000");
184  $verify = $results["response"]["VerificationAttributes"];
185  AssertEquals("Test " . ($testn ++) . ": verification attributes", is_array($verify), true);
186  AssertEquals("Test " . ($testn ++) . ": homecutester1@gmail.com", $verify["homecutester1@gmail.com"]["VerificationStatus"], "Success");
187  AssertEquals("Test " . ($testn ++) . ": homecutester2@gmail.com", $verify["homecutester2@gmail.com"]["VerificationStatus"], "Failed");
188  AssertEquals("Test " . ($testn ++) . ": homecutester3@gmail.com", $verify["homecutester3@gmail.com"]["VerificationStatus"], "Pending");
189  AssertEquals("Test " . ($testn ++) . ": homecutester4@gmail.com", $verify["homecutester4@gmail.com"]["VerificationStatus"], "");
190  AssertEquals("Test " . ($testn ++) . ": homecutester5@gmail.com", $verify["homecutester5@gmail.com"]["VerificationStatus"], "");
191 
192 }