|
6 | 6 | import org.junit.BeforeClass; |
7 | 7 | import org.junit.Test; |
8 | 8 | import com.jayway.restassured.path.json.JsonPath; |
| 9 | +import edu.harvard.iq.dataverse.settings.SettingsServiceBean; |
9 | 10 | import edu.harvard.iq.dataverse.util.BundleUtil; |
10 | 11 | import static java.lang.Thread.sleep; |
11 | 12 | import java.util.Arrays; |
@@ -846,6 +847,121 @@ public void testAddTinyFile() { |
846 | 847 | .statusCode(OK.getStatusCode()); |
847 | 848 | } |
848 | 849 |
|
| 850 | + @Test |
| 851 | + public void testRestrictFile() { |
| 852 | + msgt("testRestrictFile"); |
| 853 | + |
| 854 | + //get publicInstall setting so we can change it back |
| 855 | + Response publicInstallResponse = UtilIT.getSetting(SettingsServiceBean.Key.PublicInstall); |
| 856 | + //TODO: fix this its a little hacky |
| 857 | + String publicInstall = String.valueOf(publicInstallResponse.getBody().asString().contains("true")); |
| 858 | + |
| 859 | + // make sure this is not a public installation |
| 860 | + UtilIT.setSetting(SettingsServiceBean.Key.PublicInstall, "false"); |
| 861 | + |
| 862 | + // Set restrict to true |
| 863 | + boolean restrict = true; |
| 864 | + // Create user |
| 865 | + String apiToken = createUserGetToken(); |
| 866 | + |
| 867 | + // Create Dataverse |
| 868 | + String dataverseAlias = createDataverseGetAlias(apiToken); |
| 869 | + |
| 870 | + // Create Dataset |
| 871 | + Integer datasetId = createDatasetGetId(dataverseAlias, apiToken); |
| 872 | + |
| 873 | + // ------------------------- |
| 874 | + // Add initial file |
| 875 | + // ------------------------- |
| 876 | + msg("Add initial file"); |
| 877 | + String pathToFile = "src/main/webapp/resources/images/favicondataverse.png"; |
| 878 | + Response addResponse = UtilIT.uploadFileViaNative(datasetId.toString(), pathToFile, apiToken); |
| 879 | + |
| 880 | + String successMsgAdd = ResourceBundle.getBundle("Bundle").getString("file.addreplace.success.add"); |
| 881 | + |
| 882 | + addResponse.then().assertThat() |
| 883 | + .body("data.files[0].dataFile.contentType", equalTo("image/png")) |
| 884 | + .body("data.files[0].label", equalTo("favicondataverse.png")) |
| 885 | + .statusCode(OK.getStatusCode()); |
| 886 | + |
| 887 | + long origFileId = JsonPath.from(addResponse.body().asString()).getLong("data.files[0].dataFile.id"); |
| 888 | + |
| 889 | + msg("Orig file id: " + origFileId); |
| 890 | + assertNotNull(origFileId); // If checkOut fails, display message |
| 891 | + |
| 892 | + //restrict file good |
| 893 | + Response restrictResponse = UtilIT.restrictFile(origFileId, restrict, apiToken); |
| 894 | + |
| 895 | + restrictResponse.then().assertThat() |
| 896 | + .statusCode(OK.getStatusCode()); |
| 897 | + |
| 898 | + //restrict already restricted file bad |
| 899 | + Response restrictResponseBad = UtilIT.restrictFile(origFileId, restrict, apiToken); |
| 900 | + |
| 901 | + restrictResponseBad.then().assertThat() |
| 902 | + .statusCode(BAD_REQUEST.getStatusCode()); |
| 903 | + |
| 904 | + //unrestrict file |
| 905 | + restrict = false; |
| 906 | + Response unrestrictResponse = UtilIT.restrictFile(origFileId, restrict, apiToken); |
| 907 | + |
| 908 | + unrestrictResponse.then().assertThat() |
| 909 | + .statusCode(OK.getStatusCode()); |
| 910 | + |
| 911 | + //reset public install |
| 912 | + UtilIT.setSetting(SettingsServiceBean.Key.PublicInstall, publicInstall); |
| 913 | + |
| 914 | + |
| 915 | + } |
| 916 | + |
| 917 | + @Test |
| 918 | + public void testRestrictAddedFile() { |
| 919 | + msgt("testRestrictAddedFile"); |
| 920 | + |
| 921 | + //get publicInstall setting so we can change it back |
| 922 | + Response publicInstallResponse = UtilIT.getSetting(SettingsServiceBean.Key.PublicInstall); |
| 923 | + String publicInstall = String.valueOf(publicInstallResponse.getBody().asString().contains("true")); |
| 924 | + |
| 925 | + // make sure this is not a public installation |
| 926 | + UtilIT.setSetting(SettingsServiceBean.Key.PublicInstall, "false"); |
| 927 | + |
| 928 | + // Create user |
| 929 | + String apiToken = createUserGetToken(); |
| 930 | + |
| 931 | + // Create Dataverse |
| 932 | + String dataverseAlias = createDataverseGetAlias(apiToken); |
| 933 | + |
| 934 | + // Create Dataset |
| 935 | + Integer datasetId = createDatasetGetId(dataverseAlias, apiToken); |
| 936 | + |
| 937 | + // ------------------------- |
| 938 | + // Add initial file |
| 939 | + // ------------------------- |
| 940 | + msg("Add initial file"); |
| 941 | + String pathToFile = "src/main/webapp/resources/images/favicondataverse.png"; |
| 942 | + |
| 943 | + JsonObjectBuilder json = Json.createObjectBuilder() |
| 944 | + .add("description", "my description") |
| 945 | + .add("categories", Json.createArrayBuilder() |
| 946 | + .add("Data") |
| 947 | + ) |
| 948 | + .add("restrict", "true"); |
| 949 | + |
| 950 | + Response addResponse = UtilIT.uploadFileViaNative(datasetId.toString(), pathToFile, json.build(), apiToken); |
| 951 | + |
| 952 | + //addResponse.prettyPrint(); |
| 953 | + msgt("Here it is: " + addResponse.prettyPrint()); |
| 954 | + String successMsg = ResourceBundle.getBundle("Bundle").getString("file.addreplace.success.add"); |
| 955 | + |
| 956 | + addResponse.then().assertThat() |
| 957 | + .body("data.files[0].dataFile.contentType", equalTo("image/png")) |
| 958 | + .body("data.files[0].label", equalTo("favicondataverse.png")) |
| 959 | + .statusCode(OK.getStatusCode()); |
| 960 | + |
| 961 | + //reset public install |
| 962 | + UtilIT.setSetting(SettingsServiceBean.Key.PublicInstall, publicInstall); |
| 963 | + |
| 964 | + } |
849 | 965 |
|
850 | 966 | private void msg(String m){ |
851 | 967 | System.out.println(m); |
|
0 commit comments