@Test public void assertWorkspaceFactory() { ListWorkspacesCommand.WorkspaceFactory factory = new Workspaces(server); Workspace workspace = factory.createWorkspace("name", "computer", "owner", "comment"); assertEquals("Workspace name was incorrect", "name", workspace.getName()); assertEquals("Workspace comment was incorrect", "comment", workspace.getComment()); }
@Test public void testAddWorkspace() { int startingVertexCount = graph.getAllVertices().size(); int startingEdgeCount = graph.getAllEdges().size(); String workspaceId = "testWorkspaceId"; idGenerator.push(workspaceId); idGenerator.push(workspaceId + "_to_" + user1.getUserId()); Workspace workspace = workspaceRepository.add("workspace1", user1); assertTrue( authorizationRepository .getGraphAuthorizations() .contains(WorkspaceRepository.WORKSPACE_ID_PREFIX + workspaceId)); assertEquals( startingVertexCount + 1, graph.getAllVertices().size()); // +1 = the workspace vertex assertEquals( startingEdgeCount + 1, graph.getAllEdges().size()); // +1 = the edge between workspace and user1 assertNull( "Should not have access", graph.getVertex(workspace.getId(), new InMemoryAuthorizations())); InMemoryAuthorizations authorizations = new InMemoryAuthorizations(WorkspaceRepository.VISIBILITY_STRING, workspace.getId()); assertNotNull("Should have access", graph.getVertex(workspace.getId(), authorizations)); Workspace foundWorkspace = workspaceRepository.findById(workspace.getId(), user1); assertEquals(workspace.getId(), foundWorkspace.getId()); }
@Test public void testDeleteResponse() { new NonStrictExpectations() { { twilioRestClient.request((Request) any); result = new Response("null", TwilioRestClient.HTTP_STATUS_CODE_NO_CONTENT); twilioRestClient.getObjectMapper(); result = new ObjectMapper(); } }; Workspace.deleter("WSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").delete(); }
@Test public void testUpdateResponse() { new NonStrictExpectations() { { twilioRestClient.request((Request) any); result = new Response( "{\"account_sid\": \"ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"date_created\": \"2015-08-03T17:31:38Z\",\"date_updated\": \"2015-08-03T17:31:38Z\",\"default_activity_name\": \"Offline\",\"default_activity_sid\": \"WAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"event_callback_url\": \"\",\"events_filter\": \"\",\"friendly_name\": \"8064de33-3a05-11e5-8bae-98e0d9a1eb73\",\"links\": {\"activities\": \"https://taskrouter.twilio.com/v1/Workspaces/WSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Activities\",\"statistics\": \"https://taskrouter.twilio.com/v1/Workspaces/WSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Statistics\",\"task_queues\": \"https://taskrouter.twilio.com/v1/Workspaces/WSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/TaskQueues\",\"tasks\": \"https://taskrouter.twilio.com/v1/Workspaces/WSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Tasks\",\"workers\": \"https://taskrouter.twilio.com/v1/Workspaces/WSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Workers\",\"workflows\": \"https://taskrouter.twilio.com/v1/Workspaces/WSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Workflows\"},\"multi_task_enabled\": \"false\",\"sid\": \"WSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"timeout_activity_name\": \"Offline\",\"timeout_activity_sid\": \"WAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"url\": \"https://taskrouter.twilio.com/v1/Workspaces/WSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"}", TwilioRestClient.HTTP_STATUS_CODE_OK); twilioRestClient.getObjectMapper(); result = new ObjectMapper(); } }; Workspace.updater("WSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").update(); }
@Test public void testReadEmptyResponse() { new NonStrictExpectations() { { twilioRestClient.request((Request) any); result = new Response( "{\"meta\": {\"first_page_url\": \"https://taskrouter.twilio.com/v1/Workspaces?PageSize=1&Page=0\",\"key\": \"workspaces\",\"next_page_url\": null,\"page\": 0,\"page_size\": 1,\"previous_page_url\": null,\"url\": \"https://taskrouter.twilio.com/v1/Workspaces?PageSize=1&Page=0\"},\"workspaces\": []}", TwilioRestClient.HTTP_STATUS_CODE_OK); twilioRestClient.getObjectMapper(); result = new ObjectMapper(); } }; assertNotNull(Workspace.reader().read()); }
@Test public void testReadFullResponse() { new NonStrictExpectations() { { twilioRestClient.request((Request) any); result = new Response( "{\"meta\": {\"first_page_url\": \"https://taskrouter.twilio.com/v1/Workspaces?PageSize=1&Page=0\",\"key\": \"workspaces\",\"next_page_url\": null,\"page\": 0,\"page_size\": 1,\"previous_page_url\": null,\"url\": \"https://taskrouter.twilio.com/v1/Workspaces?PageSize=1&Page=0\"},\"workspaces\": [{\"account_sid\": \"ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"date_created\": \"2015-05-27T00:48:50Z\",\"date_updated\": \"2015-05-27T00:48:50Z\",\"default_activity_name\": \"Offline\",\"default_activity_sid\": \"WAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"event_callback_url\": \"\",\"events_filter\": \"\",\"friendly_name\": \"cce151db-4644-4d48-95a1-d962829b69f0\",\"links\": {\"activities\": \"https://taskrouter.twilio.com/v1/Workspaces/WSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Activities\",\"statistics\": \"https://taskrouter.twilio.com/v1/Workspaces/WSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Statistics\",\"task_queues\": \"https://taskrouter.twilio.com/v1/Workspaces/WSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/TaskQueues\",\"tasks\": \"https://taskrouter.twilio.com/v1/Workspaces/WSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Tasks\",\"workers\": \"https://taskrouter.twilio.com/v1/Workspaces/WSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Workers\",\"workflows\": \"https://taskrouter.twilio.com/v1/Workspaces/WSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Workflows\"},\"multi_task_enabled\": \"false\",\"sid\": \"WSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"timeout_activity_name\": \"Offline\",\"timeout_activity_sid\": \"WAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"url\": \"https://taskrouter.twilio.com/v1/Workspaces/WSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"}]}", TwilioRestClient.HTTP_STATUS_CODE_OK); twilioRestClient.getObjectMapper(); result = new ObjectMapper(); } }; assertNotNull(Workspace.reader().read()); }
@Test public void testCreateRequest() { new NonStrictExpectations() { { Request request = new Request(HttpMethod.POST, Domains.TASKROUTER.toString(), "/v1/Workspaces"); request.addPostParam("FriendlyName", serialize("friendlyName")); twilioRestClient.request(request); times = 1; result = new Response("", 500); twilioRestClient.getAccountSid(); result = "AC123"; } }; try { Workspace.creator("friendlyName").create(); fail("Expected TwilioException to be thrown for 500"); } catch (TwilioException e) { } }
@Test public void testReadRequest() { new NonStrictExpectations() { { Request request = new Request(HttpMethod.GET, Domains.TASKROUTER.toString(), "/v1/Workspaces"); twilioRestClient.request(request); times = 1; result = new Response("", 500); twilioRestClient.getAccountSid(); result = "AC123"; } }; try { Workspace.reader().read(); fail("Expected TwilioException to be thrown for 500"); } catch (TwilioException e) { } }
@Test public void testEntities() { int startingVertexCount = graph.getAllVertices().size(); int startingEdgeCount = graph.getAllEdges().size(); String workspaceId = "testWorkspaceId"; idGenerator.push(workspaceId); idGenerator.push(workspaceId + "_to_" + user1.getUserId()); Workspace workspace = workspaceRepository.add("workspace1", user1); assertEquals( startingVertexCount + 1, graph.getAllVertices().size()); // +1 = the workspace vertex assertEquals( startingEdgeCount + 1, graph.getAllEdges().size()); // +1 = the edges between workspaces and users try { workspaceRepository.updateEntityOnWorkspace( workspace, entity1Vertex.getId(), true, 100, 100, user2); fail("user2 should not have write access to workspace"); } catch (LumifyAccessDeniedException ex) { assertEquals(user2, ex.getUser()); assertEquals(workspace.getId(), ex.getResourceId()); } idGenerator.push(workspaceId + "_to_" + entity1Vertex.getId()); workspaceRepository.updateEntityOnWorkspace( workspace, entity1Vertex.getId(), true, 100, 200, user1); assertEquals( startingVertexCount + 1, graph.getAllVertices().size()); // +1 = the workspace vertex assertEquals( startingEdgeCount + 2, graph.getAllEdges().size()); // +2 = the edges between workspaces, users, and entities workspaceRepository.updateEntityOnWorkspace( workspace, entity1Vertex.getId(), true, 200, 300, user1); assertEquals( startingVertexCount + 1, graph.getAllVertices().size()); // +1 = the workspace vertex assertEquals( startingEdgeCount + 2, graph.getAllEdges().size()); // +2 = the edges between workspaces, users, and entities List<WorkspaceEntity> entities = workspaceRepository.findEntities(workspace, user1); assertEquals(1, entities.size()); assertEquals(entity1Vertex.getId(), entities.get(0).getEntityVertexId()); assertEquals(200, entities.get(0).getGraphPositionX().intValue()); assertEquals(300, entities.get(0).getGraphPositionY().intValue()); try { workspaceRepository.findEntities(workspace, user2); fail("user2 should not have read access to workspace"); } catch (LumifyAccessDeniedException ex) { assertEquals(user2, ex.getUser()); assertEquals(workspace.getId(), ex.getResourceId()); } try { workspaceRepository.softDeleteEntityFromWorkspace(workspace, entity1Vertex.getId(), user2); fail("user2 should not have write access to workspace"); } catch (LumifyAccessDeniedException ex) { assertEquals(user2, ex.getUser()); assertEquals(workspace.getId(), ex.getResourceId()); } workspaceRepository.softDeleteEntityFromWorkspace(workspace, entity1Vertex.getId(), user1); assertEquals( startingVertexCount + 1, graph.getAllVertices().size()); // +1 = the workspace vertex Map<String, InMemoryEdge> edgesAfterDelete = graph.getAllEdges(); assertEquals( startingEdgeCount + 2, edgesAfterDelete.size()); // +1 = the edges between workspaces, users boolean foundRemovedEdge = false; for (InMemoryEdge edge : edgesAfterDelete.values()) { if (edge.getLabel().equals(workspaceToEntityRelationship.getIRI())) { assertEquals( false, WorkspaceLumifyProperties.WORKSPACE_TO_ENTITY_VISIBLE.getPropertyValue(edge)); foundRemovedEdge = true; } } assertTrue(foundRemovedEdge); }
@Test public void testAccessControl() { int startingVertexCount = graph.getAllVertices().size(); int startingEdgeCount = graph.getAllEdges().size(); String workspace1Id = "testWorkspace1Id"; String workspace1Title = "workspace1"; idGenerator.push(workspace1Id); idGenerator.push(workspace1Id + "_to_" + user1.getUserId()); workspaceRepository.add(workspace1Title, user1); String workspace2Id = "testWorkspace2Id"; String workspace2Title = "workspace2"; idGenerator.push(workspace2Id); idGenerator.push(workspace2Id + "_to_" + user1.getUserId()); workspaceRepository.add(workspace2Title, user1); String workspace3Id = "testWorkspace3Id"; String workspace3Title = "workspace3"; idGenerator.push(workspace3Id); idGenerator.push(workspace3Id + "_to_" + user2.getUserId()); workspaceRepository.add(workspace3Title, user2); assertEquals( startingVertexCount + 3, graph.getAllVertices().size()); // +3 = the workspace vertices assertEquals( startingEdgeCount + 3, graph.getAllEdges().size()); // +3 = the edges between workspaces and users List<Workspace> user1Workspaces = toList(workspaceRepository.findAll(user1)); assertEquals(2, user1Workspaces.size()); boolean foundWorkspace1 = false; boolean foundWorkspace2 = false; for (Workspace workspace : user1Workspaces) { if (workspace.getDisplayTitle().equals(workspace1Title)) { foundWorkspace1 = true; } else if (workspace.getDisplayTitle().equals(workspace2Title)) { foundWorkspace2 = true; } } assertTrue("foundWorkspace1", foundWorkspace1); assertTrue("foundWorkspace2", foundWorkspace2); List<Workspace> user2Workspaces = toList(workspaceRepository.findAll(user2)); assertEquals(1, user2Workspaces.size()); assertEquals(workspace3Title, user2Workspaces.get(0).getDisplayTitle()); try { workspaceRepository.updateUserOnWorkspace( user2Workspaces.get(0), user1.getUserId(), WorkspaceAccess.READ, user1); fail("user1 should not have access to user2's workspace"); } catch (LumifyAccessDeniedException ex) { assertEquals(user1, ex.getUser()); assertEquals(user2Workspaces.get(0).getId(), ex.getResourceId()); } idGenerator.push(workspace3Id + "to" + user2.getUserId()); workspaceRepository.updateUserOnWorkspace( user2Workspaces.get(0), user1.getUserId(), WorkspaceAccess.READ, user2); assertEquals( startingVertexCount + 3, graph.getAllVertices().size()); // +3 = the workspace vertices assertEquals( startingEdgeCount + 4, graph.getAllEdges().size()); // +4 = the edges between workspaces and users List<WorkspaceUser> usersWithAccess = workspaceRepository.findUsersWithAccess(user2Workspaces.get(0).getId(), user2); boolean foundUser1 = false; boolean foundUser2 = false; for (WorkspaceUser userWithAccess : usersWithAccess) { if (userWithAccess.getUserId().equals(user1.getUserId())) { assertEquals(WorkspaceAccess.READ, userWithAccess.getWorkspaceAccess()); foundUser1 = true; } else if (userWithAccess.getUserId().equals(user2.getUserId())) { assertEquals(WorkspaceAccess.WRITE, userWithAccess.getWorkspaceAccess()); foundUser2 = true; } else { fail("Unexpected user " + userWithAccess.getUserId()); } } assertTrue("could not find user1", foundUser1); assertTrue("could not find user2", foundUser2); try { workspaceRepository.deleteUserFromWorkspace(user2Workspaces.get(0), user1.getUserId(), user1); fail("user1 should not have write access to user2's workspace"); } catch (LumifyAccessDeniedException ex) { assertEquals(user1, ex.getUser()); assertEquals(user2Workspaces.get(0).getId(), ex.getResourceId()); } try { workspaceRepository.delete(user2Workspaces.get(0), user1); fail("user1 should not have write access to user2's workspace"); } catch (LumifyAccessDeniedException ex) { assertEquals(user1, ex.getUser()); assertEquals(user2Workspaces.get(0).getId(), ex.getResourceId()); } workspaceRepository.updateUserOnWorkspace( user2Workspaces.get(0), user1.getUserId(), WorkspaceAccess.WRITE, user2); assertEquals( startingVertexCount + 3, graph.getAllVertices().size()); // +3 = the workspace vertices assertEquals( startingEdgeCount + 4, graph.getAllEdges().size()); // +4 = the edges between workspaces and users workspaceRepository.deleteUserFromWorkspace(user2Workspaces.get(0), user1.getUserId(), user2); assertEquals( startingVertexCount + 3, graph.getAllVertices().size()); // +3 = the workspace vertices assertEquals( startingEdgeCount + 3, graph.getAllEdges().size()); // +3 = the edges between workspaces and users workspaceRepository.delete(user2Workspaces.get(0), user2); assertEquals( startingVertexCount + 2, graph.getAllVertices().size()); // +2 = the workspace vertices assertEquals( startingEdgeCount + 2, graph.getAllEdges().size()); // +2 = the edges between workspaces and users }