forked from SUSE/Portus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamespace_spec.rb
94 lines (80 loc) · 3.23 KB
/
namespace_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
require "rails_helper"
describe Namespace do
it { should have_many(:repositories) }
it { should belong_to(:team) }
it { should validate_presence_of(:name) }
it { should allow_value("test1", "1test", "another-test").for(:name) }
it { should_not allow_value("TesT1", "1Test", "another_test!").for(:name) }
context "validator" do
let(:registry) { create(:registry) }
let(:owner) { create(:user) }
let(:team) { create(:team, owners: [owner]) }
let(:namespace) { create(:namespace, team: team) }
it "checks for the uniqueness of the namespace inside of the registry" do
Namespace.create!(team: team, registry: registry, name: "lala")
expect do
Namespace.create!(team: team, name: "lala", registry: registry)
end.to raise_error(ActiveRecord::RecordInvalid, /Name has already been taken/)
end
it "checks tat the namespace name follows the proper format" do
["-a", "&a", "_invalid", "R2D2", "also_invalid_"].each do |name|
n = Namespace.new(team: team, registry: registry, name: name)
expect(n).to_not be_valid
end
["a", "1", "1.0", "r2d2", "thingie", "is_valid"].each do |name|
n = Namespace.new(team: team, registry: registry, name: name)
expect(n).to be_valid
end
end
it "checks the length of the name" do
name = (0...100).map { ("a".."z").to_a[rand(26)] }.join
n = Namespace.new(team: team, registry: registry, name: name)
expect(n).to be_valid
name = (0...270).map { ("a".."z").to_a[rand(26)] }.join
n = Namespace.new(team: team, registry: registry, name: name)
expect(n).to_not be_valid
end
end
context "global namespace" do
it "must be public" do
namespace = create(:namespace, global: true, public: true)
namespace.public = false
expect(namespace.save).to be false
end
end
describe "namespace_clean_name" do
let(:registry) { create(:registry) }
let(:owner) { create(:user) }
let(:team) { create(:team, owners: [owner]) }
let(:namespace) { create(:namespace, team: team) }
context "non global namespace" do
it "returns the name of the namespace" do
expect(namespace.clean_name).to eq(namespace.name)
end
end
context "global namespace" do
it "returns the name of the namespace" do
global_namespace = create(:namespace, global: true, public: true, registry: registry)
expect(global_namespace.clean_name).to eq(registry.hostname)
end
end
end
describe "get_from_name" do
let!(:registry) { create(:registry) }
let!(:owner) { create(:user) }
let!(:team) { create(:team, owners: [owner]) }
let!(:namespace) { create(:namespace, team: team) }
let!(:repo) { create(:repository, namespace: namespace) }
it "works for global namespaces" do
ns = Namespace.find_by(global: true)
namespace, name = Namespace.get_from_name(repo.name)
expect(namespace.id).to eq ns.id
expect(name).to eq repo.name
end
it "works for user namespaces" do
ns, name = Namespace.get_from_name("#{namespace.name}/#{repo.name}")
expect(ns.id).to eq namespace.id
expect(name).to eq repo.name
end
end
end