Skip to content

Commit

Permalink
dcoraboeuf#285 Unit test for the securization of some services (not w…
Browse files Browse the repository at this point in the history
…orking yet - the advisor is not applied on all eligible beans)
  • Loading branch information
dcoraboeuf committed Nov 29, 2013
1 parent adb7698 commit e5e8f21
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package net.ontrack.backend;

import net.ontrack.core.model.*;
import net.ontrack.core.security.GlobalFunction;
import net.ontrack.core.security.SecurityRoles;
import net.ontrack.core.security.SecurityUtils;
import net.ontrack.service.AccountService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;

import java.util.concurrent.Callable;

Expand All @@ -15,7 +17,6 @@ public class AccountServiceTest extends AbstractValidationTest {

@Autowired
private AccountService accountService;

@Autowired
private SecurityUtils securityUtils;

Expand All @@ -42,6 +43,74 @@ public void authenticate_wrong_password() {
assertNull(account);
}

@Test
public void createAccount_admin_ok() throws Exception {
final String name = uid("A");
Account account = asAdmin().call(new Callable<Account>() {
@Override
public Account call() throws Exception {
return accountService.getAccount(
accountService.createAccount(
new AccountCreationForm(
name,
"Account " + name,
name + "@test.com",
SecurityRoles.USER,
"builtin",
"***",
"***"
)
).getValue());
}
});
assertEquals(name, account.getName());
}

@Test
public void createAccount_user_granted_ok() throws Exception {
final String name = uid("A");
Account account = asUser().withGlobalFn(GlobalFunction.ACCOUNT_MANAGEMENT).call(new Callable<Account>() {
@Override
public Account call() throws Exception {
return accountService.getAccount(
accountService.createAccount(
new AccountCreationForm(
name,
"Account " + name,
name + "@test.com",
SecurityRoles.USER,
"builtin",
"***",
"***"
)
).getValue());
}
});
assertEquals(name, account.getName());
}

@Test(expected = AccessDeniedException.class)
public void createAccount_user_denied() throws Exception {
final String name = uid("A");
asUser().call(new Callable<Account>() {
@Override
public Account call() throws Exception {
return accountService.getAccount(
accountService.createAccount(
new AccountCreationForm(
name,
"Account " + name,
name + "@test.com",
SecurityRoles.USER,
"builtin",
"***",
"***"
)
).getValue());
}
});
}

@Test
public void change_email_ok() throws Exception {
securityUtils.asAdmin(new Callable<Void>() {
Expand Down
4 changes: 2 additions & 2 deletions ontrack-backend/src/test/resources/log4j.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

log4j.rootLogger=error, Console

#log4j.logger.org.springframework=info
#log4j.logger.net.ontrack.backend.config.BackendProtectPointcutPostProcessor=debug
log4j.logger.org.springframework.security=debug
log4j.logger.net.ontrack.backend.config.BackendProtectPointcutPostProcessor=debug
#log4j.logger.net.ontrack=debug

log4j.appender.Console=org.apache.log4j.ConsoleAppender
Expand Down

0 comments on commit e5e8f21

Please sign in to comment.