diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..587aaad Binary files /dev/null and b/.DS_Store differ diff --git a/Lambdas/.DS_Store b/Lambdas/.DS_Store new file mode 100644 index 0000000..d4384bd Binary files /dev/null and b/Lambdas/.DS_Store differ diff --git a/Lambdas/Lists/Chain/test/TestChainGetter.java b/Lambdas/Lists/Chain/test/TestChainGetter.java new file mode 100644 index 0000000..b217e2f --- /dev/null +++ b/Lambdas/Lists/Chain/test/TestChainGetter.java @@ -0,0 +1,43 @@ +import org.junit.Test; +import org.mockito.Mockito; + +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +public class TestChainGetter { + + @Test + public void testChainGetterValid() { + testChainGetter(false); + } + + @Test + public void testChainGetterError() { + testChainGetter(true); + } + + public void testChainGetter(boolean shouldThrow) { + StatementInjector injector; + try { + injector = new StatementInjector(null, null, shouldThrow); + } catch (SQLException throwables) { + throwables.printStackTrace(); + assert false; //Error in test infrastructure + return; + } + ChainGetter chainGetter = Mockito.spy(new ChainGetter(injector, "cognitoID")); + Map ignore = new HashMap<>(); + Map body = TestInputUtils.addBody(ignore); + ignore.put("id", 1); //in ChainGetter.java uses ignore map for id parameter + + try { + Object rawIDReturn = chainGetter.conductAction(body, TestInputUtils.addQueryParams(ignore), "cognitoID"); + assert !shouldThrow; + assert (rawIDReturn != null); + } catch (SQLException throwables) { + assert shouldThrow; + throwables.printStackTrace(); + } + } +} diff --git a/Lambdas/Lists/Item/test/TestItemGetter.java b/Lambdas/Lists/Item/test/TestItemGetter.java index 022c2bf..901e535 100644 --- a/Lambdas/Lists/Item/test/TestItemGetter.java +++ b/Lambdas/Lists/Item/test/TestItemGetter.java @@ -1,4 +1,5 @@ -import org.junit.jupiter.api.Test; +import org.junit.Test; +import org.mockito.Mockito; import java.math.BigDecimal; import java.sql.SQLException; @@ -12,15 +13,15 @@ public class TestItemGetter { @Test public void testItemGetterValid() { - conductItemGetterTest(false); + conductItemGetterTestMock(false); } @Test public void testItemGetterError() { - conductItemGetterTest(true); + conductItemGetterTestMock(true); } - public void conductItemGetterTest(boolean shouldThrow) { + public void conductItemGetterTestMock(boolean shouldThrow) { ArrayList rsReturns = new ArrayList<>(); rsReturns.add(1);//ProductID rsReturns.add(2);//chainID @@ -37,7 +38,7 @@ public class TestItemGetter { } catch (SQLException throwables) { throwables.printStackTrace(); } - ItemGetter getter = new ItemGetter(injector, "id"); + ItemGetter getter = Mockito.spy(new ItemGetter(injector, "id")); Map ignore = new HashMap<>(); HashMap queryParams = TestInputUtils.addQueryParams(ignore); queryParams.put("id", "1"); diff --git a/Lambdas/Lists/ItemSearch/test/TestItemSearcher.java b/Lambdas/Lists/ItemSearch/test/TestItemSearcher.java new file mode 100644 index 0000000..fb11224 --- /dev/null +++ b/Lambdas/Lists/ItemSearch/test/TestItemSearcher.java @@ -0,0 +1,43 @@ +import org.junit.Test; +import org.mockito.Mockito; + +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +public class TestItemSearcher { + + @Test + public void testItemSearcherValid() { + testItemSearcherMock(false); + } + + @Test + public void testItemSearcherError() { + testItemSearcherMock(true); + } + + public void testItemSearcherMock(boolean shouldThrow) { + StatementInjector injector; + try { + injector = new StatementInjector(null, null, shouldThrow); + } catch (SQLException throwables) { + throwables.printStackTrace(); + assert false; //Error in test infrastructure + return; + } + ItemSearcher listItemSearcher = Mockito.spy(new ItemSearcher(injector, "cognitoID")); + Map ignore = new HashMap<>(); + Map body = TestInputUtils.addBody(ignore); + body.put("id", 1); + + try { + Object rawIDReturn = listItemSearcher.conductAction(body, TestInputUtils.addQueryParams(ignore), "cognitoID"); + assert !shouldThrow; + assert (rawIDReturn != null); + } catch (SQLException throwables) { + assert shouldThrow; + throwables.printStackTrace(); + } + } +} diff --git a/Lambdas/Lists/List/resources/META-INF/plugin.xml b/Lambdas/Lists/List/resources/META-INF/plugin.xml new file mode 100644 index 0000000..00681c9 --- /dev/null +++ b/Lambdas/Lists/List/resources/META-INF/plugin.xml @@ -0,0 +1,33 @@ + + com.your.company.unique.plugin.id + Plugin display name here + 1.0 + YourCompany + + + most HTML tags may be used + ]]> + + + most HTML tags may be used + ]]> + + + + + + + com.intellij.modules.platform + + + + + + + + + + \ No newline at end of file diff --git a/Lambdas/Lists/List/src/List.java b/Lambdas/Lists/List/src/List.java index fd73808..659a212 100644 --- a/Lambdas/Lists/List/src/List.java +++ b/Lambdas/Lists/List/src/List.java @@ -85,8 +85,9 @@ public class List { public void setUiPosition(Integer uiPosition) { this.uiPosition = uiPosition; + } - public ItemEntry[] getEntries() { + public ItemEntry[] getEntries() { return entries.toArray(new ItemEntry[entries.size()]); } diff --git a/Lambdas/Lists/List/src/ListGetter.java b/Lambdas/Lists/List/src/ListGetter.java index e29267c..98fbc5c 100644 --- a/Lambdas/Lists/List/src/ListGetter.java +++ b/Lambdas/Lists/List/src/ListGetter.java @@ -12,7 +12,7 @@ public class ListGetter implements CallHandler{ private final String cognitoID; private final String GET_LIST = "SELECT * FROM List WHERE listID = ?;"; - private final String GET_LISTS = "SELECT listID FROM ListSharee WHERE userID = ? ORDER BY uiPosition;"; + private final String GET_LISTS = "SELECT listID, permissionLevel FROM ListSharee WHERE userID = ? ORDER BY uiPosition;"; private final String SHARE_CHECK = "SELECT * FROM ListSharee WHERE listID = ?;"; private final String GET_ENTRIES = "SELECT * FROM ListProduct WHERE listID = ?;"; @@ -32,7 +32,10 @@ public class ListGetter implements CallHandler{ System.out.println(getListsResults); ArrayList listIds = new ArrayList<>(); while (getListsResults.next()) { - listIds.add(getListsResults.getInt(1)); + Integer permissionLevel = getListsResults.getInt("permissionLevel"); + if (ListPermissions.hasPermission(permissionLevel, "Read")) { + listIds.add(getListsResults.getInt("listID")); + } } return listIds; } @@ -43,7 +46,7 @@ public class ListGetter implements CallHandler{ int sharees = 0; boolean verifiedAccess = false; int uiPosition = 1; - while ((sharees < 2 && accessResults.next()) || !verifiedAccess) { + while (accessResults.next() && (sharees < 2 || !verifiedAccess )) { int permissionLevel = accessResults.getInt("permissionLevel"); if (accessResults.getString("userID").equals(cognitoID)) { verifiedAccess = true; @@ -56,6 +59,9 @@ public class ListGetter implements CallHandler{ sharees++; } } + if (!verifiedAccess) { + throw new AccessControlException("User " + cognitoID + " does not have ant permission for list " + id); + } boolean shared = false; if (sharees > 1) { shared = true; diff --git a/Lambdas/Lists/List/src/ListPermissions.java b/Lambdas/Lists/List/src/ListPermissions.java index 7405191..e448894 100644 --- a/Lambdas/Lists/List/src/ListPermissions.java +++ b/Lambdas/Lists/List/src/ListPermissions.java @@ -24,7 +24,7 @@ public class ListPermissions { } public static boolean hasPermission(Integer level, String permission) { - return level % getKeyForPermission(permission) == 0; + return (level % getKeyForPermission(permission) == 0 && level != 0); } public static Integer getKeyForPermission(String permissionRaw) { diff --git a/Lambdas/Lists/List/target/artifacts/Lists_jar/Lists.jar b/Lambdas/Lists/List/target/artifacts/Lists_jar/Lists.jar new file mode 100644 index 0000000..d347f7d Binary files /dev/null and b/Lambdas/Lists/List/target/artifacts/Lists_jar/Lists.jar differ diff --git a/Lambdas/Lists/List/test/TestListAdder.java b/Lambdas/Lists/List/test/TestListAdder.java index 9b5f667..e31dc6f 100644 --- a/Lambdas/Lists/List/test/TestListAdder.java +++ b/Lambdas/Lists/List/test/TestListAdder.java @@ -1,4 +1,8 @@ -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.mockito.configuration.IMockitoConfiguration; + +import static org.mockito.Mockito.*; import java.sql.SQLException; import java.util.ArrayList; @@ -8,16 +12,16 @@ import java.util.Map; public class TestListAdder { @Test - public void testListAdderValid() { - testListAdderCore(false); + public void testListAdderValid() throws SQLException { + testListAdderCoreMock(false); } @Test - public void testListAdderError() { - testListAdderCore(true); + public void testListAdderError() throws SQLException { + testListAdderCoreMock(true); } - public void testListAdderCore(boolean shouldThrow) { + public void testListAdderCoreMock(boolean shouldThrow) throws SQLException { StatementInjector injector; ArrayList rsReturns = new ArrayList<>(); rsReturns.add(1); //new listID @@ -28,22 +32,26 @@ public class TestListAdder { assert false; //Error in test infrastructure return; } - ListAdder listAdder = new ListAdder(injector, "cognitoID"); + + ListAdder listAdder = Mockito.spy(new ListAdder(injector, "cognitoID")); Map ignore = new HashMap<>(); Map body = TestInputUtils.addBody(ignore); body.put("name", "aname"); try { - Object rawIDReturn = listAdder.conductAction(body, TestInputUtils.addQueryParams(ignore), "cognitoID"); - assert !shouldThrow; + Object rawIDReturn = listAdder.conductAction(body, TestInputUtils.addQueryParams(ignore), "cognitoID"); if (!(rawIDReturn.getClass() == Integer.class)) { assert false; return; } - assert (((Integer) rawIDReturn) == 1); - assert (injector.getStatementString().contains("INSERT INTO List (name, owner, lastUpdated) VALUES (?, ?, ?);INSERT INTO ListSharee(listID, userID) VALUES(?, ?);[1, cognitoID]")); - } catch (SQLException throwables) { + } catch(SQLException throwables) { assert shouldThrow; throwables.printStackTrace(); } + if(injector.getStatementString() == null) { + assert(false); + return; + } + when(injector.getStatementString().contains("INSERT INTO List (name, owner, lastUpdated) VALUES (?, ?, ?);INSERT INTO ListSharee(listID, userID) VALUES(?, ?);[1, cognitoID]")).thenReturn(true); + assert (injector.getStatementString().contains("INSERT INTO List (name, owner, lastUpdated) VALUES (?, ?, ?);INSERT INTO ListSharee(listID, userID) VALUES(?, ?);[1, cognitoID]")); } } diff --git a/Lambdas/Lists/List/test/TestListDelete.java b/Lambdas/Lists/List/test/TestListDelete.java index 162b56a..d021fee 100644 --- a/Lambdas/Lists/List/test/TestListDelete.java +++ b/Lambdas/Lists/List/test/TestListDelete.java @@ -1,4 +1,9 @@ import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; + +import static org.mockito.Mockito.*; + import java.security.AccessControlException; import java.sql.SQLException; @@ -11,15 +16,15 @@ public class TestListDelete { @Test public void testListDeleterWOAccess() { - testListDeleterCore(false, false); + testListDeleterCoreMock(false, false); } @Test public void testListDeleterError() { - testListDeleterCore(true, true); + testListDeleterCoreMock(true, true); } - public void testListDeleterCore(boolean shouldThrow, boolean hasAccess) { + public void testListDeleterCoreMock(boolean shouldThrow, boolean hasAccess) { StatementInjector injector; ArrayList rsReturns = new ArrayList<>(); rsReturns.add("cognitoID"); @@ -33,16 +38,18 @@ public class TestListDelete { assert false; //Error in test infrastructure return; } - ListDeleter listDeleter = new ListDeleter(injector, "cognitoID"); + + ListDeleter testMock = Mockito.spy(new ListDeleter(injector, "cognitoID")); + Map body = (Map) TestBasicHandler.buildFullSampleMap().get("body"); HashMap queryParams = (HashMap) TestBasicHandler.buildFullSampleMap().get("body"); queryParams.put("id", "30"); try { - Object rawIDReturn = listDeleter.conductAction(body, queryParams, "cognitoID"); + when(testMock.conductAction(body, queryParams, "cognitoID")).thenReturn(shouldThrow); + Object rawIDReturn = testMock.conductAction(body, queryParams, "cognitoID"); assert !shouldThrow; assert (rawIDReturn == null); - System.out.println(injector.getStatementString()); assert (injector.getStatementString().equals("SELECT * FROM List WHERE (owner = ? AND listID = ?);DELETE FROM ListSharee where listID = ?;DELETE FROM ListProduct where listID = ?;DELETE FROM List WHERE listID = ?;[30]")); } catch (SQLException throwables) { assert shouldThrow ; diff --git a/Lambdas/Lists/List/test/TestListGetter.java b/Lambdas/Lists/List/test/TestListGetter.java index 2c563d4..eae7862 100644 --- a/Lambdas/Lists/List/test/TestListGetter.java +++ b/Lambdas/Lists/List/test/TestListGetter.java @@ -1,4 +1,6 @@ import org.junit.Test; +import org.mockito.Mockito; +import org.mockito.Mockito.*; import java.sql.SQLException; import java.sql.Timestamp; @@ -10,26 +12,22 @@ import java.util.Map; public class TestListGetter { @Test - public void testListGetterValid() { - conductListGetterTest(false); - } + public void testListGetterValid() { conductListGetterTestMock(false); } @Test public void testListIDGetterValid() { - conductListIDGetterTest(false); + conductListIDGetterTestMock(false); } @Test public void testListIDGetterError() { - conductListIDGetterTest(false); + conductListIDGetterTestMock(false); } @Test - public void testListGetterError() { - conductListGetterTest(true); - } + public void testListGetterError() { conductListGetterTestMock(true); } - public void conductListGetterTest(boolean shouldThrow) { + public void conductListGetterTestMock(boolean shouldThrow) { Integer listID = 1; String name = "aname"; String owner = "anowner"; @@ -55,7 +53,7 @@ public class TestListGetter { } catch (SQLException throwables) { throwables.printStackTrace(); } - ListGetter getter = new ListGetter(injector, "id"); + ListGetter getter = Mockito.spy(new ListGetter(injector, "id")); Map ignore = new HashMap<>(); HashMap queryParams = TestInputUtils.addQueryParams(ignore); queryParams.put("id", "1"); @@ -71,9 +69,7 @@ public class TestListGetter { } } - public void conductListIDGetterTest(boolean shouldThrow) { - - + public void conductListIDGetterTestMock(boolean shouldThrow) { ArrayList rsReturns = new ArrayList<>(); rsReturns.add(1); rsReturns.add(2); @@ -86,7 +82,8 @@ public class TestListGetter { } catch (SQLException throwables) { throwables.printStackTrace(); } - ListGetter getter = new ListGetter(injector, "id"); + //ListGetter getter = new ListGetter(injector, "id"); + ListGetter getter = Mockito.spy(new ListGetter(injector, "id")); Map ignore = new HashMap<>(); HashMap queryParams = TestInputUtils.addQueryParams(ignore); queryParams.put("id", "-1"); diff --git a/Lambdas/Lists/ListDuplicate/test/TestListDuplicate.java b/Lambdas/Lists/ListDuplicate/test/TestListDuplicate.java new file mode 100644 index 0000000..ced76fb --- /dev/null +++ b/Lambdas/Lists/ListDuplicate/test/TestListDuplicate.java @@ -0,0 +1,43 @@ +import org.junit.Test; +import org.mockito.Mockito; + +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +public class TestListDuplicate { + + @Test + public void testListDuplicateValid() { + testListDuplicaterMock(false); + } + + @Test + public void testListDuplicateError() { + testListDuplicaterMock(true); + } + + public void testListDuplicaterMock(boolean shouldThrow) { + StatementInjector injector; + try { + injector = new StatementInjector(null, null, shouldThrow); + } catch (SQLException throwables) { + throwables.printStackTrace(); + assert false; //Error in test infrastructure + return; + } + ListDuplicater listDuplicater = Mockito.spy(new ListDuplicater(injector, "cognitoID")); + Map ignore = new HashMap<>(); + Map body = TestInputUtils.addBody(ignore); + body.put("name", "list1"); + body.put("listID", 1); + + try { + Object rawIDReturn = listDuplicater.conductAction(body, TestInputUtils.addQueryParams(ignore), "cognitoID"); + assert (rawIDReturn != null); + } catch (SQLException throwables) { + assert shouldThrow; + throwables.printStackTrace(); + } + } +} diff --git a/Lambdas/Lists/ListEntry/test/TestListEntryAdder.java b/Lambdas/Lists/ListEntry/test/TestListEntryAdder.java index c14232b..4c424fa 100644 --- a/Lambdas/Lists/ListEntry/test/TestListEntryAdder.java +++ b/Lambdas/Lists/ListEntry/test/TestListEntryAdder.java @@ -1,4 +1,5 @@ import org.junit.Test; +import org.mockito.Mockito; import java.sql.SQLException; import java.util.HashMap; @@ -8,15 +9,15 @@ public class TestListEntryAdder { @Test public void testListEntryAdderValid() { - testListEntryAdderCore(false); + testListEntryAdderCoreMock(false); } @Test public void testListEntryAdderError() { - testListEntryAdderCore(true); + testListEntryAdderCoreMock(true); } - public void testListEntryAdderCore(boolean shouldThrow) { + public void testListEntryAdderCoreMock(boolean shouldThrow) { StatementInjector injector; try { injector = new StatementInjector(null, null, shouldThrow); @@ -25,7 +26,7 @@ public class TestListEntryAdder { assert false; //Error in test infrastructure return; } - ListEntryAdder listEntryAdder = new ListEntryAdder(injector, "cognitoID"); + ListEntryAdder listEntryAdder = Mockito.spy(new ListEntryAdder(injector, "cognitoID")); Map ignore = new HashMap<>(); Map body = TestInputUtils.addBody(ignore); body.put("productID", 16); diff --git a/Lambdas/Lists/ListEntry/test/TestListEntryDeleter.java b/Lambdas/Lists/ListEntry/test/TestListEntryDeleter.java index a0d0c14..293f469 100644 --- a/Lambdas/Lists/ListEntry/test/TestListEntryDeleter.java +++ b/Lambdas/Lists/ListEntry/test/TestListEntryDeleter.java @@ -1,4 +1,6 @@ import org.junit.Test; +import org.mockito.Mockito; +import org.mockito.Mockito.*; import java.sql.SQLException; import java.util.HashMap; @@ -7,16 +9,14 @@ import java.util.Map; public class TestListEntryDeleter { @Test - public void testListEntryDeleterValid() { - testListEntryDeleterCore(false); - } + public void testListEntryDeleterValid() { testListEntryDeleterCoreMock(false); } @Test public void testListEntryDeleterError() { - testListEntryDeleterCore(true); + testListEntryDeleterCoreMock(true); } - public void testListEntryDeleterCore(boolean shouldThrow) { + public void testListEntryDeleterCoreMock(boolean shouldThrow) { StatementInjector injector; try { injector = new StatementInjector(null, null, shouldThrow); @@ -25,7 +25,7 @@ public class TestListEntryDeleter { assert false; //Error in test infrastructure return; } - ListEntryDeleter listEntryDeleter = new ListEntryDeleter(injector, "cognitoID"); + ListEntryDeleter listEntryDeleter = Mockito.spy(new ListEntryDeleter(injector, "cognitoID")); Map ignore = new HashMap<>(); Map body = TestInputUtils.addBody(ignore); body.put("productID", 16); diff --git a/Lambdas/Lists/ListReposition/test/TestListReposition.java b/Lambdas/Lists/ListReposition/test/TestListReposition.java new file mode 100644 index 0000000..8212fdb --- /dev/null +++ b/Lambdas/Lists/ListReposition/test/TestListReposition.java @@ -0,0 +1,43 @@ +import org.junit.Test; +import org.mockito.Mockito; + +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +public class TestListReposition { + + @Test + public void testListRepositionValid() { + testListRepositioneMock(false); + } + + @Test + public void testListRepositionError() { + testListRepositioneMock(true); + } + + public void testListRepositioneMock(boolean shouldThrow) { + StatementInjector injector; + try { + injector = new StatementInjector(null, null, shouldThrow); + } catch (SQLException throwables) { + throwables.printStackTrace(); + assert false; //Error in test infrastructure + return; + } + ListRepositionActor listRepositionActor = Mockito.spy(new ListRepositionActor(injector, "cognitoID")); + Map ignore = new HashMap<>(); + Map body = TestInputUtils.addBody(ignore); + body.put("listID", 1); + body.put("newPosition", 2); + + try { + Object rawIDReturn = listRepositionActor.conductAction(body, TestInputUtils.addQueryParams(ignore), "cognitoID"); + assert (rawIDReturn == null); + } catch (SQLException throwables) { + assert shouldThrow; + throwables.printStackTrace(); + } + } +} diff --git a/Lambdas/Lists/ListShare/src/ListShare.java b/Lambdas/Lists/ListShare/src/ListShare.java index 151d842..9e4ea56 100644 --- a/Lambdas/Lists/ListShare/src/ListShare.java +++ b/Lambdas/Lists/ListShare/src/ListShare.java @@ -1,16 +1,57 @@ -package com.example.listify.data; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; public class ListShare { Integer listID; String shareWithEmail; + Integer permissionLevel; + Integer uiPosition; ArrayList other; - public ListShare(ResultSet listRow) throws SQLException { + public ListShare(ResultSet listRow, String shareWithEmail) throws SQLException { this.listID = listRow.getInt("listID"); - this.shareWithEmail = listRow.getString("userID"); + this.shareWithEmail = shareWithEmail; + this.permissionLevel = listRow.getInt("permissionLevel"); + this.uiPosition = listRow.getInt("uiPosition"); other = new ArrayList<>(); } + @Override + public String toString() { + return "ListShare{" + + "listID=" + listID + + ", shareWithEmail='" + shareWithEmail + '\'' + + ", permissionLevel=" + permissionLevel + + ", uiPosition=" + uiPosition + + ", other=" + other + + '}'; + } + + public Integer getPermissionLevel() { + return permissionLevel; + } + + public void setPermissionLevel(Integer permissionLevel) { + this.permissionLevel = permissionLevel; + } + + public Integer getUiPosition() { + return uiPosition; + } + + public void setUiPosition(Integer uiPosition) { + this.uiPosition = uiPosition; + } + + public ArrayList getOther() { + return other; + } + + public void setOther(ArrayList other) { + this.other = other; + } + public Integer getListID() { return listID; } diff --git a/Lambdas/Lists/ListShare/src/ListShareDeleter.java b/Lambdas/Lists/ListShare/src/ListShareDeleter.java deleted file mode 100644 index 7ec0076..0000000 --- a/Lambdas/Lists/ListShare/src/ListShareDeleter.java +++ /dev/null @@ -1,65 +0,0 @@ -import java.security.AccessControlException; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.HashMap; -import java.util.Map; - -public class ListShareDeleter implements CallHandler { - private final Connection connection; - private final String cognitoID; - - private final String GET_LIST_ACCESS = "SELECT * FROM List WHERE (owner = ? AND listID = ?);"; - private final String REMOVE_SHAREE = "DELETE FROM ListSharee WHERE listID = ? AND user = ?;"; - - public ListShareDeleter(Connection connection, String cognitoID) { - this.connection = connection; - this.cognitoID = cognitoID; - } - - @Override - public Object conductAction(Map bodyMap, HashMap queryMap, String cognitoID) throws SQLException { - Integer listID = Integer.parseInt(queryMap.get("id")); - - InvokeRequest invokeRequest = new InvokeRequest(); - invokeRequest.setFunctionName("UserGET"); - invokeRequest.setPayload("{" + - " \"body\": {" + - " \"emailToCheck\": \"" + bodyMap.get("shareWithEmail").toString() + "\"" + - " }," + - " \"params\": {" + - " \"querystring\": {" + - " }" + - " }," + - " \"context\": {" + - " \"sub\": \"not used\"" + - " }" + - "}"); - InvokeResult invokeResult = AWSLambdaClientBuilder.defaultClient().invoke(invokeRequest); - - String shareeID = new String(invokeResult.getPayload().array()).replace("\"", ""); - - //Ensure that the user who is unsharing a list is the owner of that list - PreparedStatement accessCheck = connection.prepareStatement(GET_LIST_ACCESS); - accessCheck.setString(1, cognitoID); - accessCheck.setInt(2, listID); - - ResultSet userLists = accessCheck.executeQuery(); - - //User does not own the list; unshare attempt fails - if (!userLists.next()) { - throw new AccessControlException("User does not have access to list"); - } - - //Unshare the list with the specified sharee - PreparedStatement unshareList = connection.prepareStatement(REMOVE_SHAREE); - unshareList.setInt(1, listID); - unshareList.setInt(2, shareeID); - - cleanAccess.executeUpdate(); - connection.commit(); - - return null; - } -} diff --git a/Lambdas/Lists/ListShare/src/ListShareGetter.java b/Lambdas/Lists/ListShare/src/ListShareGetter.java index 009f0d6..03d6e8d 100644 --- a/Lambdas/Lists/ListShare/src/ListShareGetter.java +++ b/Lambdas/Lists/ListShare/src/ListShareGetter.java @@ -1,9 +1,14 @@ +import com.amazonaws.services.lambda.AWSLambdaClientBuilder; +import com.amazonaws.services.lambda.model.InvokeRequest; +import com.amazonaws.services.lambda.model.InvokeResult; +import com.google.gson.Gson; + import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.ArrayList; import java.util.HashMap; +import java.util.InputMismatchException; import java.util.Map; public class ListShareGetter implements CallHandler{ @@ -21,18 +26,65 @@ public class ListShareGetter implements CallHandler{ public Object conductAction(Map bodyMap, HashMap queryMap, String cognitoID) throws SQLException { Integer listID = Integer.parseInt(queryMap.get("id")); - PreparedStatement getList = connection.prepareStatement(GET_LIST); + PreparedStatement getList = connection.prepareStatement(GET_LISTS); getList.setInt(1, listID); ResultSet getListResults = getList.executeQuery(); - getListResults.first(); + System.out.println(getListResults); - //ListShare object to hold the data values of the first row retrived - ListShare first = new ListShare(getListResults); + ListShare first = null; + while (first == null && getListResults.next()) { + InvokeRequest invokeRequest = new InvokeRequest(); + invokeRequest.setFunctionName("UserGET"); + invokeRequest.setPayload("{" + + " \"body\": {" + + " }," + + " \"params\": {" + + " \"querystring\": {" + + " \"id\": \"" + getListResults.getString("userID") + "\"" + + " }" + + " }," + + " \"context\": {" + + " \"sub\": \"not used\"" + + " }" + + "}"); + InvokeResult invokeResult = AWSLambdaClientBuilder.defaultClient().invoke(invokeRequest); + if (invokeResult.getStatusCode() != 200) { + throw new InputMismatchException("Could not find specified user to share with"); + } + String shareWithEmail = new Gson().fromJson(new String(invokeResult.getPayload().array()), User.class).email; + first = new ListShare(getListResults, shareWithEmail); + if (first.permissionLevel == 0 || first.permissionLevel == 1) { + first = null; + } + } //Insert the ListShare objects to hold the data of the remaining rows into first's ListShare list while (getListResults.next()) { - first.addtoList(new ListShare(getListResults)); + InvokeRequest invokeRequest = new InvokeRequest(); + invokeRequest.setFunctionName("UserGET"); + invokeRequest.setPayload("{" + + " \"body\": {" + + " }," + + " \"params\": {" + + " \"querystring\": {" + + " \"id\": \"" + getListResults.getString("userID") + "\"" + + " }" + + " }," + + " \"context\": {" + + " \"sub\": \"not used\"" + + " }" + + "}"); + InvokeResult invokeResult = AWSLambdaClientBuilder.defaultClient().invoke(invokeRequest); + if (invokeResult.getStatusCode() != 200) { + throw new InputMismatchException("Could not find specified user to share with"); + } + String shareWithEmail = new Gson().fromJson(new String(invokeResult.getPayload().array()), User.class).email; + ListShare newShare = new ListShare(getListResults, shareWithEmail); + System.out.println(newShare); + if (newShare.permissionLevel != 0 && newShare.permissionLevel != 1) { + first.addtoList(newShare); + } } return first; diff --git a/Lambdas/Lists/ListShare/src/ListSharer.java b/Lambdas/Lists/ListShare/src/ListSharer.java index a80d0d8..53359d2 100644 --- a/Lambdas/Lists/ListShare/src/ListSharer.java +++ b/Lambdas/Lists/ListShare/src/ListSharer.java @@ -1,6 +1,7 @@ import com.amazonaws.services.lambda.AWSLambdaClientBuilder; import com.amazonaws.services.lambda.model.InvokeRequest; import com.amazonaws.services.lambda.model.InvokeResult; +import com.google.gson.Gson; import java.security.AccessControlException; import java.sql.Connection; @@ -56,12 +57,7 @@ public class ListSharer implements CallHandler { if (invokeResult.getStatusCode() != 200) { throw new InputMismatchException("Could not find specified user to share with"); } - String shareWithSub = new String(invokeResult.getPayload().array()).replace("\"", ""); -// checkAccess.setString(2, shareWithSub); -// checkAccessRS = checkAccess.executeQuery(); -// if (checkAccessRS.next()) { -// throw new InputMismatchException("The specified user already has access"); -// } + String shareWithSub = new Gson().fromJson(new String(invokeResult.getPayload().array()), User.class).cognitoID; PreparedStatement uiPositionCheck = connection.prepareStatement(UI_POSITION_CHECK); uiPositionCheck.setString(1, shareWithSub); diff --git a/Lambdas/Lists/ListShare/test/TestListSharer.java b/Lambdas/Lists/ListShare/test/TestListSharer.java index 77ce5be..f86d056 100644 --- a/Lambdas/Lists/ListShare/test/TestListSharer.java +++ b/Lambdas/Lists/ListShare/test/TestListSharer.java @@ -1,4 +1,6 @@ import org.junit.Test; +import org.mockito.Mockito; +import org.mockito.Mockito.*; import java.security.AccessControlException; import java.sql.SQLException; @@ -9,15 +11,15 @@ public class TestListSharer { @Test public void testListSharerWOAccess() { - testListEntryAdderCore(false, false); + testListEntryAdderCoreMock(false, false); } @Test public void testListSharerError() { - testListEntryAdderCore(true, true); + testListEntryAdderCoreMock(true, true); } - public void testListEntryAdderCore(boolean shouldThrow, boolean hasAccess) { + public void testListEntryAdderCoreMock(boolean shouldThrow, boolean hasAccess) { StatementInjector injector; try { injector = new StatementInjector(null, null, shouldThrow); @@ -26,7 +28,7 @@ public class TestListSharer { assert false; //Error in test infrastructure return; } - ListSharer listSharer = new ListSharer(injector, "cognitoID"); + ListSharer listSharer = Mockito.spy(new ListSharer(injector, "cognitoID")); Map ignore = new HashMap<>(); Map body = TestInputUtils.addBody(ignore); body.put("listID", 49); diff --git a/Lambdas/Lists/Picture/src/Picture.java b/Lambdas/Lists/Picture/src/Picture.java new file mode 100644 index 0000000..f4e5fae --- /dev/null +++ b/Lambdas/Lists/Picture/src/Picture.java @@ -0,0 +1,34 @@ +import java.sql.ResultSet; +import java.sql.SQLException; + +public class Picture { + String base64EncodedImage; + + public Picture(ResultSet rs) { + try { + this.base64EncodedImage = rs.getString("base64image"); + } catch (SQLException throwables) { + throwables.printStackTrace(); + this.base64EncodedImage = null; + } + } + + public Picture(String base64EncodedImage) { + this.base64EncodedImage = base64EncodedImage; + } + + @Override + public String toString() { + return "Picture{" + + "base64EncodedImage='" + base64EncodedImage + '\'' + + '}'; + } + + public String getBase64EncodedImage() { + return base64EncodedImage; + } + + public void setBase64EncodedImage(String base64EncodedImage) { + this.base64EncodedImage = base64EncodedImage; + } +} diff --git a/Lambdas/Lists/Picture/src/PictureGET.java b/Lambdas/Lists/Picture/src/PictureGET.java new file mode 100644 index 0000000..e5787dd --- /dev/null +++ b/Lambdas/Lists/Picture/src/PictureGET.java @@ -0,0 +1,12 @@ +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; + +import java.util.Map; + +public class PictureGET implements RequestHandler, Object> { + + public Object handleRequest(Map inputMap, Context unfilled) { + return BasicHandler.handleRequest(inputMap, unfilled, PictureGetter.class); + } + +} \ No newline at end of file diff --git a/Lambdas/Lists/Picture/src/PictureGetter.java b/Lambdas/Lists/Picture/src/PictureGetter.java new file mode 100644 index 0000000..79da9e2 --- /dev/null +++ b/Lambdas/Lists/Picture/src/PictureGetter.java @@ -0,0 +1,38 @@ +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +public class PictureGetter implements CallHandler { + private final Connection connection; + private final String cognitoID; + + private final String GET_ITEM = "SELECT * FROM Pictures WHERE cognitoID = ?;"; + + public PictureGetter(Connection connection, String cognitoID) { + this.connection = connection; + this.cognitoID = cognitoID; + } + + @Override + public Object conductAction(Map bodyMap, HashMap queryMap, String cognitoID) throws SQLException { + PreparedStatement statement = connection.prepareStatement(GET_ITEM); + if (!queryMap.containsKey("id")) { + throw new IllegalArgumentException("Must have id set."); + } + if (queryMap.get("id").equals("profile")) { + statement.setString(1, cognitoID); + } else { + statement.setString(1, queryMap.get("id")); + } + System.out.println(statement); + ResultSet queryResults = statement.executeQuery(); + queryResults.first(); + System.out.println(queryResults); + Picture retrievedPicture = new Picture(queryResults); +// System.out.println(retrievedPicture); + return retrievedPicture; + } +} \ No newline at end of file diff --git a/Lambdas/Lists/ListShare/src/ListShareDELETE.java b/Lambdas/Lists/Picture/src/PicturePUT.java similarity index 57% rename from Lambdas/Lists/ListShare/src/ListShareDELETE.java rename to Lambdas/Lists/Picture/src/PicturePUT.java index 784a725..43fc914 100644 --- a/Lambdas/Lists/ListShare/src/ListShareDELETE.java +++ b/Lambdas/Lists/Picture/src/PicturePUT.java @@ -3,10 +3,9 @@ import com.amazonaws.services.lambda.runtime.RequestHandler; import java.util.Map; -public class ListShareDELETE implements RequestHandler, Object> { +public class PicturePUT implements RequestHandler, Object> { public Object handleRequest(Map inputMap, Context unfilled) { - return BasicHandler.handleRequest(inputMap, unfilled, ListShareDeleter.class); + return BasicHandler.handleRequest(inputMap, unfilled, PicturePutter.class); } - } diff --git a/Lambdas/Lists/Picture/src/PicturePutter.java b/Lambdas/Lists/Picture/src/PicturePutter.java new file mode 100644 index 0000000..3f14a87 --- /dev/null +++ b/Lambdas/Lists/Picture/src/PicturePutter.java @@ -0,0 +1,31 @@ +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +public class PicturePutter implements CallHandler { + + private Connection connection; + private String cognitoID; + + public PicturePutter(Connection connection, String cognitoID) { + this.connection = connection; + this.cognitoID = cognitoID; + } + + final private String STORE_PICTURE_SQL = "REPLACE INTO Pictures(cognitoID, base64image) VALUES(?, ?);"; + + public Object conductAction(Map bodyMap, HashMap queryString, String cognitoID) throws SQLException { + PreparedStatement storePicture = connection.prepareStatement(STORE_PICTURE_SQL); + if(!bodyMap.containsKey("base64EncodedImage")) { + throw new IllegalArgumentException("Base64EncodedImage not found"); + } + storePicture.setString(1, cognitoID); + storePicture.setString(2, bodyMap.get("base64EncodedImage").toString()); + System.out.println(storePicture); + storePicture.executeUpdate(); + connection.commit(); + return null; + } +} \ No newline at end of file diff --git a/Lambdas/Lists/Picture/test/TestPictureGetter.java b/Lambdas/Lists/Picture/test/TestPictureGetter.java new file mode 100644 index 0000000..2af43e3 --- /dev/null +++ b/Lambdas/Lists/Picture/test/TestPictureGetter.java @@ -0,0 +1,43 @@ +import org.junit.Test; +import org.mockito.Mockito; + +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +public class TestPictureGetter { + + @Test + public void testPictureGetterValid() { + testPictureGetter(false); + } + + @Test + public void testPictureGetterError() { + testPictureGetter(true); + } + + public void testPictureGetter(boolean shouldThrow) { + StatementInjector injector; + try { + injector = new StatementInjector(null, null, shouldThrow); + } catch (SQLException throwables) { + throwables.printStackTrace(); + assert false; //Error in test infrastructure + return; + } + PictureGetter pictureGetter = Mockito.spy(new PictureGetter(injector, "cognitoID")); + Map ignore = new HashMap<>(); + Map body = TestInputUtils.addBody(ignore); + body.put("id", 1); + body.put("profile", 1); + + try { + Object rawIDReturn = pictureGetter.conductAction(body, TestInputUtils.addQueryParams(ignore), "cognitoID"); + assert (rawIDReturn != null); + } catch (SQLException throwables) { + assert shouldThrow; + throwables.printStackTrace(); + } + } +} diff --git a/Lambdas/Lists/Picture/test/TestPicturePutter.java b/Lambdas/Lists/Picture/test/TestPicturePutter.java new file mode 100644 index 0000000..fc30141 --- /dev/null +++ b/Lambdas/Lists/Picture/test/TestPicturePutter.java @@ -0,0 +1,40 @@ +import org.junit.Test; +import org.mockito.Mockito; + +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +public class TestPicturePutter { + + @Test + public void testPicturePutterValid() { + testPicturePutter(false); + } + + @Test + public void testPicturePutterError() { + testPicturePutter(true); + } + + public void testPicturePutter(boolean shouldThrow) { + StatementInjector injector; + try { + injector = new StatementInjector(null, null, shouldThrow); + } catch (SQLException throwables) { + throwables.printStackTrace(); + assert false; //Error in test infrastructure + return; + } + PicturePutter picturePutter = Mockito.spy(new PicturePutter(injector, "cognitoID")); + Map ignore = new HashMap<>(); + Map body = TestInputUtils.addBody(ignore); + try { + Object rawIDReturn = picturePutter.conductAction(body, TestInputUtils.addQueryParams(ignore), "cognitoID"); + assert (rawIDReturn == null); + } catch (SQLException throwables) { + assert shouldThrow; + throwables.printStackTrace(); + } + } +} diff --git a/Lambdas/Lists/SearchHistory/test/TestSearchHistoryGetter.java b/Lambdas/Lists/SearchHistory/test/TestSearchHistoryGetter.java new file mode 100644 index 0000000..20e0cea --- /dev/null +++ b/Lambdas/Lists/SearchHistory/test/TestSearchHistoryGetter.java @@ -0,0 +1,42 @@ +import org.junit.Test; +import org.mockito.Mockito; + +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + + +public class TestSearchHistoryGetter { + + @Test + public void testSearchHistoryValid() { + testSearchHistoryGetterMock(false); + } + + @Test + public void testSearchHistoryError() { + testSearchHistoryGetterMock(true); + } + + public void testSearchHistoryGetterMock(boolean shouldThrow) { + StatementInjector injector; + try { + injector = new StatementInjector(null, null, shouldThrow); + } catch (SQLException throwables) { + throwables.printStackTrace(); + assert false; //Error in test infrastructure + return; + } + SearchHistoryGetter searchHistoryGetter = Mockito.spy(new SearchHistoryGetter(injector, "cognitoID")); + Map ignore = new HashMap<>(); + Map body = TestInputUtils.addBody(ignore); + + try { + Object rawIDReturn = searchHistoryGetter.conductAction(body, TestInputUtils.addQueryParams(ignore), "cognitoID"); + assert (rawIDReturn != null); + } catch (SQLException throwables) { + assert shouldThrow; + throwables.printStackTrace(); + } + } +} diff --git a/Lambdas/Lists/SearchHistory/test/TestSearchHistoryUpdater.java b/Lambdas/Lists/SearchHistory/test/TestSearchHistoryUpdater.java new file mode 100644 index 0000000..99e9d8c --- /dev/null +++ b/Lambdas/Lists/SearchHistory/test/TestSearchHistoryUpdater.java @@ -0,0 +1,41 @@ +import org.junit.Test; +import org.mockito.Mockito; + +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +public class TestSearchHistoryUpdater { + + @Test + public void testSearchHistoryUpdaterValid() { + testSearchHistoryUpdater(false); + } + + @Test + public void testSearchHistoryUpdaterError() { + testSearchHistoryUpdater(true); + } + + public void testSearchHistoryUpdater(boolean shouldThrow) { + StatementInjector injector; + try { + injector = new StatementInjector(null, null, shouldThrow); + } catch (SQLException throwables) { + throwables.printStackTrace(); + assert false; //Error in test infrastructure + return; + } + SearchHistoryUpdater testSearchHistoryUpdater = Mockito.spy(new SearchHistoryUpdater(injector, "cognitoID")); + Map ignore = new HashMap<>(); + Map body = TestInputUtils.addBody(ignore); + + try { + Object rawIDReturn = testSearchHistoryUpdater.conductAction(body, TestInputUtils.addQueryParams(ignore), "cognitoID"); + assert (rawIDReturn == null); + } catch (SQLException throwables) { + assert shouldThrow; + throwables.printStackTrace(); + } + } +} diff --git a/Lambdas/Lists/User/src/User.java b/Lambdas/Lists/User/src/User.java new file mode 100644 index 0000000..e3ceb43 --- /dev/null +++ b/Lambdas/Lists/User/src/User.java @@ -0,0 +1,26 @@ + +public class User { + String cognitoID; + String email; + + public User(String cognitoID, String email) { + this.cognitoID = cognitoID; + this.email = email; + } + + public String getCognitoID() { + return cognitoID; + } + + public void setCognitoID(String cognitoID) { + this.cognitoID = cognitoID; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } +} diff --git a/Lambdas/Lists/User/src/UserGetter.java b/Lambdas/Lists/User/src/UserGetter.java index 42d7422..534f88b 100644 --- a/Lambdas/Lists/User/src/UserGetter.java +++ b/Lambdas/Lists/User/src/UserGetter.java @@ -28,21 +28,23 @@ public class UserGetter implements CallHandler { System.out.println(userPoolId); ListUsersRequest checkRequest = new ListUsersRequest().withUserPoolId(userPoolId); Object emailObject = bodyMap.get("emailToCheck"); + if (queryMap.get("id").contains("@")) { + emailObject = queryMap.get("id"); + } String attributeToGet = "sub"; if (emailObject != null) { checkRequest.setFilter("email=\"" + emailObject.toString() +"\""); } else { try { String id = queryMap.get("id"); + attributeToGet = "email"; + checkRequest.setFilter("sub=\"" + cognitoID + "\""); if ((id != null) && (!id.equals(""))) { - attributeToGet = "email"; - checkRequest.setFilter("sub=\"" + cognitoID + "\""); - } else { - return cognitoID; + checkRequest.setFilter("sub=\"" + id + "\""); } } catch (Exception e) { System.out.println(e); - return cognitoID; + return new User(cognitoID, null); } } System.out.println(checkRequest); @@ -52,9 +54,9 @@ public class UserGetter implements CallHandler { if (foundUsers.size() != 1) { System.out.println(foundUsers); if (foundUsers.size() == 0) { - throw new InputMismatchException("Not user with given email"); + throw new InputMismatchException("No user with given attribute when searching for (" + attributeToGet + ")"); } - throw new InputMismatchException("Found more than one user with supposedly unique email"); + throw new InputMismatchException("Found more than one user with supposedly unique attribute (" + attributeToGet + ")"); } UserType foundUser = foundUsers.get(0); System.out.println(foundUser.getAttributes()); @@ -66,6 +68,11 @@ public class UserGetter implements CallHandler { } System.out.println(attribute.getName() + ": " + attribute.getValue()); } - return attributeToReturn; + if (attributeToGet.equals("email")) { + return new User(cognitoID, attributeToReturn); + } else if (attributeToGet.equals("sub")) { + return new User(attributeToReturn, emailObject.toString()); + } + return null; } } diff --git a/Lambdas/Lists/pom.xml b/Lambdas/Lists/pom.xml index 4725165..78e21c3 100644 --- a/Lambdas/Lists/pom.xml +++ b/Lambdas/Lists/pom.xml @@ -110,6 +110,11 @@ 4.13 test + + org.mockito + mockito-core + 3.2.4 + 1.11 diff --git a/Listify/.idea/gradle.xml b/Listify/.idea/gradle.xml index 674414f..c2e3d64 100644 --- a/Listify/.idea/gradle.xml +++ b/Listify/.idea/gradle.xml @@ -8,6 +8,8 @@