-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
e1da44d
commit f1d287b
Showing
11 changed files
with
177 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#Sun Sep 21 13:03:28 CEST 2014 | ||
#Sun Sep 21 13:09:15 CEST 2014 | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-2.1-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-2.1-all.zip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
ontrack-it-utils/src/main/java/net/nemerosa/ontrack/it/AbstractServiceTestSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package net.nemerosa.ontrack.it; | ||
|
||
import net.nemerosa.ontrack.model.security.*; | ||
import net.nemerosa.ontrack.model.structure.*; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.authentication.TestingAuthenticationToken; | ||
import org.springframework.security.core.context.SecurityContext; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.core.context.SecurityContextImpl; | ||
|
||
import java.util.Collections; | ||
import java.util.Optional; | ||
import java.util.concurrent.Callable; | ||
|
||
import static net.nemerosa.ontrack.test.TestUtils.uid; | ||
|
||
public abstract class AbstractServiceTestSupport extends AbstractITTestSupport { | ||
|
||
@Autowired | ||
protected AccountService accountService; | ||
|
||
@Autowired | ||
protected StructureService structureService; | ||
|
||
protected Account doCreateAccount() throws Exception { | ||
return asUser().with(AccountManagement.class).call(() -> { | ||
String name = uid("A"); | ||
return accountService.create( | ||
new AccountInput( | ||
name, | ||
"Test " + name, | ||
name + "@test.com", | ||
"test", | ||
Collections.emptyList() | ||
) | ||
); | ||
}); | ||
} | ||
|
||
protected Project doCreateProject() throws Exception { | ||
return doCreateProject(nameDescription()); | ||
} | ||
|
||
protected Project doCreateProject(NameDescription nameDescription) throws Exception { | ||
return asUser().with(ProjectCreation.class).call(() -> structureService.newProject( | ||
Project.of(nameDescription) | ||
)); | ||
} | ||
|
||
protected Branch doCreateBranch() throws Exception { | ||
return doCreateBranch(doCreateProject(), nameDescription()); | ||
} | ||
|
||
protected Branch doCreateBranch(Project project, NameDescription nameDescription) throws Exception { | ||
return asUser().with(project.id(), BranchCreate.class).call(() -> structureService.newBranch( | ||
Branch.of(project, nameDescription) | ||
)); | ||
} | ||
|
||
protected UserCall asUser() { | ||
return new UserCall(); | ||
} | ||
|
||
protected AccountCall asAccount(Account account) { | ||
return new AccountCall(account); | ||
} | ||
|
||
protected <T> T view(ProjectEntity projectEntity, Callable<T> callable) throws Exception { | ||
return asUser().with(projectEntity.projectId(), ProjectView.class).call(callable); | ||
} | ||
|
||
protected static interface ContextCall { | ||
<T> T call(Callable<T> call) throws Exception; | ||
} | ||
|
||
protected static abstract class AbstractContextCall implements ContextCall { | ||
|
||
@Override | ||
public <T> T call(Callable<T> call) throws Exception { | ||
// Gets the current context | ||
SecurityContext oldContext = SecurityContextHolder.getContext(); | ||
try { | ||
// Sets the new context | ||
contextSetup(); | ||
// Call | ||
return call.call(); | ||
} finally { | ||
// Restores the context | ||
SecurityContextHolder.setContext(oldContext); | ||
} | ||
} | ||
|
||
protected abstract void contextSetup(); | ||
} | ||
|
||
protected static class AccountCall extends AbstractContextCall { | ||
|
||
protected final Account account; | ||
|
||
public AccountCall(Account account) { | ||
this.account = account; | ||
} | ||
|
||
public AccountCall(String name, SecurityRole role) { | ||
this(Account.of(name, name, name + "@test.com", role, AuthenticationSource.none())); | ||
} | ||
|
||
@Override | ||
protected void contextSetup() { | ||
SecurityContext context = new SecurityContextImpl(); | ||
TestingAuthenticationToken authentication = new TestingAuthenticationToken( | ||
(AccountHolder) () -> account, | ||
"", | ||
account.getRole().name() | ||
); | ||
context.setAuthentication(authentication); | ||
SecurityContextHolder.setContext(context); | ||
} | ||
} | ||
|
||
protected static class UserCall extends AccountCall { | ||
|
||
public UserCall() { | ||
super("user", SecurityRole.USER); | ||
} | ||
|
||
public UserCall with(Class<? extends GlobalFunction> fn) { | ||
account.withGlobalRole( | ||
Optional.of( | ||
new GlobalRole( | ||
"test", "Test global role", "", | ||
Collections.singleton(fn), | ||
Collections.emptySet() | ||
) | ||
) | ||
); | ||
return this; | ||
} | ||
|
||
public UserCall with(int projectId, Class<? extends ProjectFunction> fn) { | ||
account.withProjectRole( | ||
new ProjectRoleAssociation( | ||
projectId, | ||
new ProjectRole( | ||
"test", "Test", "", | ||
Collections.singleton(fn) | ||
) | ||
) | ||
); | ||
return this; | ||
} | ||
|
||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...ory-impl/src/test/java/net/nemerosa/ontrack/repository/AbstractRepositoryTestSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package net.nemerosa.ontrack.repository; | ||
|
||
import net.nemerosa.ontrack.it.AbstractITTestSupport; | ||
|
||
public abstract class AbstractRepositoryTestSupport extends AbstractITTestSupport { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.