summaryrefslogtreecommitdiffstats
path: root/application/tests/test_api_v1.php
blob: 9f415abbdba1b1beb450251aecf781d0aeb38103 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/*
 * Copyright 2015 Florian "Bluewind" Pritz <bluewind@server-speed.net>
 *
 * Licensed under AGPLv3
 * (see COPYING for full license text)
 *
 */

namespace tests;

class test_api_v1 extends Test {

	private $apikeys = array();

	public function __construct()
	{
		parent::__construct();

		$CI =& get_instance();
		$CI->load->model("muser");
		$CI->load->model("mfile");

		foreach (array(1,2,3,4,5) as $i) {
			$CI->db->insert("users", array(
				'username' => "testuser-api_v1-$i",
				'password' => $CI->muser->hash_password("testpass$i"),
				'email'    => "testuser$i@localhost.invalid",
				'referrer' => NULL
			));
			$this->apikeys[$i] = \service\user::create_apikey($CI->db->insert_id(), "", "apikey");
		}

	}

	public function test_create_apikey_createNewKey()
	{
		$ret = $this->CallAPI("POST", "$this->server/api/1.0.0/user/create_apikey", array(
			"username" => "testuser-api_v1-1",
			"password" => "testpass1",
			"access_level" => "apikey",
			"comment" => "main api key",
		));
		$this->expectSuccess("create-apikey", $ret);

		$this->t->isnt($ret["data"]["new_key"], "", "apikey not empty");
	}

	public function test_history_empty()
	{
		$ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/history", array(
			"apikey" => $this->apikeys[1],
		));
		$this->expectSuccess("get history", $ret);

		$this->t->ok(empty($ret["data"]["items"]), "items key exists and empty");
		$this->t->ok(empty($ret["data"]["multipaste_items"]), "multipaste_items key exists and empty");
		$this->t->is($ret["data"]["total_size"], 0, "total_size = 0 since no uploads");
	}

	public function test_get_config()
	{
		$ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/get_config", array(
		));
		$this->expectSuccess("get_config", $ret);

		$this->t->like($ret["data"]["upload_max_size"], '/[0-9]+/', "upload_max_size is int");
		$this->t->like($ret["data"]["max_files_per_request"], '/[0-9]+/', "max_files_per_request is int");
	}

	public function test_upload_uploadFile()
	{
		$ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/upload", array(
			"apikey" => $this->apikeys[2],
			"file[1]" => curl_file_create("data/tests/small-file"),
		));
		$this->expectSuccess("upload file", $ret);

		$this->t->ok(!empty($ret["data"]["ids"]), "got IDs");
		$this->t->ok(!empty($ret["data"]["urls"]), "got URLs");
	}

	public function test_history_notEmptyAfterUpload()
	{
		$ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/upload", array(
			"apikey" => $this->apikeys[3],
			"file[1]" => curl_file_create("data/tests/small-file"),
		));
		$this->expectSuccess("upload file", $ret);

		$ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/history", array(
			"apikey" => $this->apikeys[3],
		));
		$this->expectSuccess("history not empty after upload", $ret);

		$this->t->ok(!empty($ret["data"]["items"]), "history not empty after upload (items)");
		$this->t->ok(empty($ret["data"]["multipaste_items"]), "didn't upload multipaste");
		$this->t->is($ret["data"]["total_size"], filesize("data/tests/small-file"), "total_size == uploaded file");
	}

	public function test_history_notSharedBetweenUsers()
	{
		$ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/upload", array(
			"apikey" => $this->apikeys[4],
			"file[1]" => curl_file_create("data/tests/small-file"),
		));
		$this->expectSuccess("upload file", $ret);

		$ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/history", array(
			"apikey" => $this->apikeys[5],
		));
		$this->expectSuccess("get history", $ret);

		$this->t->ok(empty($ret["data"]["items"]), "items key exists and empty");
		$this->t->ok(empty($ret["data"]["multipaste_items"]), "multipaste_items key exists and empty");
		$this->t->is($ret["data"]["total_size"], 0, "total_size = 0 since no uploads");
	}

	public function test_delete_canDeleteUploaded()
	{
		$ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/upload", array(
			"apikey" => $this->apikeys[2],
			"file[1]" => curl_file_create("data/tests/small-file"),
		));
		$this->expectSuccess("upload file", $ret);

		$id = $ret["data"]["ids"][0];

		$ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/delete", array(
			"apikey" => $this->apikeys[2],
			"ids[1]" => $id,
		));
		$this->expectSuccess("delete uploaded file", $ret);

		$this->t->ok(empty($ret["data"]["errors"]), "no errors");
		$this->t->is_deeply(array($id => array("id" => $id)), $ret["data"]["deleted"], "deleted wanted ID");
		$this->t->is($ret["data"]["total_count"], 1, "total_count correct");
		$this->t->is($ret["data"]["deleted_count"], 1, "deleted_count correct");
	}

	public function test_delete_errorIfNotOwner()
	{
		$ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/upload", array(
			"apikey" => $this->apikeys[2],
			"file[1]" => curl_file_create("data/tests/small-file"),
		));
		$this->expectSuccess("upload file", $ret);

		$id = $ret["data"]["ids"][0];

		$ret = $this->CallAPI("POST", "$this->server/api/1.0.0/file/delete", array(
			"apikey" => $this->apikeys[1],
			"ids[1]" => $id,
		));
		$this->expectSuccess("delete file of someone else", $ret);

		$this->t->ok(empty($ret["data"]["deleted"]), "not deleted");
		$this->t->is_deeply(array($id => array("id" => $id, "reason" => "wrong owner")), $ret["data"]["errors"], "error wanted ID");
		$this->t->is($ret["data"]["total_count"], 1, "total_count correct");
		$this->t->is($ret["data"]["deleted_count"], 0, "deleted_count correct");
	}
}