2 /*********************************************************************************
3 * By installing or using this file, you are confirming on behalf of the entity
4 * subscribed to the SugarCRM Inc. product ("Company") that Company is bound by
5 * the SugarCRM Inc. Master Subscription Agreement (“MSA”), which is viewable at:
6 * http://www.sugarcrm.com/master-subscription-agreement
8 * If Company is not bound by the MSA, then by installing or using this file
9 * you are agreeing unconditionally that Company will be bound by the MSA and
10 * certifying that you have authority to bind Company accordingly.
12 * Copyright (C) 2004-2013 SugarCRM Inc. All rights reserved.
13 ********************************************************************************/
14 require_once 'modules/OAuthTokens/OAuthToken.php';
16 class OAuthTokenTest extends Sugar_PHPUnit_Framework_TestCase
18 public static function setUpBeforeClass()
20 SugarTestHelper::setUp('current_user');
23 public static function tearDownAfterClass()
25 OAuthToken::deleteByUser($GLOBALS['current_user']->id);
26 SugarTestHelper::tearDown();
32 public function testCleanup()
34 // create request token
35 $tok = OAuthToken::generate();
36 $tok->consumer = create_guid();
37 $tok->setState(OAuthToken::REQUEST);
38 $tok->assigned_user_id = $GLOBALS['current_user']->id;
40 // create invalid token
41 $tok = OAuthToken::generate();
42 $tok->consumer = create_guid();
43 $tok->setState(OAuthToken::INVALID);
44 $tok->assigned_user_id = $GLOBALS['current_user']->id;
47 $cnt = $GLOBALS['db']->getOne("SELECT count(*) c FROM {$tok->table_name} WHERE assigned_user_id=".$GLOBALS['db']->quoted($GLOBALS['current_user']->id));
48 $this->assertEquals(2, $cnt, "Wrong number of tokens in the table");
50 // set time way in the past
51 $GLOBALS['db']->query("UPDATE {$tok->table_name} SET token_ts=1 WHERE assigned_user_id=".$GLOBALS['db']->quoted($GLOBALS['current_user']->id));
54 OAuthToken::cleanup();
56 // ensure tokens are gone
57 $cnt = $GLOBALS['db']->getOne("SELECT count(*) c FROM {$tok->table_name} WHERE assigned_user_id=".$GLOBALS['db']->quoted($GLOBALS['current_user']->id));
58 $this->assertEquals(0, $cnt, "Tokens were not deleted");