PageRenderTime 38ms CodeModel.GetById 18ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 1ms

/tests/TestOfTestAuthAPIController.php

https://github.com/derekrogerson/ThinkUp
PHP | 160 lines | 96 code | 22 blank | 42 comment | 0 complexity | 7d73df06a056defd7bcd96d6579419df MD5 | raw file
  1<?php
  2/**
  3 *
  4 * ThinkUp/tests/TestOfTestAuthAPIController.php
  5 *
  6 * Copyright (c) 2009-2010 Gina Trapani, Guillaume Boudreau
  7 *
  8 * LICENSE:
  9 *
 10 * This file is part of ThinkUp (http://thinkupapp.com).
 11 *
 12 * ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
 13 * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
 14 * later version.
 15 *
 16 * ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 17 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 18 * details.
 19 *
 20 * You should have received a copy of the GNU General Public License along with ThinkUp.  If not, see
 21 * <http://www.gnu.org/licenses/>.
 22 */
 23require_once dirname(__FILE__).'/init.tests.php';
 24require_once THINKUP_ROOT_PATH.'webapp/_lib/extlib/simpletest/autorun.php';
 25require_once THINKUP_ROOT_PATH.'webapp/config.inc.php';
 26
 27/**
 28 * Test of TestAuthAPIController
 29 *
 30 * @license http://www.gnu.org/licenses/gpl.html
 31 * @copyright 2009-2010 Gina Trapani, Guillaume Boudreau
 32 * @author Guillaume Boudreau <gboudreau@pommepause.com>
 33 *
 34 */
 35class TestOfTestAuthAPIController extends ThinkUpUnitTestCase {
 36    public function __construct() {
 37        $this->UnitTestCase('TestAuthAPIController class test');
 38    }
 39
 40    public function setUp() {
 41        parent::setUp();
 42        $_SERVER['HTTP_HOST'] = 'http://localhost';
 43    }
 44
 45    public function testConstructor() {
 46        $controller = new TestAuthAPIController(true);
 47        $this->assertTrue(isset($controller));
 48    }
 49
 50    public function testControl() {
 51        $builders = $this->buildData();
 52        $config = Config::getInstance();
 53        $escaped_site_root_path = str_replace('/', '\/', $config->getValue('site_root_path'));
 54
 55        $controller = new TestAuthAPIController(true);
 56
 57        // No username, no API secret provided
 58        // This isn't an API call, so present HTML error output
 59        $results = $controller->go();
 60        $this->assertPattern('/You must <a href="'.$escaped_site_root_path.
 61        'session\/login.php">log in<\/a> to do this./', $results);
 62
 63        // No API secret provided
 64        // This isn't an API call, so present HTML error output
 65        $_GET['un'] = 'me@example.com';
 66        $results = $controller->go();
 67        $this->assertPattern('/You must <a href="'.$escaped_site_root_path.
 68        'session\/login.php">log in<\/a> to do this./', $results);
 69
 70        // Wrong API secret provided
 71        $_GET['as'] = 'fail_me';
 72        $results = $controller->go();
 73        $this->assertPattern("/UnauthorizedUserException: Unauthorized API call/", $results);
 74
 75        // Wrong username provided
 76        $_GET['as'] = Session::getAPISecretFromPassword('XXX');
 77        $_GET['un'] = 'fail_me';
 78        $results = $controller->go();
 79        $this->assertPattern("/UnauthorizedUserException: Unauthorized API call/", $results);
 80
 81        // Working request
 82        $_GET['un'] = 'me@example.com';
 83        $_GET['as'] = Session::getAPISecretFromPassword('XXX');
 84        $results = $controller->go();
 85        $this->assertPattern('/{"result":"success"}/', $results);
 86
 87        $config = Config::getInstance();
 88        $this->assertEqual($_SESSION[$config->getValue('source_root_path')]['user'], 'me@example.com');
 89
 90        // Now that _SESSION['user'] is set, we shouldn't need to provide un/as to use this controller
 91        // Also, the result will be returned as HTML, not JSON
 92        unset($_GET['as']);
 93        $results = $controller->go();
 94        $this->assertPattern('/<html/', $results);
 95
 96        // And just to make sure, if we 'logout', we should be denied access now
 97        Session::logout();
 98        $results = $controller->go();
 99        $this->assertPattern('/You must <a href="'.$escaped_site_root_path.
100        'session\/login.php">log in<\/a> to do this./', $results);
101    }
102
103    public function testGetLoggedInUser() {
104        // Using _POST
105        $builders = $this->buildData();
106        $controller = new TestAuthAPIController(true);
107        $_POST['un'] = 'me@example.com';
108        $_POST['as'] = Session::getAPISecretFromPassword('XXX');
109        $results = $controller->go();
110        $this->assertPattern('/{"result":"success"}/', $results);
111    }
112
113    public function testGetAuthParameters() {
114        $builders = $this->buildData();
115        $this->assertEqual(ThinkUpAuthAPIController::getAuthParameters('me@example.com'),
116        'un=me%40example.com&as=1829cc1b13f920a05fb201e8d2a9e4dc58b669b1');
117    }
118
119    public function testIsAPICall() {
120        $builders = $this->buildData();
121        $controller = new TestAuthAPIController(true);
122
123        // API call (JSON)
124        $_GET['un'] = 'me@example.com';
125        $_GET['as'] = Session::getAPISecretFromPassword('XXX');
126        $results = $controller->go();
127        $this->assertPattern('/{"result":"success"}/', $results);
128        $this->assertFalse(strpos($results, '<html'));
129        unset($_GET['as']);
130        unset($_GET['un']);
131
132        // HTML
133        $this->simulateLogin('me@example.com');
134        $results = $controller->go();
135        $this->assertFalse(strpos($results, '{"result":"success"}'));
136        $this->assertPattern('/<html/', $results);
137    }
138
139    private function buildData() {
140        $owner_builder = FixtureBuilder::build('owners', array(
141            'id' => 1, 
142            'email' => 'me@example.com', 
143            'pwd' => 'XXX', 
144            'is_activated' => 1
145        ));
146
147        $instance_builder = FixtureBuilder::build('instances', array(
148            'id' => 1,
149            'network_username' => 'jack',
150            'network' => 'twitter'
151            ));
152
153            $owner_instance_builder = FixtureBuilder::build('owner_instances', array(
154            'owner_id' => 1, 
155            'instance_id' => 1
156            ));
157
158            return array($owner_builder, $instance_builder, $owner_instance_builder);
159    }
160}