]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/Users/authentication/SAMLAuthenticate/Bug49959Test.php
Release 6.5.1
[Github/sugarcrm.git] / tests / modules / Users / authentication / SAMLAuthenticate / Bug49959Test.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
5  * 
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.
12  * 
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
16  * details.
17  * 
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
21  * 02110-1301 USA.
22  * 
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.
25  * 
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.
29  * 
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  ********************************************************************************/
36
37
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';
41
42 /**
43  * @ticket 49959
44  */
45 class Bug49959Test extends Sugar_PHPUnit_Framework_TestCase
46 {
47     /**
48      * @var SAMLAuthenticateUserTest
49      */
50     protected static $auth;
51
52     /**
53      * @var User
54      */
55     protected static $user;
56     protected static $user_id,
57         $user_name  = 'Bug49959TestUser',
58         $user_email = 'bug49959.test.user@example.com';
59
60     /**
61      * This method is called before the first test of this test class is run.
62      *
63      * Creates shared test resources
64      */
65     public static function setUpBeforeClass()
66     {
67         self::$auth = new SAMLAuthenticateUserTest;
68         $user = self::$user = new User();
69
70         $user->user_name = self::$user_name;
71         $user->email1    = self::$user_email;
72
73         self::$user_id = $user->save();
74     }
75
76
77     /**
78      * This method is called after the last test of this test class is run.
79      *
80      * Removes shared test resources
81      */
82     public static function tearDownAfterClass()
83     {
84         self::$user->mark_deleted(self::$user_id);
85     }
86
87     /**
88      * Test fetching of a user to be authenticated by different fields
89      */
90     public function testFetchUser()
91     {
92         // test fetching user by ID
93         $user = self::$auth->fetch_user(self::$user_id, 'id');
94         $this->assertEquals(self::$user_id, $user->id);
95
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);
99
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);
103
104         // test fetching user by unsupported field
105         $user = self::$auth->fetch_user(self::$user_email, 'unsupported_field');
106         $this->assertNull($user->id);
107
108         // test fetching non-existing user
109         $user = self::$auth->fetch_user('some_wrong_key');
110         $this->assertNull($user->id);
111     }
112
113     /**
114      * Test that get_nameid() method of SamlResponse is called by default
115      */
116     public function testDefaultNameId()
117     {
118         // create a mock of SAML response
119         $mock = $this->getResponse();
120         $mock->expects($this->once())
121             ->method('get_nameid');
122
123         // create a default SAML settings object
124         require(get_custom_file_if_exists('modules/Users/authentication/SAMLAuthenticate/settings.php'));
125
126         // expect that get_nameid() method of response is used by default
127         self::$auth->get_user_id($mock, $settings);
128     }
129
130     /**
131      * Test that custom XPath is used when specified in settings
132      */
133     public function testCustomNameId()
134     {
135         $node_id = 'Bug49959Test';
136
137         // create response with custom XML
138         $mock2 = $this->getResponse();
139         $mock2->xml = $this->getResponseXml($node_id);
140
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';
144
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);
148     }
149
150     /**
151      * Returns a mock of SamlResponse object
152      *
153      * @return SamlResponse
154      */
155     protected function getResponse()
156     {
157         return $this->getMock('SamlResponse', array(), array(), '', false);
158     }
159
160     /**
161      * Returns custom response XML document
162      *
163      * @param $node_id
164      * @return DOMDocument
165      */
166     protected function getResponseXml($node_id)
167     {
168         $document = new DOMDocument();
169         $document->loadXML('<root>' . $node_id . '</root>');
170         $root = $document->createElement('root');
171         $document->appendChild($root);
172         return $document;
173     }
174 }
175
176 /**
177  * A SAMLAuthenticate class wrapper that makes some of its methods accessible
178  */
179 class SAMLAuthenticateUserTest extends SAMLAuthenticateUser
180 {
181     public function fetch_user($id, $field = null)
182     {
183         return parent::fetch_user($id, $field);
184     }
185
186     public function get_user_id($samlresponse, $settings)
187     {
188         return parent::get_user_id($samlresponse, $settings);
189     }
190 }