From 98bcadd699a6e1f6105c193bee2bd28ed2ea7f7f Mon Sep 17 00:00:00 2001 From: Ray Lee Date: Wed, 20 Jul 2016 10:59:20 -0700 Subject: [PATCH] DRYD-22: Add XmlReplay tests. --- .../IntegrationTests/xmlreplay/XmlReplay.java | 21 +++- .../xmlreplay/XmlReplayTransport.java | 16 ++- .../test-data/xmlreplay/security-oauth.xml | 104 ++++++++++++++++++ .../security-oauth/password-grant-admin.txt | 1 + .../password-grant-bad-password.txt | 1 + .../security-oauth/refresh-token-grant.txt | 1 + .../test-data/xmlreplay/xml-replay-master.xml | 1 + 7 files changed, 139 insertions(+), 6 deletions(-) create mode 100644 services/IntegrationTests/src/test/resources/test-data/xmlreplay/security-oauth.xml create mode 100644 services/IntegrationTests/src/test/resources/test-data/xmlreplay/security-oauth/password-grant-admin.txt create mode 100644 services/IntegrationTests/src/test/resources/test-data/xmlreplay/security-oauth/password-grant-bad-password.txt create mode 100644 services/IntegrationTests/src/test/resources/test-data/xmlreplay/security-oauth/refresh-token-grant.txt diff --git a/services/IntegrationTests/src/main/java/org/collectionspace/services/IntegrationTests/xmlreplay/XmlReplay.java b/services/IntegrationTests/src/main/java/org/collectionspace/services/IntegrationTests/xmlreplay/XmlReplay.java index 7c042acb2..2a58de2cc 100644 --- a/services/IntegrationTests/src/main/java/org/collectionspace/services/IntegrationTests/xmlreplay/XmlReplay.java +++ b/services/IntegrationTests/src/main/java/org/collectionspace/services/IntegrationTests/xmlreplay/XmlReplay.java @@ -566,11 +566,28 @@ public class XmlReplay { String testID = testNode.valueOf("@ID"); String testIDLabel = Tools.notEmpty(testID) ? (testGroupID+'.'+testID) : (testGroupID+'.'+testElementIndex); String method = testNode.valueOf("method"); + String contentType = testNode.valueOf("contentType"); String uri = testNode.valueOf("uri"); String fullURL = Tools.glue(protoHostPort, "/", uri); + if (contentType == null || contentType.equals("")) { + contentType = XmlReplayTransport.APPLICATION_XML; + } + + String currentAuthForTest = null; String authIDForTest = testNode.valueOf("@auth"); - String currentAuthForTest = authsMap.map.get(authIDForTest); + + if (Tools.notEmpty(authIDForTest)){ + currentAuthForTest = authsMap.map.get(authIDForTest); + } + else { + String tokenAuthExpression = testNode.valueOf("@tokenauth"); + + if (Tools.notEmpty(tokenAuthExpression)){ + currentAuthForTest = "Bearer " + evalStruct.eval(tokenAuthExpression, serviceResultsMap, null, jexl, jc); + } + } + if (Tools.notEmpty(currentAuthForTest)){ authForTest = currentAuthForTest; //else just run with current from last loop; } @@ -619,7 +636,7 @@ public class XmlReplay { if (parts.varsList.size()>0){ vars = parts.varsList.get(0); } - serviceResult = XmlReplayTransport.doPOST_PUTFromXML(parts.responseFilename, vars, protoHostPort, uri, method, XmlReplayTransport.APPLICATION_XML, evalStruct, authForTest, testIDLabel); + serviceResult = XmlReplayTransport.doPOST_PUTFromXML(parts.responseFilename, vars, protoHostPort, uri, method, contentType, evalStruct, authForTest, testIDLabel); if (vars!=null) { serviceResult.addVars(vars); } diff --git a/services/IntegrationTests/src/main/java/org/collectionspace/services/IntegrationTests/xmlreplay/XmlReplayTransport.java b/services/IntegrationTests/src/main/java/org/collectionspace/services/IntegrationTests/xmlreplay/XmlReplayTransport.java index 0cf0cd4d3..efd9cbed4 100644 --- a/services/IntegrationTests/src/main/java/org/collectionspace/services/IntegrationTests/xmlreplay/XmlReplayTransport.java +++ b/services/IntegrationTests/src/main/java/org/collectionspace/services/IntegrationTests/xmlreplay/XmlReplayTransport.java @@ -51,6 +51,14 @@ public class XmlReplayTransport { private static String DD = "--"; private static String CRLF = "\r\n"; + private static String formatAuth(String authForTest) { + if (authForTest.startsWith("Bearer ")) { + return authForTest; + } + + return ("Basic " + authForTest); + } + public static ServiceResult doGET(String urlString, String authForTest, String fromTestID) throws Exception { ServiceResult pr = new ServiceResult(); pr.fromTestID = fromTestID; @@ -64,7 +72,7 @@ public class XmlReplayTransport { GetMethod getMethod = new GetMethod(urlString); getMethod.addRequestHeader("Accept", "multipart/mixed"); getMethod.addRequestHeader("Accept", "application/xml"); - getMethod.setRequestHeader("Authorization", "Basic " + authForTest); //"dGVzdDp0ZXN0"); + getMethod.setRequestHeader("Authorization", formatAuth(authForTest)); //"dGVzdDp0ZXN0"); getMethod.setRequestHeader("X-XmlReplay-fromTestID", fromTestID); try { int statusCode1 = client.executeMethod(getMethod); @@ -101,7 +109,7 @@ public class XmlReplayTransport { DeleteMethod deleteMethod = new DeleteMethod(urlString); deleteMethod.setRequestHeader("Accept", "multipart/mixed"); deleteMethod.addRequestHeader("Accept", "application/xml"); - deleteMethod.setRequestHeader("Authorization", "Basic " + authForTest); + deleteMethod.setRequestHeader("Authorization", formatAuth(authForTest)); deleteMethod.setRequestHeader("X-XmlReplay-fromTestID", fromTestID); int statusCode1 = 0; String res = ""; @@ -222,7 +230,7 @@ public class XmlReplayTransport { conn.setRequestProperty("Accept", "application/xml"); conn.setRequestProperty("content-type", contentType); } - conn.setRequestProperty("Authorization", "Basic " + authForTest); //TODO: remove test user : hard-coded as "dGVzdDp0ZXN0" + conn.setRequestProperty("Authorization", formatAuth(authForTest)); //TODO: remove test user : hard-coded as "dGVzdDp0ZXN0" conn.setRequestProperty("Connection", "close"); conn.setRequestProperty("X-XmlReplay-fromTestID", fromTestID); conn.setDoOutput(true); @@ -278,7 +286,7 @@ public class XmlReplayTransport { PostMethod postMethod = new PostMethod(urlString); postMethod.setRequestHeader("Accept", "multipart/mixed"); postMethod.addRequestHeader("Accept", "application/xml"); - postMethod.setRequestHeader("Authorization", "Basic " + authForTest); + postMethod.setRequestHeader("Authorization", formatAuth(authForTest)); postMethod.setRequestHeader("X-XmlReplay-fromTestID", fromTestID); //this method takes an array of params. Not sure what they expect us to do with a raw post: // postMethod.setRequestBody(); diff --git a/services/IntegrationTests/src/test/resources/test-data/xmlreplay/security-oauth.xml b/services/IntegrationTests/src/test/resources/test-data/xmlreplay/security-oauth.xml new file mode 100644 index 000000000..f54773b72 --- /dev/null +++ b/services/IntegrationTests/src/test/resources/test-data/xmlreplay/security-oauth.xml @@ -0,0 +1,104 @@ + + + + + Y3NwYWNlLXVpOg== + w3NwaWNlLXVpOg== + + + + + + GET + /cspace-services/oauth/token + 405 + + + PUT + application/x-www-form-urlencoded + /cspace-services/oauth/token + security-oauth/password-grant-admin.txt + 405 + + + DELETE + /cspace-services/oauth/token + 405 + + + + POST + application/x-www-form-urlencoded + /cspace-services/oauth/token + security-oauth/password-grant-admin.txt + 401 + + + + POST + application/x-www-form-urlencoded + /cspace-services/oauth/token + security-oauth/password-grant-bad-password.txt + 400 + + + + POST + application/x-www-form-urlencoded + /cspace-services/oauth/token + security-oauth/password-grant-admin.txt + 200 + + + + + + + POST + application/x-www-form-urlencoded + /cspace-services/oauth/token + security-oauth/password-grant-admin.txt + 200 + + + + GET + /cspace-services/collectionobjects + 200 + + + + POST + /cspace-services/collectionobjects + collectionobject/co1.xml + 200,201 + + + + GET + /cspace-services/collectionobjects + 401 + + + + GET + /cspace-services/collectionobjects + 401 + + + + POST + application/x-www-form-urlencoded + /cspace-services/oauth/token + security-oauth/refresh-token-grant.txt + 200 + + + + POST + /cspace-services/collectionobjects + collectionobject/co1.xml + 200,201 + + + diff --git a/services/IntegrationTests/src/test/resources/test-data/xmlreplay/security-oauth/password-grant-admin.txt b/services/IntegrationTests/src/test/resources/test-data/xmlreplay/security-oauth/password-grant-admin.txt new file mode 100644 index 000000000..0bf30694e --- /dev/null +++ b/services/IntegrationTests/src/test/resources/test-data/xmlreplay/security-oauth/password-grant-admin.txt @@ -0,0 +1 @@ +grant_type=password&username=admin@core.collectionspace.org&password=Administrator \ No newline at end of file diff --git a/services/IntegrationTests/src/test/resources/test-data/xmlreplay/security-oauth/password-grant-bad-password.txt b/services/IntegrationTests/src/test/resources/test-data/xmlreplay/security-oauth/password-grant-bad-password.txt new file mode 100644 index 000000000..1a5022c1a --- /dev/null +++ b/services/IntegrationTests/src/test/resources/test-data/xmlreplay/security-oauth/password-grant-bad-password.txt @@ -0,0 +1 @@ +grant_type=password&username=admin@core.collectionspace.org&password=NotThePassword \ No newline at end of file diff --git a/services/IntegrationTests/src/test/resources/test-data/xmlreplay/security-oauth/refresh-token-grant.txt b/services/IntegrationTests/src/test/resources/test-data/xmlreplay/security-oauth/refresh-token-grant.txt new file mode 100644 index 000000000..0b951eadb --- /dev/null +++ b/services/IntegrationTests/src/test/resources/test-data/xmlreplay/security-oauth/refresh-token-grant.txt @@ -0,0 +1 @@ +grant_type=refresh_token&refresh_token=${tokenGrant.got("//refresh_token")} \ No newline at end of file diff --git a/services/IntegrationTests/src/test/resources/test-data/xmlreplay/xml-replay-master.xml b/services/IntegrationTests/src/test/resources/test-data/xmlreplay/xml-replay-master.xml index 26f4e3706..7ed8392e3 100644 --- a/services/IntegrationTests/src/test/resources/test-data/xmlreplay/xml-replay-master.xml +++ b/services/IntegrationTests/src/test/resources/test-data/xmlreplay/xml-replay-master.xml @@ -12,6 +12,7 @@ + -- 2.47.3