View Javadoc
1   /*
2    * Copyright (C) 2003-2014 eXo Platform SAS.
3    *
4    * This is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU Lesser General Public License as
6    * published by the Free Software Foundation; either version 3 of
7    * the License, or (at your option) any later version.
8    *
9    * This software is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this software; if not, write to the Free
16   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
18   */
19  package org.exoplatform.mobile.tests;
20  
21  import static org.hamcrest.CoreMatchers.equalTo;
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertThat;
24  import static org.junit.Assert.assertTrue;
25  
26  import org.exoplatform.model.ExoAccount;
27  import org.exoplatform.utils.ExoUtils;
28  import org.robolectric.annotation.Config;
29  
30  import android.os.Build;
31  
32  import org.junit.After;
33  import org.junit.Before;
34  import org.junit.Test;
35  import org.junit.runner.RunWith;
36  import org.robolectric.RobolectricTestRunner;
37  
38  /**
39   * Created by The eXo Platform SAS Author : Philippe Aristote
40   * paristote@exoplatform.com May 13, 2014
41   */
42  @RunWith(RobolectricTestRunner.class)
43  @Config(sdk=Build.VERSION_CODES.LOLLIPOP) // API LEVEL 21
44  public class ExoAccountInfoValidationTest {
45  
46      @Before
47      public void setup() {
48      }
49  
50      @After
51      public void teardown() {
52      }
53  
54      @Test
55      public void testForbiddenTenantNames() {
56  
57          final String[] CLOUD_URLS_FORBIDDEN = { "http://exoplatform.net",
58                  "http://wks-acc.exoplatform.org", "http://netstg.exoplatform.org" };
59  
60          for (String string : CLOUD_URLS_FORBIDDEN) {
61              assertTrue(string + " should be forbidden", ExoUtils.isURLForbidden(string));
62          }
63  
64      }
65  
66      @Test
67      public void testShouldValidateCorrectEmails() {
68  
69          final String[] EMAILS_OK = { "test@example.com", "test.test@example.com",
70                  "test-test@example.com", "test_test@example.com", "test+test@example.com",
71                  "test@test.example.com", "test@test-example.com" };
72  
73          for (String string : EMAILS_OK) {
74              assertTrue(string + " should be valid", ExoUtils.isEmailValid(string));
75          }
76  
77      }
78  
79      @Test
80      public void testShouldNotValidateIncorrectEmailsAndNull() {
81  
82          final String[] EMAILS_INCORRECT = { "example.com", "@example.com", "test", "test@",
83                  "test@.com" };
84  
85          for (String string : EMAILS_INCORRECT) {
86              assertFalse(string + " should not be validated", ExoUtils.isEmailValid(string));
87          }
88  
89          assertFalse("null should not be validated", ExoUtils.isEmailValid(null));
90      }
91  
92      @Test
93      public void testShouldValidateCorrectURLs() {
94  
95          final String[] URLS_OK = { "test.com", "test.example.com", "test-example.com", "test.fr",
96                  "test.info", "test.org", "test.net", "test.co.uk", "http://test.com",
97                  "https://test.com", "t.e.s.t.com", "10.100.10.1", "test.com:80", "test123.com",
98                  "www.test.com/some/path" };
99  
100         for (String string : URLS_OK) {
101             assertTrue(string + " should be valid", ExoUtils.isUrlValid(string));
102         }
103 
104     }
105 
106     @Test
107     public void testShouldNotValidateIncorrectURLsAndNull() {
108 
109         final String[] URLS_INCORRECT = { "test.", ".com", "http://test{}.com",
110                 "https://test().com", "test[].com", "test!.com", "test@.com", "test#.com",
111                 "test$.com", "test&.com", "test*.com", "test|.com", "test~.com",
112                 "test example.com", "test_example.com", "http:test-example.org",
113                 "http//test-example.org", "0.0.0.0", "300.10.100.200", "test.10.20.30" };
114 
115         for (String string : URLS_INCORRECT) {
116             assertFalse(string + " should not be validated", ExoUtils.isUrlValid(string));
117         }
118 
119         assertFalse("null should not be validated", ExoUtils.isUrlValid(null));
120     }
121 
122     @Test
123     public void testShouldStripCorrectURLs() {
124 
125         String expectedUrl = "http://test.com";
126         String[] HTTP_URLS = { "test.com", "http://test.com", "test.com/some/long/path/",
127                 "http://test.com/shortpath/", "http://test.com/index.html" };
128 
129         for (String string : HTTP_URLS) {
130             String url = ExoUtils.stripUrl(string);
131             assertThat("URL '" + string + "' should have been stripped to " + expectedUrl,
132                        url,
133                        equalTo(expectedUrl));
134         }
135 
136         expectedUrl = "https://test.com";
137         String[] HTTPS_URLS = { "https://test.com", "https://test.com/some/long/path/",
138                 "https://test.com/shortpath/", "https://test.com/index.html" };
139 
140         for (String string : HTTPS_URLS) {
141             String url = ExoUtils.stripUrl(string);
142             assertThat("URL '" + string + "' should have been stripped to " + expectedUrl,
143                        url,
144                        equalTo(expectedUrl));
145         }
146 
147         // Other URLs with ports
148         String myURL = "http://test.fr:8080";
149         expectedUrl = "http://test.fr:8080";
150         String url = ExoUtils.stripUrl(myURL);
151         assertThat("URL '" + myURL + "' should have been stripped to " + expectedUrl,
152                    url,
153                    equalTo(expectedUrl));
154 
155         myURL = "https://test.fr:443";
156         expectedUrl = "https://test.fr:443";
157         url = ExoUtils.stripUrl(myURL);
158         assertThat("URL '" + myURL + "' should have been stripped to " + expectedUrl,
159                    url,
160                    equalTo(expectedUrl));
161 
162         myURL = "http://test.fr:80";
163         expectedUrl = "http://test.fr:80";
164         url = ExoUtils.stripUrl(myURL);
165         assertThat("URL '" + myURL + "' should have been stripped to " + expectedUrl,
166                    url,
167                    equalTo(expectedUrl));
168 
169     }
170 
171     // @Test
172     public void testStripShouldFailWithIncorrectURLsAndNull() {
173 
174     }
175 
176     @Test
177     public void testAccountsAreEqual() {
178 
179         ExoAccount account1 = new ExoAccount();
180         ExoAccount account2 = new ExoAccount();
181 
182         // account 1
183         account1.accountName = "test server";
184         account1.serverUrl = "http://test-example.com";
185         account1.username = "testuser";
186         // account 2
187         account2.accountName = "test server";
188         account2.serverUrl = "http://test-example.com";
189         account2.username = "testuser";
190         // test
191         assertTrue("Account 1 and 2 should be equal", account1.equals(account2));
192 
193         // empty usernames
194         account1.username = "";
195         account2.username = "";
196         assertTrue("Account 1 and 2 should be equal with empty usernames",
197                    account1.equals(account2));
198 
199         // different passwords
200         account1.password = "password";
201         account2.password = "other-password";
202         assertTrue("Account 1 and 2 should be equal with different passwords",
203                    account1.equals(account2));
204 
205         // different auto login option
206         account1.isAutoLoginEnabled = true;
207         account2.isAutoLoginEnabled = false;
208         assertTrue("Account 1 and 2 should be equal with different auto login option",
209                    account1.equals(account2));
210 
211         // different remember me option
212         account1.isRememberEnabled = true;
213         account2.isRememberEnabled = false;
214         assertTrue("Account 1 and 2 should be equal with different remember me option",
215                    account1.equals(account2));
216 
217     }
218 
219     @Test
220     public void testAccountsAreNotEqual() {
221 
222         ExoAccount account1 = new ExoAccount();
223         ExoAccount account2 = new ExoAccount();
224 
225         // different server name
226         account1.accountName = "test server";
227         account2.accountName = "another test server";
228         account1.serverUrl = "http://test-example.com";
229         account2.serverUrl = "http://test-example.com";
230         account1.username = "testuser";
231         account2.username = "testuser";
232         assertFalse("Account 1 and 2 should be different, server names are not equal",
233                     account1.equals(account2));
234 
235         // different server URL
236         account1.accountName = "test server";
237         account2.accountName = "test server";
238         account1.serverUrl = "http://test-example.com";
239         account2.serverUrl = "http://another-test-example.com";
240         account1.username = "testuser";
241         account2.username = "testuser";
242         assertFalse("Account 1 and 2 should be different, URLs are not equal",
243                     account1.equals(account2));
244 
245         // different username
246         account1.accountName = "test server";
247         account2.accountName = "test server";
248         account1.serverUrl = "http://test-example.com";
249         account2.serverUrl = "http://test-example.com";
250         account1.username = "testuser";
251         account2.username = "othertestuser";
252         assertFalse("Account 1 and 2 should be different, usernames are not equal",
253                     account1.equals(account2));
254 
255     }
256 
257 }