SubmissionsTest.php 3.08 KB
Newer Older
1
<?php
2
/* GNUkebox -- a free software server for recording your listening habits
mattl's avatar
mattl committed
3

4
   Copyright (C) 2009, 2015 Free Software Foundation, Inc
mattl's avatar
mattl committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

21
require_once(__DIR__ . '/../vendor/autoload.php');
22

23
class SubmissionsTest extends PHPUnit_Framework_TestCase {
24 25
	// Check we can login with standard authentication
	public function testStandardAuth() {
26 27
		$result = $this->standardLogin('testuser', 'password');
		$this->assertEquals('OK', trim($result[0]));
elleo's avatar
elleo committed
28
		sleep(1);
29 30 31 32 33
	}


	// Make sure we get BADAUTH if we give an incorrect password
	public function testFailedStandardAuth() {
34 35
		$result = $this->standardLogin('testuser', 'icanhazsecurity?');
		$this->assertEquals('BADAUTH', trim($result[0]));
elleo's avatar
elleo committed
36
		sleep(1);
37 38
	}

elleo's avatar
elleo committed
39 40

	public function testScrobble() {
41 42
		$result = $this->standardLogin('testuser', 'password');
		$this->assertEquals('OK', trim($result[0]));
elleo's avatar
elleo committed
43 44 45

		$session_id = trim($result[1]);
		$scrobble_server = trim($result[3]);
46 47
		$result = $this->scrobble($scrobble_server, $session_id,
								  'Richard Stallman',
48 49
			'The Free Software Song', 'b25b9595-54ed-7605-8ac2-20b7b2e0a026');
		$this->assertEquals('OK', trim($result));
elleo's avatar
elleo committed
50 51 52 53 54
		sleep(1);
	}


	public function testNowPlaying() {
55 56
		$result = $this->standardLogin('testuser', 'password');
		$this->assertEquals('OK', trim($result[0]));
elleo's avatar
elleo committed
57 58 59

		$session_id = trim($result[1]);
		$nowplaying_server = trim($result[2]);
60 61
		$result = $this->nowPlaying($nowplaying_server, $session_id,
									'The Libre.fm Players', 'Let Freedom Ring',
62 63
			'b25b9595-54ed-7605-8ac2-20b7b2e0a026');
		$this->assertEquals('OK', trim($result));
elleo's avatar
elleo committed
64
		sleep(1);
elleo's avatar
elleo committed
65 66 67
	}


68
	private function standardLogin($username, $password) {
69
		require('../config.php');
70 71 72

		$timestamp = time();
		$token = md5(md5($password) . $timestamp);
73
		$response = file($submissions_server . "/?hs=true&p=1.2&u=$username&t=$timestamp&a=$token&c=unittest");
74 75 76
		return $response;
	}

77

elleo's avatar
elleo committed
78
	private function scrobble($server, $session_id, $artist, $track) {
79 80 81 82 83 84 85 86 87
		$r = new HttpRequest($server);
		$r->setMethod(HTTP_METH_POST);
		$r->addPostFields(['s'=>$session_id,
						   'a[0]'=>$artist,
						   't[0]'=>$track,
						   'i[0]'=>time(),
						   'm[0]'=>$mbid,
						   'o[0]'=>'U']);
		$r->send();
elleo's avatar
elleo committed
88 89 90
		return $r->getResponseBody();
	}

elleo's avatar
elleo committed
91

92
	private function nowPlaying($server, $session_id, $artist, $track, $mbid) {
93 94 95 96 97 98
		$r = new HttpRequest($server);
		$r->setMethod(HTTP_METH_POST);
		$r->addPostFields(['s'=>$session_id,
						   'a'=>$artist,
						   't'=>$track]);
		$r->send();
elleo's avatar
elleo committed
99 100
		return $r->getResponseBody();
	}
101
}