|
| 1 | +<?php namespace Tests\Routes; |
| 2 | + |
| 3 | +class OAuthTest extends \Tests\LrsTestCase { |
| 4 | + use RouteTestTrait; |
| 5 | + |
| 6 | + public function setup() { |
| 7 | + parent::setup(); |
| 8 | + \DB::getMongoDB()->oauth_clients->insert([ |
| 9 | + 'client_id' => $this->lrs->api['basic_key'], |
| 10 | + 'client_secret' => $this->lrs->api['basic_secret'], |
| 11 | + 'redirect_uri' => 'http://www.example.com/' |
| 12 | + ]); |
| 13 | + } |
| 14 | + |
| 15 | + protected function requestToken($client_id, $client_secret, $grant_type = 'client_credentials') { |
| 16 | + $method = 'POST'; |
| 17 | + $uri = '/oauth/access_token'; |
| 18 | + $params = [ |
| 19 | + 'client_id' => $client_id, |
| 20 | + 'client_secret' => $client_secret, |
| 21 | + 'grant_type' => $grant_type |
| 22 | + ]; |
| 23 | + $server = []; |
| 24 | + $content = null; |
| 25 | + return $this->request($method, $uri, $params, $server, $content); |
| 26 | + } |
| 27 | + |
| 28 | + protected function requestApi($token) { |
| 29 | + $method = 'GET'; |
| 30 | + $uri = '/data/xAPI/statements'; |
| 31 | + $params = []; |
| 32 | + $server = [ |
| 33 | + 'HTTP_Authorization' => 'Bearer '.$token, |
| 34 | + 'HTTP_X-Experience-API-Version' => '1.0.1' |
| 35 | + ]; |
| 36 | + $content = null; |
| 37 | + return $this->request($method, $uri, $params, $server, $content); |
| 38 | + } |
| 39 | + |
| 40 | + public function testAccessTokenViaClient() { |
| 41 | + $response = $this->requestToken($this->lrs->api['basic_key'], $this->lrs->api['basic_secret']); |
| 42 | + $data = json_decode($response->getContent()); |
| 43 | + |
| 44 | + $this->assertEquals(true, is_object($data)); |
| 45 | + |
| 46 | + // Checks properties of result object. |
| 47 | + $this->assertEquals(true, isset($data->access_token)); |
| 48 | + $this->assertEquals(true, isset($data->expires_in)); |
| 49 | + $this->assertEquals(true, isset($data->token_type)); |
| 50 | + |
| 51 | + // Checks property types of result object. |
| 52 | + $this->assertEquals(true, is_string($data->access_token)); |
| 53 | + $this->assertEquals(true, is_int($data->expires_in)); |
| 54 | + $this->assertEquals(true, is_string($data->token_type)); |
| 55 | + |
| 56 | + // Checks property values of result object. |
| 57 | + $this->assertEquals(3600, $data->expires_in); |
| 58 | + $this->assertEquals('Bearer', $data->token_type); |
| 59 | + } |
| 60 | + |
| 61 | + public function testAccessViaToken() { |
| 62 | + $token_response = $this->requestToken($this->lrs->api['basic_key'], $this->lrs->api['basic_secret']); |
| 63 | + $token = json_decode($token_response->getContent())->access_token; |
| 64 | + $response = $this->requestApi($token); |
| 65 | + $data = $response->getContent(); |
| 66 | + |
| 67 | + // Checks result object. |
| 68 | + $this->assertEquals('{"more":"","statements":[]}', $data); |
| 69 | + } |
| 70 | + |
| 71 | + public function tearDown() { |
| 72 | + parent::tearDown(); |
| 73 | + } |
| 74 | +} |
0 commit comments