rpicms

A CMS for the Raspberry Pi
git clone git://archive.git.mtrnord.blog/RpicmsTeam/rpicms.git
Log | Files | Refs | README | LICENSE

ClientConfigTest.php (6696B)


      1 <?php
      2 
      3 /**
      4  *  This program is free software: you can redistribute it and/or modify
      5  *  it under the terms of the GNU Lesser General Public License as published by
      6  *  the Free Software Foundation, either version 3 of the License, or
      7  *  (at your option) any later version.
      8  *
      9  *  This program is distributed in the hope that it will be useful,
     10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12  *  GNU Lesser General Public License for more details.
     13  *
     14  *  You should have received a copy of the GNU Lesser General Public License
     15  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16  */
     17 
     18 namespace fkooman\OAuth\Client;
     19 
     20 use fkooman\OAuth\Client\Exception\ClientConfigException;
     21 
     22 class ClientConfigTest extends \PHPUnit_Framework_TestCase
     23 {
     24     public function testSimple()
     25     {
     26         $data = array("client_id" => "foo", "client_secret" => "bar", "authorize_endpoint" => "http://www.example.org/authorize", "token_endpoint" => "http://www.example.org/token");
     27         $c = new ClientConfig($data);
     28         $this->assertEquals("foo", $c->getClientId());
     29         $this->assertEquals("bar", $c->getClientSecret());
     30         $this->assertEquals("http://www.example.org/authorize", $c->getAuthorizeEndpoint());
     31         $this->assertEquals("http://www.example.org/token", $c->getTokenEndpoint());
     32         $this->assertNull($c->getRedirectUri());
     33         $this->assertFalse($c->getCredentialsInRequestBody());
     34     }
     35 
     36     public function testLessSimple()
     37     {
     38         $data = array("client_id" => "foo", "client_secret" => "bar", "authorize_endpoint" => "http://www.example.org/authorize", "token_endpoint" => "http://www.example.org/token", "redirect_uri" => "http://www.example.org/callback", "credentials_in_request_body" => true);
     39         $c = new ClientConfig($data);
     40         $this->assertEquals("foo", $c->getClientId());
     41         $this->assertEquals("bar", $c->getClientSecret());
     42         $this->assertEquals("http://www.example.org/authorize", $c->getAuthorizeEndpoint());
     43         $this->assertEquals("http://www.example.org/token", $c->getTokenEndpoint());
     44         $this->assertEquals("http://www.example.org/callback", $c->getRedirectUri());
     45         $this->assertTrue($c->getCredentialsInRequestBody());
     46     }
     47 
     48     /**
     49      * @dataProvider validClients
     50      */
     51     public function testValidClients(array $data)
     52     {
     53         new ClientConfig($data);
     54     }
     55 
     56     /**
     57      * @dataProvider invalidClients
     58      */
     59     public function testInvalidClients(array $data, $message)
     60     {
     61         try {
     62             new ClientConfig($data);
     63             $this->assertTrue(false);
     64         } catch (ClientConfigException $e) {
     65             $this->assertEquals($message, $e->getMessage());
     66         }
     67     }
     68 
     69     public function validClients()
     70     {
     71         return array (
     72             array(
     73               array ("client_id" => "foo", "client_secret" => "bar", "authorize_endpoint" => "http://www.example.org/authorize", "token_endpoint" => "http://www.example.org/token"),
     74             ),
     75             array(
     76               array ("client_id" => "foo", "client_secret" => "bar", "authorize_endpoint" => "http://www.example.org/authorize", "token_endpoint" => "http://www.example.org/token", "credentials_in_request_body" => true, "redirect_uri" => "http://www.example.org/callback"),
     77             ),
     78             array(
     79               array ("foo" => "bar", "xyz" => "abc", "client_id" => "foo", "client_secret" => "bar", "authorize_endpoint" => "http://www.example.org/authorize", "token_endpoint" => "http://www.example.org/token"),
     80             ),
     81             // empty client_secret is allowed
     82             array(
     83               array ("client_id" => "foo", "client_secret" => null, "authorize_endpoint" => "http://www.example.org/authorize", "token_endpoint" => "http://www.example.org/token"),
     84             ),
     85             array(
     86               array ("client_id" => "foo", "client_secret" => null, "authorize_endpoint" => "http://www.example.org/authorize", "token_endpoint" => "http://www.example.org/token", "default_token_token" => "bearer"),
     87             ),
     88         );
     89     }
     90 
     91     public function invalidClients()
     92     {
     93         return array(
     94             array(
     95                 array(),
     96                 "missing field 'client_id'",
     97             ),
     98             array(
     99                 array("client_id" => "", "client_secret" => "", "authorize_endpoint" => "", "token_endpoint" => ""),
    100                 "client_id must be a non-empty string",
    101             ),
    102             array(
    103                 array("client_id" => "foo"),
    104                 "missing field 'authorize_endpoint'",
    105             ),
    106             array(
    107                 array("client_id" => "foo", "client_secret" => "bar"),
    108                 "missing field 'authorize_endpoint'",
    109             ),
    110             array(
    111                 array("client_id" => "foo", "client_secret" => "bar", "authorize_endpoint" => "http://www.example.org/authorize", "token_endpoint" => 5),
    112                 "uri must be a non-empty string",
    113             ),
    114             array(
    115                 array("client_id" => "foo", "client_secret" => "bar", "authorize_endpoint" => "http://www.example.org/authorize"),
    116                 "missing field 'token_endpoint'",
    117             ),
    118             array(
    119                 array("client_id" => "foo", "client_secret" => "bar", "authorize_endpoint" => "not_a_url", "token_endpoint" => "http://www.example.org/token#foo"),
    120                 "uri must be valid URL",
    121             ),
    122             array(
    123                 array("client_id" => "foo", "client_secret" => "bar", "authorize_endpoint" => "http://www.example.org/authorize", "token_endpoint" => "http://www.example.org/token#foo"),
    124                 "uri must not contain a fragment",
    125             ),
    126             array(
    127               array ("client_id" => "foo", "client_secret" => "∑", "authorize_endpoint" => "http://www.example.org/authorize", "token_endpoint" => "http://www.example.org/token"),
    128                 "invalid characters in client_id or client_secret",
    129             ),
    130             array(
    131               array ("client_id" => "foo", "client_secret" => 5, "authorize_endpoint" => "http://www.example.org/authorize", "token_endpoint" => "http://www.example.org/token"),
    132                 "client_secret must be a non-empty string or null",
    133             ),
    134             array(
    135               array ("client_id" => "foo", "client_secret" => "bar", "authorize_endpoint" => "http://www.example.org/authorize", "token_endpoint" => "http://www.example.org/token", "default_token_type" => ''),
    136                 "default_token_type must be a non-empty string or null",
    137             ),
    138 
    139         );
    140     }
    141 }