| 1 | #! /usr/bin/env php |
|---|
| 2 | <?php |
|---|
| 3 | |
|---|
| 4 | require_once(dirname($argv[0]) . "/utilities.php"); |
|---|
| 5 | require_once(IA_ROOT_DIR.'www/utilities.php'); |
|---|
| 6 | |
|---|
| 7 | test_cleanup(); |
|---|
| 8 | test_prepare(); |
|---|
| 9 | |
|---|
| 10 | log_print("Dude 1 creates a page"); |
|---|
| 11 | $res = curl_test(array( |
|---|
| 12 | 'url' => url_textblock_edit('sandbox/test_page'), |
|---|
| 13 | 'user' => 'test_dude1', |
|---|
| 14 | 'post' => array( |
|---|
| 15 | 'text' => "Test page\nxzx-content1-xzx\n", |
|---|
| 16 | 'title' => "Test xzx-title1-xzx", |
|---|
| 17 | 'last_revision' => "0", |
|---|
| 18 | ))); |
|---|
| 19 | |
|---|
| 20 | log_print("Dude 1 attaches a sample file"); |
|---|
| 21 | // FIXME: easily borked. |
|---|
| 22 | file_put_contents('/tmp/test_file', 'xzx-file-xzx'); |
|---|
| 23 | $res = curl_test(array( |
|---|
| 24 | 'url' => url_attachment_new('sandbox/test_page'), |
|---|
| 25 | 'user' => 'test_dude1', |
|---|
| 26 | 'post' => array( |
|---|
| 27 | 'file_name' => '@/tmp/test_file'), |
|---|
| 28 | )); |
|---|
| 29 | log_assert_equal($res['url'], url_absolute( |
|---|
| 30 | url_textblock('sandbox/test_page'))); |
|---|
| 31 | |
|---|
| 32 | log_print("Anon looks at file"); |
|---|
| 33 | $res = curl_test(array( |
|---|
| 34 | 'url' => url_attachment('sandbox/test_page', 'test_file'), |
|---|
| 35 | 'validate_html' => false, |
|---|
| 36 | )); |
|---|
| 37 | log_assert_equal($res['content'], 'xzx-file-xzx'); |
|---|
| 38 | |
|---|
| 39 | log_print("Anon looks at attachment list"); |
|---|
| 40 | $res = curl_test(array( |
|---|
| 41 | 'url' => url_attachment_list('sandbox/test_page'), |
|---|
| 42 | )); |
|---|
| 43 | log_assert(strstr($res['content'], html_escape( |
|---|
| 44 | url_attachment('sandbox/test_page', 'test_file')))); |
|---|
| 45 | // FIXME: this shouldn't be visible |
|---|
| 46 | log_assert(strstr($res['content'], html_escape( |
|---|
| 47 | url_attachment_delete('sandbox/test_page', 'test_file')))); |
|---|
| 48 | |
|---|
| 49 | log_print("Anon tries to delete attachment, fails"); |
|---|
| 50 | $res = curl_test(array( |
|---|
| 51 | 'url' => url_attachment_delete('sandbox/test_page', 'test_file'), |
|---|
| 52 | 'post' => array(), |
|---|
| 53 | )); |
|---|
| 54 | log_assert_equal($res['url'], url_absolute(url_login())); |
|---|
| 55 | |
|---|
| 56 | log_print("Admin looks in list and sees attachment"); |
|---|
| 57 | $res = curl_test(array( |
|---|
| 58 | 'url' => url_attachment_list('sandbox/test_page'), |
|---|
| 59 | )); |
|---|
| 60 | log_assert(strstr($res['content'], html_escape( |
|---|
| 61 | url_attachment('sandbox/test_page', 'test_file')))); |
|---|
| 62 | |
|---|
| 63 | log_print("Admin deletes attachment, OK"); |
|---|
| 64 | $res = curl_test(array( |
|---|
| 65 | 'url' => url_attachment_delete('sandbox/test_page', 'test_file'), |
|---|
| 66 | 'user' => 'test_admin', |
|---|
| 67 | 'post' => array() |
|---|
| 68 | )); |
|---|
| 69 | log_assert_equal($res['url'], url_absolute(url_textblock('sandbox/test_page'))); |
|---|
| 70 | |
|---|
| 71 | log_print("Admin looks in list and attachment is gone"); |
|---|
| 72 | $res = curl_test(array( |
|---|
| 73 | 'url' => url_attachment_list('sandbox/test_page', 'test_file'), |
|---|
| 74 | )); |
|---|
| 75 | log_assert(!strstr($res['content'], html_escape( |
|---|
| 76 | url_attachment('sandbox/test_page', 'test_file')))); |
|---|
| 77 | |
|---|
| 78 | log_print("Dude 1 attaches 5 files"); |
|---|
| 79 | for ($i = 1; $i <= 5; ++$i) { |
|---|
| 80 | // FIXME: easily borked. |
|---|
| 81 | file_put_contents("/tmp/test_file_$i", "xzx-spam-file-$i-xzx"); |
|---|
| 82 | $res = curl_test(array( |
|---|
| 83 | 'url' => url_attachment_new('sandbox/test_page'), |
|---|
| 84 | 'user' => 'test_dude1', |
|---|
| 85 | 'post' => array( |
|---|
| 86 | 'file_name' => "@/tmp/test_file_$i"), |
|---|
| 87 | )); |
|---|
| 88 | log_assert_equal($res['url'], url_absolute( |
|---|
| 89 | url_textblock('sandbox/test_page'))); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | log_print("Dude 1 tries to overwrite file 3"); |
|---|
| 93 | file_put_contents('/tmp/test_file_3', 'xzx-spam-file-3-replaced-xzx'); |
|---|
| 94 | $res = curl_test(array( |
|---|
| 95 | 'url' => url_attachment_new('sandbox/test_page'), |
|---|
| 96 | 'user' => 'test_dude1', |
|---|
| 97 | 'post' => array( |
|---|
| 98 | 'file_name' => '@/tmp/test_file_3'), |
|---|
| 99 | )); |
|---|
| 100 | log_assert_equal($res['url'], url_absolute(url_home())); |
|---|
| 101 | |
|---|
| 102 | log_print("Overwriting failed"); |
|---|
| 103 | $res = curl_test(array( |
|---|
| 104 | 'url' => url_attachment('sandbox/test_page', 'test_file_3'), |
|---|
| 105 | 'validate_html' => false, |
|---|
| 106 | )); |
|---|
| 107 | log_assert_equal($res['content'], 'xzx-spam-file-3-xzx'); |
|---|
| 108 | log_assert($res['content'] != 'xzx-spam-file-3-replaced-xzx'); |
|---|
| 109 | |
|---|
| 110 | log_print("Dude 2 tries to overwrites file 1"); |
|---|
| 111 | file_put_contents('/tmp/test_file_1', 'xzx-spam-file-1-replaced-xzx'); |
|---|
| 112 | $res = curl_test(array( |
|---|
| 113 | 'url' => url_attachment_new('sandbox/test_page'), |
|---|
| 114 | 'user' => 'test_dude2', |
|---|
| 115 | 'post' => array( |
|---|
| 116 | 'file_name' => '@/tmp/test_file_1'), |
|---|
| 117 | )); |
|---|
| 118 | log_assert_equal($res['url'], url_absolute(url_home())); |
|---|
| 119 | |
|---|
| 120 | log_print("Anon sees original file"); |
|---|
| 121 | $res = curl_test(array( |
|---|
| 122 | 'url' => url_attachment('sandbox/test_page', 'test_file_1'), |
|---|
| 123 | 'validate_html' => false, |
|---|
| 124 | )); |
|---|
| 125 | log_assert_equal($res['content'], 'xzx-spam-file-1-xzx'); |
|---|
| 126 | |
|---|
| 127 | log_print("Admin looks at file list and is horrified at the spamming from dude1"); |
|---|
| 128 | $res = curl_test(array( |
|---|
| 129 | 'url' => url_attachment_list('sandbox/test_page'), |
|---|
| 130 | 'user' => 'test_admin', |
|---|
| 131 | )); |
|---|
| 132 | for ($i = 1; $i <= 5; ++$i) { |
|---|
| 133 | log_assert(strstr($res['content'], html_escape( |
|---|
| 134 | url_attachment('sandbox/test_page', "test_file_$i")))); |
|---|
| 135 | } |
|---|
| 136 | log_assert(strstr(strtolower($res['content']), html_escape( |
|---|
| 137 | url_user_profile('test_dude1')))); |
|---|
| 138 | log_assert(!strstr(strtolower($res['content']), html_escape( |
|---|
| 139 | url_user_profile('test_dude2')))); |
|---|
| 140 | |
|---|
| 141 | log_print("Admin moves the spammed page."); |
|---|
| 142 | $res = curl_test(array( |
|---|
| 143 | 'url' => url_textblock_move('sandbox/test_page'), |
|---|
| 144 | 'user' => 'test_admin', |
|---|
| 145 | 'post' => array( |
|---|
| 146 | 'new_name' => 'sAnDbox/test_page_2', |
|---|
| 147 | ))); |
|---|
| 148 | log_assert_equal($res['url'], url_absolute(url_textblock('sandbox/test_page_2'))); |
|---|
| 149 | |
|---|
| 150 | log_print("Anon sees the attachments stayed and he complains"); |
|---|
| 151 | $res = curl_test(array( |
|---|
| 152 | 'url' => url_attachment('sandbox/test_page_2', 'test_file_2'), |
|---|
| 153 | 'validate_html' => false, |
|---|
| 154 | )); |
|---|
| 155 | log_assert_equal($res['content'], 'xzx-spam-file-2-xzx'); |
|---|
| 156 | |
|---|
| 157 | log_print("Admin deletes page"); |
|---|
| 158 | $res = curl_test(array( |
|---|
| 159 | 'url' => url_textblock_delete('sandbox/test_page_2'), |
|---|
| 160 | 'user' => 'test_admin', |
|---|
| 161 | 'post' => array() |
|---|
| 162 | )); |
|---|
| 163 | log_assert_equal($res['url'], url_absolute(url_home())); |
|---|
| 164 | |
|---|
| 165 | log_print("Anon sees spam is finally gone"); |
|---|
| 166 | $res = curl_test(array( |
|---|
| 167 | 'url' => url_attachment('sandbox/test_page_2', 'test_file_2'), |
|---|
| 168 | 'validate_html' => false, |
|---|
| 169 | )); |
|---|
| 170 | log_assert($res['content'] != 'xzx-spam-file-2-xzx'); |
|---|
| 171 | log_assert_equal($res['http_code'], 404); |
|---|
| 172 | |
|---|
| 173 | log_print("Check files are gone in the db and on disk"); |
|---|
| 174 | log_assert_equal(array(), attachment_get_all('sandbox/test_page_2')); |
|---|
| 175 | log_assert_equal(0, attachment_get_count('sandbox/test_page_2')); |
|---|
| 176 | log_assert_equal(array(), glob(IA_ROOT_DIR . "attach/sandbox_test_page*")); |
|---|
| 177 | |
|---|
| 178 | log_print("All tests passed. Warning: incomplete."); |
|---|
| 179 | test_cleanup(); |
|---|