summaryrefslogtreecommitdiffstats
path: root/application/test/tests/api_v2/common.php
blob: dbef3cc9ca6cfa7582d2dde49b8725076b8099af (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
<?php
/*
 * Copyright 2015 Florian "Bluewind" Pritz <bluewind@server-speed.net>
 *
 * Licensed under AGPLv3
 * (see COPYING for full license text)
 *
 */

namespace test\tests\api_v2;

class common extends \test\Test {

	protected $userCounter = null;

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

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

	protected function uploadFile($apikey, $file)
	{
		$ret = $this->CallAPI("POST", "$this->server_url/api/v2.0.0/file/upload", array(
			"apikey" => $apikey,
			"file[1]" => curl_file_create($file),
		));
		$this->expectSuccess("upload file", $ret);
		return $ret;
	}

	protected function createUser($counter)
	{
		$CI =& get_instance();
		$CI->muser->add_user("apiv2testuser$counter", "testpass$counter",
			"testuser$counter@testsuite.local", NULL);
		return $CI->db->insert_id();
	}

	protected function createApikey($userid, $access_level = "apikey")
	{
		return \service\user::create_apikey($userid, "", $access_level);
	}

	protected function createUserAndApikey($access_level = "apikey")
	{
		assert($this->userCounter !== null);
		$this->userCounter++;
		$userid = $this->createUser($this->userCounter);
		return $this->createApikey($userid, $access_level);
	}

	protected function callEndpoint($verb, $endpoint, $data)
	{
		return $this->CallAPI($verb, "$this->server_url/api/v2.0.0/$endpoint", $data);
	}
}