/* * RClientTest.java * * Copyright (C) 2010-2016, Microsoft Corporation * * This program is licensed to you under the terms of Version 2.0 of the * Apache License. This program is distributed WITHOUT * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the * Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0) for more details. * */ package com.revo.deployr.client; import com.revo.deployr.DeployrUtil; import com.revo.deployr.client.auth.basic.RBasicAuthentication; import com.revo.deployr.client.factory.RClientFactory; import org.junit.*; import java.util.List; import static org.junit.Assert.*; public class RClientTest { RClient rClient = null; String url; public RClientTest() { } @BeforeClass public static void setUpClass() throws Exception { } @AfterClass public static void tearDownClass() throws Exception { } @Before public void setUp() { try { String url = System.getProperty("connection.protocol") + System.getProperty("connection.endpoint"); if (url == null) { fail("setUp: connection.[protocol|endpoint] null."); } boolean allowSelfSigned = Boolean.valueOf(System.getProperty("allow.SelfSignedSSLCert")); rClient =RClientFactory.createClient(url, allowSelfSigned); } catch (Exception ex) { if (rClient != null) { rClient.release(); } fail("setUp: " + ex); } } @After public void tearDown() { if (rClient != null) { rClient.release(); } } /** * Test of login method, of class RClient. */ @Test public void testRClientLoginRAuthentication() { // Test variables. String userName = "testuser"; RUser rUser = null; RBasicAuthentication pAuthentication = null; RProject rProject = null; // Test error handling. Exception exception = null; String exceptionMsg = ""; Exception cleanupException = null; String cleanupExceptionMsg = ""; // Test. pAuthentication = new RBasicAuthentication(userName, System.getProperty("password.testuser")); try { rUser = rClient.login(pAuthentication); } catch (Exception ex) { exception = ex; exceptionMsg = "rClient.login failed: "; } // Test user is logged in if (exception == null) { try { rProject = rUser.createProject(); } catch (Exception ex) { exception = ex; exceptionMsg = "rUser.createProject failed: "; } } // Test cleanup. if (rProject != null) { try { rProject.close(); } catch (Exception ex) { cleanupException = ex; cleanupExceptionMsg = "rProject.close failed: "; } } if (rUser != null) { try { rClient.logout(rUser); } catch (Exception ex) { cleanupException = ex; cleanupExceptionMsg = "rClient.logout failed: "; } } // Test asserts. if (exception == null) { assertEquals(userName, rUser.about().username); assertNotNull(rProject); } else { fail(exceptionMsg + exception.getMessage()); } // Test cleanup errors. if (cleanupException != null) { fail(cleanupExceptionMsg + cleanupException.getMessage()); } } /** * Test for `X-XSRF-TOKEN` response header. This token is generated and * returned during `/r/user/login`. This test also tests for the proper * `X-XSRF-TOKEN` request header in an authenticated `/r/user/logout` call. * This is somewhat implied. */ @Test public void testRClientLoginRAuthenticationCSRF() { // Test variables. String userName = "testuser"; RUser rUser = null; RBasicAuthentication pAuthentication = null; RProject rProject = null; // Test error handling. Exception exception = null; String exceptionMsg = ""; Exception cleanupException = null; String cleanupExceptionMsg = ""; // Test. pAuthentication = new RBasicAuthentication(userName, System.getProperty("password.testuser")); try { rUser = rClient.login(pAuthentication); } catch (Exception ex) { exception = ex; exceptionMsg = "rClient.login failed: "; } if (rUser != null) { try { rClient.logout(rUser); } catch (Exception ex) { cleanupException = ex; cleanupExceptionMsg = "rClient.logout failed: "; } } // Test asserts. if (exception == null) { assertNotNull(rUser.about().csrf); } else { fail(exceptionMsg + exception.getMessage()); } // Test cleanup errors. if (cleanupException != null) { fail(cleanupExceptionMsg + cleanupException.getMessage()); } } /** * Test for `X-XSRF-TOKEN` header behavior during authenticated calls. This * token is generated and returned during `/r/user/login`. The logout API * `/r/user/logout` is an authenticated call hence should be rejected with a * HTTP 403 error. */ @Test public void testRClientLoginRAuthenticationCSRFFailure() { // Test variables. int errorCode = -1; // // Test: // - /r/user/lgout (should fail by desing with HTTP 403) // try { rClient.logout(null); // not authenticated } catch (RSecurityException ex) { errorCode = ex.errorCode; } catch (Exception ex) { /* noop */ errorCode = -1; } // we should fail with a HTTP 403 assertEquals(RSecurityException.AUTHORIZATION, errorCode); } /** * Test of login method, of class RClient. */ @Test public void testRClientLoginRAuthenticationDisableAutosave() throws Exception { // Test variables. RBasicAuthentication pAuthentication = new RBasicAuthentication("testuser", System.getProperty("password.testuser")); boolean disableAutosave = true; String code = "x<-5"; RUser rUser = null; boolean autoSave = false; RProject rProject = null; RProject getProject = null; RProjectExecution projectExecution = null; List<RProjectExecution> listProjectExecution = null; List<RProjectExecution> listGetProjectExecution = null; String pid = ""; // Test error handling. Exception exception = null; String exceptionMsg = ""; Exception cleanupException = null; String cleanupExceptionMsg = ""; try { rUser = rClient.login(pAuthentication, disableAutosave); } catch (Exception ex) { exception = ex; exceptionMsg = "rClient.login failed: "; } if (exception == null) { rProject = DeployrUtil.createPersistentProject(rUser, "test autosave", "test autosave desc"); pid = rProject.about().id; assertNotNull(rProject); } if (exception == null) { try { projectExecution = rProject.executeCode(code); listProjectExecution = rProject.getHistory(); } catch (Exception ex) { exception = ex; exceptionMsg = "rProject.executeCode failed: "; } } if (exception == null) { try { rProject.close(); } catch (Exception ex) { exception = ex; exceptionMsg = "rProject.close failed: "; } } if (exception == null) { try { getProject = rUser.getProject(pid); listGetProjectExecution = getProject.getHistory(); } catch (Exception ex) { exception = ex; exceptionMsg = "rUser.getProject failed: "; } } // Test cleanup. if (rProject != null) { try { rProject.delete(); } catch (Exception ex) { cleanupException = ex; cleanupExceptionMsg = "rProject.delete failed: "; } } if (rUser != null) { try { rClient.logout(rUser); } catch (Exception ex) { cleanupException = ex; cleanupExceptionMsg = "rClient.logout failed: "; } } // Test asserts. if (exception == null) { assertEquals(1, listProjectExecution.size()); assertEquals(0, listGetProjectExecution.size()); } else { fail(exceptionMsg + exception.getMessage()); } // Test cleanup errors. if (cleanupException != null) { fail(cleanupExceptionMsg + cleanupException.getMessage()); } } /** * Test of logout method, of class RClient. */ @Test public void testLogout() { // Test variables. RBasicAuthentication pAuthentication = new RBasicAuthentication("testuser", System.getProperty("password.testuser")); String userName = "testuser"; RUser rUser = null; // Test error handling. Exception exception = null; String exceptionMsg = ""; try { // Test. rUser = rClient.login(pAuthentication); } catch (Exception ex) { exception = ex; exceptionMsg = "rClient.login failed: "; } if (exception == null) { try { rClient.logout(rUser); } catch (Exception ex) { exception = ex; exceptionMsg = "rClient.logout failed: "; } } // Test asserts. if (exception == null) { assertEquals(userName, rUser.about().username); } else { fail(exceptionMsg + exception.getMessage()); } } }