1 /*
2  * SessionScopeTests.java
3  *
4  * Copyright (C) 2021 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant
7  * to the terms of a commercial license agreement with RStudio, then
8  * this program is licensed to you under the terms of version 3 of the
9  * GNU Affero General Public License. This program is distributed WITHOUT
10  * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12  * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13  *
14  */
15 package org.rstudio.studio.client.application.model;
16 
17 import com.google.gwt.junit.client.GWTTestCase;
18 
19 
20 public class SessionScopeTests extends GWTTestCase
21 {
22    @Override
getModuleName()23    public String getModuleName()
24    {
25       return "org.rstudio.studio.RStudioTests";
26    }
27 
testFullScopeString()28    public void testFullScopeString()
29    {
30       SessionScope scope = new SessionScope("12345abcdefghijklmnop");
31       assertEquals(scope.getUserId(), "12345");
32       assertEquals(scope.getProjectId(), "abcdefgh");
33       assertEquals(scope.getSessionId(), "ijklmnop");
34    }
35 
testSessionOnlyScopeString()36    public void testSessionOnlyScopeString()
37    {
38       SessionScope scope = new SessionScope("12345678");
39       assertEquals(scope.getUserId(), null);
40       assertEquals(scope.getProjectId(), null);
41       assertEquals(scope.getSessionId(), "12345678");
42    }
43 
testInvalidString()44    public void testInvalidString()
45    {
46       SessionScope scope = new SessionScope("foo");
47       assertEquals(scope.getUserId(), null);
48       assertEquals(scope.getProjectId(), null);
49       assertEquals(scope.getSessionId(), null);
50    }
51 
testFullLocalHostUrl()52    public void testFullLocalHostUrl()
53    {
54       SessionScope scope = SessionScope.scopeFromUrl("http://localhost:8787/s/d5de8cfc78a31979f75db/");
55       assertEquals(scope.getUserId(), "d5de8");
56       assertEquals(scope.getProjectId(), "cfc78a31");
57       assertEquals(scope.getSessionId(), "979f75db");
58    }
59 
testFullLocalHostUrlNoTrailingSlash()60    public void testFullLocalHostUrlNoTrailingSlash()
61    {
62       SessionScope scope = SessionScope.scopeFromUrl("http://localhost:8787/s/d5de8cfc78a31979f75db");
63       assertEquals(scope.getUserId(), "d5de8");
64       assertEquals(scope.getProjectId(), "cfc78a31");
65       assertEquals(scope.getSessionId(), "979f75db");
66    }
67 
testFullUrl()68    public void testFullUrl()
69    {
70       SessionScope scope = SessionScope.scopeFromUrl("https://rsp.rstudioservices.com/s/f96bb2bd7dace6d420ad2/");
71       assertEquals(scope.getUserId(), "f96bb");
72       assertEquals(scope.getProjectId(), "2bd7dace");
73       assertEquals(scope.getSessionId(), "6d420ad2");
74    }
75 }
76