2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Affero General Public License version 3 as published by the
8 * Free Software Foundation with the addition of the following permission added
9 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
18 * You should have received a copy of the GNU Affero General Public License along with
19 * this program; if not, see http://www.gnu.org/licenses or write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26 * The interactive user interfaces in modified source and object code versions
27 * of this program must display Appropriate Legal Notices, as required under
28 * Section 5 of the GNU Affero General Public License version 3.
30 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31 * these Appropriate Legal Notices must retain the display of the "Powered by
32 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33 * technical reasons, the Appropriate Legal Notices must display the words
34 * "Powered by SugarCRM".
35 ********************************************************************************/
38 require_once 'modules/Users/authentication/SAMLAuthenticate/SAMLAuthenticateUser.php';
39 require_once 'modules/Users/authentication/SAMLAuthenticate/lib/onelogin/saml/settings.php';
40 require_once 'modules/Users/authentication/SAMLAuthenticate/lib/onelogin/saml/response.php';
45 class Bug49959Test extends Sugar_PHPUnit_Framework_TestCase
48 * @var SAMLAuthenticateUserTest
50 protected static $auth;
55 protected static $user;
56 protected static $user_id,
57 $user_name = 'Bug49959TestUser',
58 $user_email = 'bug49959.test.user@example.com';
61 * This method is called before the first test of this test class is run.
63 * Creates shared test resources
65 public static function setUpBeforeClass()
67 self::$auth = new SAMLAuthenticateUserTest;
68 $user = self::$user = new User();
70 $user->user_name = self::$user_name;
71 $user->email1 = self::$user_email;
73 self::$user_id = $user->save();
78 * This method is called after the last test of this test class is run.
80 * Removes shared test resources
82 public static function tearDownAfterClass()
84 self::$user->mark_deleted(self::$user_id);
88 * Test fetching of a user to be authenticated by different fields
90 public function testFetchUser()
92 // test fetching user by ID
93 $user = self::$auth->fetch_user(self::$user_id, 'id');
94 $this->assertEquals(self::$user_id, $user->id);
96 // test fetching user by username
97 $user = self::$auth->fetch_user(self::$user_name, 'user_name');
98 $this->assertEquals(self::$user_id, $user->id);
100 // test fetching user by email (default case)
101 $user = self::$auth->fetch_user(self::$user_email);
102 $this->assertEquals(self::$user_id, $user->id);
104 // test fetching user by unsupported field
105 $user = self::$auth->fetch_user(self::$user_email, 'unsupported_field');
106 $this->assertNull($user->id);
108 // test fetching non-existing user
109 $user = self::$auth->fetch_user('some_wrong_key');
110 $this->assertNull($user->id);
114 * Test that get_nameid() method of SamlResponse is called by default
116 public function testDefaultNameId()
118 // create a mock of SAML response
119 $mock = $this->getResponse();
120 $mock->expects($this->once())
121 ->method('get_nameid');
123 // create a default SAML settings object
124 require(get_custom_file_if_exists('modules/Users/authentication/SAMLAuthenticate/settings.php'));
126 // expect that get_nameid() method of response is used by default
127 self::$auth->get_user_id($mock, $settings);
131 * Test that custom XPath is used when specified in settings
133 public function testCustomNameId()
135 $node_id = 'Bug49959Test';
137 // create response with custom XML
138 $mock2 = $this->getResponse();
139 $mock2->xml = $this->getResponseXml($node_id);
141 // create SAML settings object with custom name id definition
142 require(get_custom_file_if_exists('modules/Users/authentication/SAMLAuthenticate/settings.php'));
143 $settings->saml_settings['check']['user_name'] = '//root';
145 // expect that user ID is fetched from the document according to settings
146 $result = self::$auth->get_user_id($mock2, $settings);
147 $this->assertEquals($node_id, $result);
151 * Returns a mock of SamlResponse object
153 * @return SamlResponse
155 protected function getResponse()
157 return $this->getMock('SamlResponse', array(), array(), '', false);
161 * Returns custom response XML document
164 * @return DOMDocument
166 protected function getResponseXml($node_id)
168 $document = new DOMDocument();
169 $document->loadXML('<root>' . $node_id . '</root>');
170 $root = $document->createElement('root');
171 $document->appendChild($root);
177 * A SAMLAuthenticate class wrapper that makes some of its methods accessible
179 class SAMLAuthenticateUserTest extends SAMLAuthenticateUser
181 public function fetch_user($id, $field = null)
183 return parent::fetch_user($id, $field);
186 public function get_user_id($samlresponse, $settings)
188 return parent::get_user_id($samlresponse, $settings);