/application/controllers/test/authTest.php

https://github.com/Sa-ryong/Stadioom-php · PHP · 176 lines · 129 code · 24 blank · 23 comment · 0 complexity · 6307efd530eee87a715fe12f858a166a MD5 · raw file

  1. <?php
  2. require(APPPATH . '/libraries/Stadioom_REST_Controller.php');
  3. /**
  4. * Contains stuffs for just testing.
  5. */
  6. class AuthTest extends Test_REST_Controller {
  7. public function __construct() {
  8. parent::__construct();
  9. force_ssl();
  10. // if (function_exists('force_ssl'))
  11. // remove_ssl();
  12. }
  13. public function all_get() {
  14. try {
  15. $grantCode = $this->generateGrantCode('valid@gmail.com', 'password');
  16. $result = $this->runTest("sign up with an yet-implemented authorization code.", "api/auth/signUp", array('grantType' => 'non-authorization_code',
  17. 'code' => $grantCode,
  18. 'name' => 'Wegra',
  19. 'gender' => 'male',
  20. 'dob' => 1232123222));
  21. Assert::assertError($result, 501);
  22. echo '=> PASSED.<br><br>';
  23. $grantCode = $this->generateGrantCode('wegra.lee', 'password');
  24. $result = $this->runTest("sign up with an 'invalid' email", "api/auth/signUp", array('grantType' => 'authorization_code',
  25. 'code' => $grantCode,
  26. 'name' => 'Wegra',
  27. 'gender' => 'male',
  28. 'dob' => 1232123222));
  29. Assert::assertError($result, 400);
  30. echo '=> PASSED.<br><br>';
  31. $grantCode = $this->generateGrantCode('wegra.lee', 'password');
  32. $result = $this->runTest("sign up with a 'too short(length = 3)' name", "api/auth/signUp", array('grantType' => 'authorization_code',
  33. 'code' => $grantCode,
  34. 'name' => '123',
  35. 'gender' => 'female',
  36. 'dob' => 1232123222));
  37. Assert::assertError($result, 400);
  38. echo '=> PASSED.<br><br>';
  39. $grantCode = $this->generateGrantCode('wegra.lee', 'password');
  40. $result = $this->runTest("sign up with a 'too long(length = 101)' name", "api/auth/signUp", array('grantType' => 'authorization_code',
  41. 'code' => $grantCode,
  42. 'name' => 'name_----1---------2---------3---------4---------5---------6---------7---------8---------9---------0-',
  43. 'gender' => 'male',
  44. 'dob' => 1232123222));
  45. Assert::assertError($result, 400);
  46. echo '=> PASSED.<br><br>';
  47. // get valid test accounts.
  48. $testUser1 = $this->createTestUser();
  49. $testUser2 = $this->createTestUser();
  50. $testUser3 = $this->createTestUser();
  51. $grantCode = $this->generateGrantCode($testUser1['email'], $testUser1['password']);
  52. $result = $this->runTest("sign up with a valid email_1", "api/auth/signUp", array('grantType' => 'authorization_code',
  53. 'code' => $grantCode,
  54. 'name' => $testUser1['name'],
  55. 'gender' => 'male',
  56. 'dob' => 1232123222));
  57. Assert::assertEmpty($result);
  58. echo '=> PASSED.<br><br>';
  59. $grantCode = $this->generateGrantCode($testUser2['email'], $testUser2['password']);
  60. $result = $this->runTest("sign up with a valid email_2", "api/auth/signUp", array('grantType' => 'authorization_code',
  61. 'code' => $grantCode,
  62. 'name' => $testUser2['name'],
  63. 'gender' => 'male',
  64. 'dob' => 1232123222));
  65. Assert::assertEmpty($result);
  66. echo '=> PASSED.<br><br>';
  67. $grantCode = $this->generateGrantCode($testUser2['email'], $testUser2['password']);
  68. $result = $this->runTest("sign up with a valid email_2 (already registered)", "api/auth/signUp", array('grantType' => 'authorization_code',
  69. 'code' => $grantCode,
  70. 'name' => $testUser2['name'],
  71. 'gender' => 'male',
  72. 'dob' => 1232123222));
  73. Assert::assertError($result, 406);
  74. echo '=> PASSED.<br><br>';
  75. $grantCode = $this->generateGrantCode($testUser2['email'], $testUser2['password']);
  76. $result = $this->runTest("sign in with a valid email_2", "api/auth/signIn", array('grantType' => 'authorization_code',
  77. 'code' => $grantCode));
  78. Assert::assertArray_NotNull($result, 'accessToken');
  79. echo '=> PASSED.<br><br>';
  80. $grantCode = $this->generateGrantCode($testUser2['email'], $testUser2['password'] . '_');
  81. $result = $this->runTest("sign in with a valid email_2 w/ 'invalid' password.", "api/auth/signIn", array('grantType' => 'authorization_code',
  82. 'code' => $grantCode));
  83. Assert::assertError($result, 403);
  84. echo '=> PASSED.<br><br>';
  85. $grantCode = $this->generateGrantCode('unregistered@seedshock.com', 'password');
  86. $result = $this->runTest("sign in with an 'unregistered' email.", "api/auth/signIn", array('grantType' => 'authorization_code',
  87. 'code' => $grantCode));
  88. Assert::assertError($result, 404);
  89. echo '=> PASSED.<br><br>';
  90. $grantCode = $this->generateGrantCode($testUser1['email'], $testUser1['password']);
  91. $result = $this->runTest("sign in with a valid email_1", "api/auth/signIn", array('grantType' => 'authorization_code',
  92. 'code' => $grantCode));
  93. Assert::assertArray_NotNull($result, 'accessToken');
  94. echo '=> PASSED.<br><br>';
  95. // now.. keep the access token of email_1 for later tests.
  96. $json = json_decode($result);
  97. $accessToken = $json->accessToken;
  98. $result = $this->runTest("invite an 'invalid' email", "api/auth/invite", array('accessToken' => $accessToken,
  99. 'inviteeEmails' => array('invalid.email.com')));
  100. Assert::assertData($result, 'invalid.email.com', 'invalid email.');
  101. echo '=> PASSED.<br><br>';
  102. $result = $this->runTest("invite a valid email_2 (already registered)", "api/auth/invite", array('accessToken' => $accessToken,
  103. 'inviteeEmails' => array($testUser2['email']), 'invitationMessage' => 'This is a custom message for test.'));
  104. Assert::assertData($result, $testUser2['email'], 'already registered.');
  105. echo '=> PASSED.<br><br>';
  106. $result = $this->runTest("invite a valid email_3", "api/auth/invite", array('accessToken' => $accessToken,
  107. 'inviteeEmails' => array($testUser3['email']), 'invitationMessage' => 'This is a custom message for test.'));
  108. Assert::assertData($result, $testUser3['email'], 'invitation sent.');
  109. echo '=> PASSED.<br><br>';
  110. $result = $this->runTest("invite nobody", "api/auth/invite", array('accessToken' => $accessToken,
  111. 'invitationMessage' => 'This is a custom message for test.'));
  112. Assert::assertError($result, 400);
  113. echo '=> PASSED.<br><br>';
  114. $result = $this->runTest("invite all of them", "api/auth/invite", array('accessToken' => $accessToken,
  115. 'inviteeEmails' => array($testUser2['email'], 'invalid.email.com', 'xegra.lee@gmail.com'), 'invitationMessage' => 'This is a custom message for test.'));
  116. Assert::assertData($result, 'invalid.email.com', 'invalid email.');
  117. Assert::assertData($result, $testUser2['email'], 'already registered.');
  118. Assert::assertData($result, 'xegra.lee@gmail.com', 'invitation sent.');
  119. echo '=> PASSED.<br><br>';
  120. echo '| =============================================================================<br>';
  121. echo "| All Tests Passed.<br>";
  122. echo '| =============================================================================<br>';
  123. } catch (Exception $e) {
  124. echo '=> FAILED.<br><br>';
  125. echo "<pre>";
  126. echo 'Last Request: ' . $this->last_request . '<br>';
  127. echo 'Code: ' . $e->getCode() . '<br>';
  128. echo 'File: ' . $e->getFile() . '<br>';
  129. echo 'Line: ' . $e->getLine() . '<br>';
  130. echo 'Message: ' . $e->getMessage() . '<br>';
  131. echo 'Previous: ' . $e->getPrevious() . '<br>';
  132. echo 'Trace: <br>' . $e->getTraceAsString() . '<br>';
  133. echo "</pre>";
  134. }
  135. }
  136. //
  137. // public function base64_encode_get() {
  138. // $msg = $this->get('email') . ":" . $this->get('password');
  139. //
  140. // $this->response(base64_encode($msg), 200);
  141. // }
  142. //
  143. // public function url_base64_encode_get() {
  144. // $msg = $this->get('email') . ":" . $this->get('password');
  145. //
  146. // $this->response(urlencode(base64_encode($msg)), 200);
  147. // }
  148. //
  149. // public function sendEmail_get() {
  150. // $this->UserDao->sendVerificationEmail("wegra.lee@gmail.com", "codecodecode");
  151. // }
  152. }