KEYCLOAK-3704 Add missing test

This commit is contained in:
mhajas 2017-01-05 16:53:08 +01:00
parent 732d56b0c0
commit 61e7936d79
5 changed files with 54 additions and 9 deletions

View file

@ -88,6 +88,16 @@ module.controller('AlbumCtrl', function ($scope, $http, $routeParams, $location,
$location.path('/');
});
};
$scope.createWithInvalidUser = function () {
var newAlbum = new Album($scope.album);
newAlbum.$save({user: 'invalidUser'}, function (data) {
document.getElementById("output").innerHTML = 'Request was successful'
},
function (response) {
document.getElementById("output").innerHTML = response.data;
});
};
});
module.controller('ProfileCtrl', function ($scope, $http, $routeParams, $location, Profile) {

View file

@ -4,4 +4,5 @@
Name: <input type="text" id="album.name" ng-model="album.name"/>
<button ng-click="create()" id="save-album">Save</button>
<button ng-click="createWithInvalidUser()" id="save-album-invalid">Save with invalid user</button>
</form>

View file

@ -22,6 +22,7 @@ import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
@ -54,17 +55,18 @@ public class AlbumService {
@POST
@Consumes("application/json")
public Response create(Album newAlbum) {
public Response create(Album newAlbum, @QueryParam("user") String username) {
newAlbum.setId(++nextId);
Principal userPrincipal = request.getUserPrincipal();
newAlbum.setUserId(userPrincipal.getName());
if (username == null) {
username = request.getUserPrincipal().getName();
}
newAlbum.setUserId(username);
Query queryDuplicatedAlbum = this.entityManager.createQuery("from Album where name = :name and userId = :userId");
queryDuplicatedAlbum.setParameter("name", newAlbum.getName());
queryDuplicatedAlbum.setParameter("userId", userPrincipal.getName());
queryDuplicatedAlbum.setParameter("userId", username);
if (!queryDuplicatedAlbum.getResultList().isEmpty()) {
throw new ErrorResponse("Name [" + newAlbum.getName() + "] already taken. Choose another one.", Status.CONFLICT);

View file

@ -53,23 +53,34 @@ public class PhotozClientAuthzTestApp extends AbstractPageWithInjectedUrl {
protected ConsentPage consentPage;
@FindBy(xpath = "//a[@ng-click = 'Identity.logout()']")
WebElement signOutButton;
private WebElement signOutButton;
@FindBy(id = "entitlement")
WebElement entitlement;
private WebElement entitlement;
@FindBy(id = "entitlements")
WebElement entitlements;
private WebElement entitlements;
@FindBy(id = "output")
private WebElement output;
public void createAlbum(String name) {
createAlbum(name, "save-album");
}
public void createAlbum(String name, String buttonId) {
navigateTo();
this.driver.findElement(By.id("create-album")).click();
Form.setInputValue(this.driver.findElement(By.id("album.name")), name);
pause(200); // We need to wait a bit for the form to "accept" the input (otherwise it registers the input as empty)
this.driver.findElement(By.id("save-album")).click();
this.driver.findElement(By.id(buttonId)).click();
pause(WAIT_AFTER_OPERATION);
}
public void createAlbumWithInvalidUser(String name) {
createAlbum(name, "save-album-invalid");
}
@Override
public URL getInjectedUrl() {
return this.url;
@ -137,6 +148,10 @@ public class PhotozClientAuthzTestApp extends AbstractPageWithInjectedUrl {
pause(WAIT_AFTER_OPERATION);
}
public WebElement getOutput() {
return output;
}
@Override
public void navigateTo(boolean waitForMatch) {
super.navigateTo(waitForMatch);

View file

@ -59,6 +59,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.keycloak.testsuite.util.IOUtil.loadJson;
import static org.keycloak.testsuite.util.IOUtil.loadRealm;
import static org.keycloak.testsuite.util.WaitUtils.waitUntilElement;
/**
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
@ -141,6 +142,22 @@ public abstract class AbstractPhotozExampleAdapterTest extends AbstractExampleAd
}
}
@Test
public void createAlbumWithInvalidUser() {
try {
this.deployer.deploy(RESOURCE_SERVER_ID);
loginToClientPage("alice", "alice");
clientPage.createAlbumWithInvalidUser("Alice Family Album");
waitUntilElement(clientPage.getOutput()).text().not().contains("Request was successful");
waitUntilElement(clientPage.getOutput()).text().contains("Could not register protected resource");
} finally {
this.deployer.undeploy(RESOURCE_SERVER_ID);
}
}
@Test
public void testOnlyOwnerCanDeleteAlbum() throws Exception {
try {