blob: 962963b1a90965684a683661e4da78db21594fce [file] [log] [blame]
// Copyright (C) 2017 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.googlesource.gerrit.plugins.uploadvalidator;
import com.google.gerrit.reviewdb.client.Project;
import static com.google.common.truth.Truth.*;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.junit.Test;
public class RefAwareValidatorConfigTest {
private Project.NameKey projectName = new Project.NameKey("testProject");
@Test
public void isEnabledForAllRefsByDefault() throws Exception {
ValidatorConfig config =
getConfig("[plugin \"uploadvalidator\"]\n"
+ "blockedFileExtension = jar");
assertThat(config.isEnabledForRef(projectName, "anyRef")).isTrue();
}
@Test
public void isEnabledForSingleRef() throws Exception {
ValidatorConfig config =
getConfig("[plugin \"uploadvalidator\"]\n"
+ " ref = refs/heads/anyref\n"
+ " blockedFileExtension = jar");
assertThat(config.isEnabledForRef(projectName, "refs/heads/anyref"))
.isTrue();
}
@Test
public void isDisabledForInvalidRef() throws Exception {
ValidatorConfig config =
getConfig("[plugin \"uploadvalidator\"]\n"
+ " ref = anInvalidRef\n"
+ " blockedFileExtension = jar");
assertThat(config.isEnabledForRef(projectName, "refs/heads/anyref"))
.isFalse();
}
@Test
public void isEnabledForRegexRef() throws Exception {
ValidatorConfig config =
getConfig("[plugin \"uploadvalidator\"]\n"
+ " ref = ^refs/heads/mybranch.*\n"
+ " blockedFileExtension = jar");
assertThat(config.isEnabledForRef(projectName, "refs/heads/anotherref"))
.isFalse();
assertThat(config.isEnabledForRef(projectName, "refs/heads/mybranch123"))
.isTrue();
}
@Test
public void isEnabledForMultipleRefs() throws Exception {
ValidatorConfig config =
getConfig("[plugin \"uploadvalidator\"]\n"
+ " ref = refs/heads/branch1\n"
+ " ref = refs/heads/branch2\n"
+ " blockedFileExtension = jar");
assertThat(config.isEnabledForRef(projectName, "refs/heads/branch1"))
.isTrue();
assertThat(config.isEnabledForRef(projectName, "refs/heads/branch2"))
.isTrue();
assertThat(config.isEnabledForRef(projectName, "refs/heads/branch3"))
.isFalse();
}
private ValidatorConfig getConfig(String defaultConfig)
throws ConfigInvalidException {
ValidatorConfig config =
new ValidatorConfig(new FakeConfigFactory(projectName, defaultConfig));
return config;
}
}