Search in sources :

Example 1 with UtilIT.getRandomString

use of edu.harvard.iq.dataverse.api.UtilIT.getRandomString in project dataverse by IQSS.

the class AdminIT method testFilterAuthenticatedUsers.

/**
 * Run multiple test against API endpoint to search authenticated users
 * @throws Exception
 */
@Test
public void testFilterAuthenticatedUsers() throws Exception {
    Response createUserResponse;
    // --------------------------------------------
    // Make 11 random users
    // --------------------------------------------
    String randUserNamePrefix = "r" + UtilIT.getRandomString(4) + "_";
    List<String> randomUsernames = new ArrayList<String>();
    for (int i = 0; i < 11; i++) {
        createUserResponse = UtilIT.createRandomUser(randUserNamePrefix);
        createUserResponse.then().assertThat().statusCode(OK.getStatusCode());
        String newUserName = UtilIT.getUsernameFromResponse(createUserResponse);
        randomUsernames.add(newUserName);
    }
    // --------------------------------------------
    // Create superuser
    // --------------------------------------------
    Response createSuperuser = UtilIT.createRandomUser();
    String superuserUsername = UtilIT.getUsernameFromResponse(createSuperuser);
    String superuserApiToken = UtilIT.getApiTokenFromResponse(createSuperuser);
    Response toggleSuperuser = UtilIT.makeSuperUser(superuserUsername);
    toggleSuperuser.then().assertThat().statusCode(OK.getStatusCode());
    // --------------------------------------------
    // Search for the 11 new users and verify results
    // --------------------------------------------
    Response filterReponse01 = UtilIT.filterAuthenticatedUsers(superuserApiToken, randUserNamePrefix, null, 100);
    filterReponse01.then().assertThat().statusCode(OK.getStatusCode());
    filterReponse01.prettyPrint();
    int numResults = 11;
    filterReponse01.then().assertThat().body("data.userCount", equalTo(numResults)).body("data.selectedPage", equalTo(1)).body("data.pagination.pageCount", equalTo(1)).body("data.pagination.numResults", equalTo(numResults));
    String userIdentifer;
    for (int i = 0; i < numResults; i++) {
        userIdentifer = JsonPath.from(filterReponse01.getBody().asString()).getString("data.users[" + i + "].userIdentifier");
        assertEquals(randomUsernames.contains(userIdentifer), true);
    }
    List<Object> userList1 = JsonPath.from(filterReponse01.body().asString()).getList("data.users");
    assertEquals(userList1.size(), numResults);
    // --------------------------------------------
    // Search for the 11 new users, but only return 5 per page
    // --------------------------------------------
    int numUsersReturned = 5;
    Response filterReponse02 = UtilIT.filterAuthenticatedUsers(superuserApiToken, randUserNamePrefix, 1, numUsersReturned);
    filterReponse02.then().assertThat().statusCode(OK.getStatusCode());
    filterReponse02.prettyPrint();
    filterReponse02.then().assertThat().body("data.userCount", equalTo(numResults)).body("data.selectedPage", equalTo(1)).body("data.pagination.docsPerPage", equalTo(numUsersReturned)).body("data.pagination.pageCount", equalTo(3)).body("data.pagination.numResults", equalTo(numResults));
    String userIdentifer2;
    for (int i = 0; i < numUsersReturned; i++) {
        userIdentifer2 = JsonPath.from(filterReponse02.getBody().asString()).getString("data.users[" + i + "].userIdentifier");
        assertEquals(randomUsernames.contains(userIdentifer2), true);
    }
    List<Object> userList2 = JsonPath.from(filterReponse02.body().asString()).getList("data.users");
    assertEquals(userList2.size(), numUsersReturned);
    // --------------------------------------------
    // Search for the 11 new users, return 5 per page, and start on NON-EXISTENT 4th page -- should revert to 1st page
    // --------------------------------------------
    Response filterReponse02a = UtilIT.filterAuthenticatedUsers(superuserApiToken, randUserNamePrefix, 4, numUsersReturned);
    filterReponse02a.then().assertThat().statusCode(OK.getStatusCode());
    filterReponse02a.prettyPrint();
    filterReponse02a.then().assertThat().body("data.userCount", equalTo(numResults)).body("data.selectedPage", equalTo(1)).body("data.pagination.docsPerPage", equalTo(numUsersReturned)).body("data.pagination.pageCount", equalTo(3)).body("data.pagination.numResults", equalTo(numResults));
    List<Object> userList2a = JsonPath.from(filterReponse02a.body().asString()).getList("data.users");
    assertEquals(userList2a.size(), numUsersReturned);
    // --------------------------------------------
    // Search for the 11 new users, return 5 per page, start on 3rd page
    // --------------------------------------------
    Response filterReponse03 = UtilIT.filterAuthenticatedUsers(superuserApiToken, randUserNamePrefix, 3, 5);
    filterReponse03.then().assertThat().statusCode(OK.getStatusCode());
    filterReponse03.prettyPrint();
    filterReponse03.then().assertThat().body("data.userCount", equalTo(numResults)).body("data.selectedPage", equalTo(3)).body("data.pagination.docsPerPage", equalTo(5)).body("data.pagination.hasNextPageNumber", equalTo(false)).body("data.pagination.pageCount", equalTo(3)).body("data.pagination.numResults", equalTo(numResults));
    List<Object> userList3 = JsonPath.from(filterReponse03.body().asString()).getList("data.users");
    assertEquals(userList3.size(), 1);
    // --------------------------------------------
    // Run search that returns no users
    // --------------------------------------------
    Response filterReponse04 = UtilIT.filterAuthenticatedUsers(superuserApiToken, "zzz" + randUserNamePrefix, 1, 50);
    filterReponse04.then().assertThat().statusCode(OK.getStatusCode());
    filterReponse04.prettyPrint();
    filterReponse04.then().assertThat().body("data.userCount", equalTo(0)).body("data.selectedPage", equalTo(1));
    List<Object> userList4 = JsonPath.from(filterReponse04.body().asString()).getList("data.users");
    assertEquals(userList4.size(), 0);
    // --------------------------------------------
    // Run search that returns 1 user
    // --------------------------------------------
    String singleUsername = randomUsernames.get(0);
    Response filterReponse05 = UtilIT.filterAuthenticatedUsers(superuserApiToken, singleUsername, 1, 50);
    filterReponse05.then().assertThat().statusCode(OK.getStatusCode());
    filterReponse05.prettyPrint();
    filterReponse05.then().assertThat().body("data.userCount", equalTo(1)).body("data.selectedPage", equalTo(1));
    List<Object> userList5 = JsonPath.from(filterReponse05.body().asString()).getList("data.users");
    assertEquals(userList5.size(), 1);
    // --------------------------------------------
    // Delete  random users
    // --------------------------------------------
    Response deleteUserResponse;
    for (String aUsername : randomUsernames) {
        deleteUserResponse = UtilIT.deleteUser(aUsername);
        assertEquals(200, deleteUserResponse.getStatusCode());
    }
    // --------------------------------------------
    // Delete superuser
    // --------------------------------------------
    deleteUserResponse = UtilIT.deleteUser(superuserUsername);
    assertEquals(200, deleteUserResponse.getStatusCode());
}
Also used : Response(com.jayway.restassured.response.Response) ArrayList(java.util.ArrayList) UtilIT.getRandomString(edu.harvard.iq.dataverse.api.UtilIT.getRandomString) Test(org.junit.Test)

Aggregations

Response (com.jayway.restassured.response.Response)1 UtilIT.getRandomString (edu.harvard.iq.dataverse.api.UtilIT.getRandomString)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1