]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/Users/authentication/SAMLAuthenticate/Bug50936Test.php
Release 6.5.1
[Github/sugarcrm.git] / tests / modules / Users / authentication / SAMLAuthenticate / Bug50936Test.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 require_once('modules/Users/authentication/AuthenticationController.php');
38 require_once('tests/modules/Users/AuthenticateTest.php');
39
40 /**
41  * Bug50936Test.php
42  * This tests that we can correctly load a custom settings.php file for SAMLAuthentication when called from
43  * modules/Users/authentication/SAMLAuthenticate/index.php
44  *
45  * This tests mimics the contents of modules/Users/authentication/SAMLAuthenticate/index.php by placing it
46  * in a custom directory minus the header() function call.  We can't include that because it'd just cause other issues
47  */
48 class Bug50936Test extends Sugar_PHPUnit_Framework_OutputTestCase
49 {
50     var $customContents;
51
52         public function setUp()
53     {
54         $GLOBALS['app'] = new SugarApplication();
55         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
56         $this->sugar_config_old = $GLOBALS['sugar_config'];
57         $_REQUEST['user_name'] = 'foo';
58         $_REQUEST['user_password'] = 'bar';
59         $_SESSION['authenticated_user_id'] = true;
60         $_SESSION['hasExpiredPassword'] = false;
61         $_SESSION['isMobile'] = null;
62         $GLOBALS['sugar_config']['authenticationClass'] = 'SAMLAuthenticate';
63         $GLOBALS['sugar_config']['SAML_X509Cert'] = 'Bug50936_X509Cert';
64
65         //Create the custom directory if it does not exist
66         if(!is_dir('custom/modules/Users/authentication/SAMLAuthenticate')) {
67            mkdir_recursive('custom/modules/Users/authentication/SAMLAuthenticate');
68         }
69
70 $contents = <<<EOQ
71 <?php
72         require('modules/Users/authentication/SAMLAuthenticate/lib/onelogin/saml.php');
73         require(get_custom_file_if_exists('modules/Users/authentication/SAMLAuthenticate/settings.php'));
74
75         \$authrequest = new SamlAuthRequest(\$settings);
76         \$url = \$authrequest->create();
77         echo \$url;
78 EOQ;
79
80         file_put_contents('custom/modules/Users/authentication/SAMLAuthenticate/index.php', $contents);
81
82 $contents = <<<EOQ
83 <?php
84                 // this function should be modified to return the SAML settings for the current use
85                 \$settings = new SamlSettings();
86                 // when using Service Provider Initiated SSO (starting at index.php), this URL asks the IdP to authenticate the user.
87                 \$settings->idp_sso_target_url = 'www.sugarcrm.com';
88
89                 // the certificate for the users account in the IdP
90                 \$settings->x509certificate = \$GLOBALS['sugar_config']['SAML_X509Cert'];
91
92                 // The URL where to the SAML Response/SAML Assertion will be posted
93                 \$settings->assertion_consumer_service_url = \$GLOBALS['sugar_config']['site_url']. "/index.php?module=Users&action=Authenticate";
94
95                 // Name of this application
96                 \$settings->issuer = "php-saml";
97
98                 // Tells the IdP to return the email address of the current user
99                 \$settings->name_identifier_format = "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress";
100
101         ?>
102 EOQ;
103
104         if(file_exists('custom/modules/Users/authentication/SAMLAuthenticate/settings.php')) {
105            $this->customContents = file_get_contents('custom/modules/Users/authentication/SAMLAuthenticate/settings.php');
106         }
107
108         file_put_contents('custom/modules/Users/authentication/SAMLAuthenticate/settings.php', $contents);
109         }
110
111         public function tearDown()
112         {
113         //If we had a custom settings.php file already, just restore it
114         if(!empty($this->customContents))
115         {
116             file_put_contents('custom/modules/Users/authentication/SAMLAuthenticate/settings.php', $this->customContents);
117         } else {
118             unlink('custom/modules/Users/authentication/SAMLAuthenticate/settings.php');
119         }
120
121         //Remove the test index.php file
122         if(file_exists('custom/modules/Users/authentication/SAMLAuthenticate/index.php'))
123         {
124             unlink('custom/modules/Users/authentication/SAMLAuthenticate/index.php');
125         }
126
127             unset($GLOBALS['current_user']);
128             $GLOBALS['sugar_config'] = $this->sugar_config_old;
129             unset($_REQUEST['login_module']);
130         unset($_REQUEST['login_action']);
131         unset($_REQUEST['login_record']);
132         unset($_REQUEST['user_name']);
133         unset($_REQUEST['user_password']);
134         unset($_SESSION['authenticated_user_id']);
135         unset($_SESSION['hasExpiredPassword']);
136         unset($_SESSION['isMobile']);
137         }
138
139     public function testLoadCustomSettingsFromIndex()
140     {
141         require('custom/modules/Users/authentication/SAMLAuthenticate/index.php');
142         $this->expectOutputRegex('/www\.sugarcrm\.com/', 'Failed to override custom/modules/Users/authentication/SAMLAuthenticate/settings.php');
143     }
144
145
146 }
147 ?>