fix git tag command unit test

This commit is contained in:
Konstantin Schaper
2020-12-02 09:39:17 +01:00
parent 0dbea12a62
commit 2fa4972883
2 changed files with 28 additions and 14 deletions

View File

@@ -65,14 +65,12 @@ import static sonia.scm.ContextEntry.ContextBuilder.entity;
import static sonia.scm.NotFoundException.notFound; import static sonia.scm.NotFoundException.notFound;
public class GitTagCommand extends AbstractGitCommand implements TagCommand { public class GitTagCommand extends AbstractGitCommand implements TagCommand {
private final GPG gpg;
private final HookContextFactory hookContextFactory; private final HookContextFactory hookContextFactory;
private final ScmEventBus eventBus; private final ScmEventBus eventBus;
@Inject @Inject
GitTagCommand(GitContext context, GPG gpg, HookContextFactory hookContextFactory, ScmEventBus eventBus) { GitTagCommand(GitContext context, HookContextFactory hookContextFactory, ScmEventBus eventBus) {
super(context); super(context);
this.gpg = gpg;
this.hookContextFactory = hookContextFactory; this.hookContextFactory = hookContextFactory;
this.eventBus = eventBus; this.eventBus = eventBus;
} }
@@ -114,20 +112,12 @@ public class GitTagCommand extends AbstractGitCommand implements TagCommand {
User user = SecurityUtils.getSubject().getPrincipals().oneByType(User.class); User user = SecurityUtils.getSubject().getPrincipals().oneByType(User.class);
PersonIdent taggerIdent = new PersonIdent(user.getDisplayName(), user.getMail()); PersonIdent taggerIdent = new PersonIdent(user.getDisplayName(), user.getMail());
// Ref ref =
git.tag() git.tag()
.setObjectId(revObject) .setObjectId(revObject)
.setTagger(taggerIdent) .setTagger(taggerIdent)
.setName(name) .setName(name)
.call(); .call();
// Uncomment lines once jgit added support for signing tags
// try (RevWalk walk = new RevWalk(git.getRepository())) {
// revObject = walk.parseTag(ref.getObjectId());
// final Optional<Signature> tagSignature = GitUtil.getTagSignature(revObject, gpg, walk);
// tagSignature.ifPresent(tag::addSignature);
// }
eventBus.post(new PostReceiveRepositoryHookEvent(hookEvent)); eventBus.post(new PostReceiveRepositoryHookEvent(hookEvent));
return tag; return tag;

View File

@@ -24,7 +24,12 @@
package sonia.scm.repository.spi; package sonia.scm.repository.spi;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ThreadContext;
import org.eclipse.jgit.lib.GpgSigner; import org.eclipse.jgit.lib.GpgSigner;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@@ -42,6 +47,7 @@ import sonia.scm.repository.api.HookContextFactory;
import sonia.scm.repository.api.TagDeleteRequest; import sonia.scm.repository.api.TagDeleteRequest;
import sonia.scm.repository.api.TagCreateRequest; import sonia.scm.repository.api.TagCreateRequest;
import sonia.scm.security.GPG; import sonia.scm.security.GPG;
import sonia.scm.util.MockUtil;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
@@ -65,16 +71,34 @@ public class GitTagCommandTest extends AbstractGitCommandTestBase {
@Mock @Mock
private ScmEventBus eventBus; private ScmEventBus eventBus;
private Subject subject;
@Before @Before
public void setSigner() { public void setSigner() {
GpgSigner.setDefault(new GitTestHelper.SimpleGpgSigner()); GpgSigner.setDefault(new GitTestHelper.SimpleGpgSigner());
} }
@Before
public void bindThreadContext() {
SecurityUtils.setSecurityManager(new DefaultSecurityManager());
subject = MockUtil.createUserSubject(SecurityUtils.getSecurityManager());
ThreadContext.bind(subject);
}
@After
public void unbindThreadContext() {
ThreadContext.unbindSubject();
ThreadContext.unbindSecurityManager();
}
@Test @Test
public void shouldCreateATag() throws IOException { public void shouldCreateATag() throws IOException {
createCommand().create(new TagCreateRequest("592d797cd36432e591416e8b2b98154f4f163411", "newtag")); createCommand().create(new TagCreateRequest("592d797cd36432e591416e8b2b98154f4f163411", "newtag"));
Optional<Tag> tag = findTag(createContext(), "newtag"); Optional<Tag> optionalTag = findTag(createContext(), "newtag");
assertThat(tag).isNotEmpty(); assertThat(optionalTag).isNotEmpty();
final Tag tag = optionalTag.get();
assertThat(tag.getName()).isEqualTo("newtag");
assertThat(tag.getRevision()).isEqualTo("592d797cd36432e591416e8b2b98154f4f163411");
} }
@Test @Test
@@ -126,7 +150,7 @@ public class GitTagCommandTest extends AbstractGitCommandTestBase {
} }
private GitTagCommand createCommand() { private GitTagCommand createCommand() {
return new GitTagCommand(createContext(), gpg, hookContextFactory, eventBus); return new GitTagCommand(createContext(), hookContextFactory, eventBus);
} }
private List<Tag> readTags(GitContext context) throws IOException { private List<Tag> readTags(GitContext context) throws IOException {