gritano-core 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.3"
4
+ - "2.0.0"
5
+ before_script:
6
+ - bundle exec rake db:migrate
7
+ script: bundle exec rake
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'activerecord', "~> 4.0.0.rc1"
4
+ gem "grit", "~> 2.5.0"
5
+
6
+ group :development do
7
+ gem "rspec", "~> 2.13.0"
8
+ gem "rdoc", "~> 4.0.0"
9
+ gem "jeweler", "~> 1.8.4"
10
+ gem "simplecov", "~> 0.7.0"
11
+ gem "sqlite3", "~> 1.3.7"
12
+ gem 'database_cleaner', "~> 1.0.1"
13
+ gem 'guard-rspec'
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,101 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (4.0.0.rc1)
5
+ activesupport (= 4.0.0.rc1)
6
+ builder (~> 3.1.0)
7
+ activerecord (4.0.0.rc1)
8
+ activemodel (= 4.0.0.rc1)
9
+ activerecord-deprecated_finders (~> 1.0.2)
10
+ activesupport (= 4.0.0.rc1)
11
+ arel (~> 4.0.0)
12
+ activerecord-deprecated_finders (1.0.2)
13
+ activesupport (4.0.0.rc1)
14
+ i18n (~> 0.6, >= 0.6.4)
15
+ minitest (~> 4.2)
16
+ multi_json (~> 1.3)
17
+ thread_safe (~> 0.1)
18
+ tzinfo (~> 0.3.37)
19
+ arel (4.0.0)
20
+ atomic (1.1.9)
21
+ builder (3.1.4)
22
+ coderay (1.0.9)
23
+ database_cleaner (1.0.1)
24
+ diff-lcs (1.2.4)
25
+ ffi (1.8.1)
26
+ formatador (0.2.4)
27
+ git (1.2.5)
28
+ grit (2.5.0)
29
+ diff-lcs (~> 1.1)
30
+ mime-types (~> 1.15)
31
+ posix-spawn (~> 0.3.6)
32
+ guard (1.8.0)
33
+ formatador (>= 0.2.4)
34
+ listen (>= 1.0.0)
35
+ lumberjack (>= 1.0.2)
36
+ pry (>= 0.9.10)
37
+ thor (>= 0.14.6)
38
+ guard-rspec (3.0.1)
39
+ guard (>= 1.8)
40
+ rspec (~> 2.13)
41
+ i18n (0.6.4)
42
+ jeweler (1.8.4)
43
+ bundler (~> 1.0)
44
+ git (>= 1.2.5)
45
+ rake
46
+ rdoc
47
+ json (1.8.0)
48
+ listen (1.1.6)
49
+ rb-fsevent (>= 0.9.3)
50
+ rb-inotify (>= 0.9)
51
+ rb-kqueue (>= 0.2)
52
+ lumberjack (1.0.3)
53
+ method_source (0.8.1)
54
+ mime-types (1.23)
55
+ minitest (4.7.4)
56
+ multi_json (1.7.5)
57
+ posix-spawn (0.3.6)
58
+ pry (0.9.12.2)
59
+ coderay (~> 1.0.5)
60
+ method_source (~> 0.8)
61
+ slop (~> 3.4)
62
+ rake (10.0.4)
63
+ rb-fsevent (0.9.3)
64
+ rb-inotify (0.9.0)
65
+ ffi (>= 0.5.0)
66
+ rb-kqueue (0.2.0)
67
+ ffi (>= 0.5.0)
68
+ rdoc (4.0.1)
69
+ json (~> 1.4)
70
+ rspec (2.13.0)
71
+ rspec-core (~> 2.13.0)
72
+ rspec-expectations (~> 2.13.0)
73
+ rspec-mocks (~> 2.13.0)
74
+ rspec-core (2.13.1)
75
+ rspec-expectations (2.13.0)
76
+ diff-lcs (>= 1.1.3, < 2.0)
77
+ rspec-mocks (2.13.1)
78
+ simplecov (0.7.1)
79
+ multi_json (~> 1.0)
80
+ simplecov-html (~> 0.7.1)
81
+ simplecov-html (0.7.1)
82
+ slop (3.4.5)
83
+ sqlite3 (1.3.7)
84
+ thor (0.18.1)
85
+ thread_safe (0.1.0)
86
+ atomic
87
+ tzinfo (0.3.37)
88
+
89
+ PLATFORMS
90
+ ruby
91
+
92
+ DEPENDENCIES
93
+ activerecord (~> 4.0.0.rc1)
94
+ database_cleaner (~> 1.0.1)
95
+ grit (~> 2.5.0)
96
+ guard-rspec
97
+ jeweler (~> 1.8.4)
98
+ rdoc (~> 4.0.0)
99
+ rspec (~> 2.13.0)
100
+ simplecov (~> 0.7.0)
101
+ sqlite3 (~> 1.3.7)
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Ígor Bonadio
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,23 @@
1
+ = Gritano::Core
2
+
3
+ {<img src="https://travis-ci.org/igorbonadio/gritano-core.png" />}[https://travis-ci.org/igorbonadio/gritano-core]
4
+
5
+ Gritano::Core is a library that defines all Gritano's Models.
6
+
7
+ It can be used to develop tools that manage users, repositories and permissions.
8
+
9
+ == Contributing to Gritano::Core
10
+
11
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
12
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
13
+ * Fork the project.
14
+ * Start a feature/bugfix branch.
15
+ * Commit and push until you are happy with your contribution.
16
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
17
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
18
+
19
+ == Copyright
20
+
21
+ Copyright (c) 2013 Ígor Bonadio. See LICENSE.txt for
22
+ further details.
23
+
data/Rakefile ADDED
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "gritano-core"
18
+ gem.homepage = "http://github.com/igorbonadio/gritano-core"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Gritano::Core is a library that defines all Gritano's Models.}
21
+ gem.description = %Q{Gritano::Core is a library that defines all Gritano's Models. It can be used to develop tools that manage users, repositories and permissions.}
22
+ gem.email = "igorbonadio@gmail.com"
23
+ gem.authors = ["Igor Bonadio"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "gritano-core #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
50
+
51
+ require File.expand_path('../lib/gritano-core', __FILE__)
52
+ namespace :db do
53
+ desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x"
54
+ task :migrate do
55
+ Gritano::Core::Migration.migrate(YAML::load(File.open("db/development.yml")))
56
+ end
57
+ end
data/TODO ADDED
@@ -0,0 +1,5 @@
1
+ Lançamento:
2
+ ✔ migration method @done (13-06-05 12:11)
3
+ ✔ validar se o repo termina com .git @done (13-06-05 11:15)
4
+ ✔ README @done (13-06-06 21:11)
5
+ ☐ Bump para a versão 1.1.0
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.1.0
@@ -0,0 +1,2 @@
1
+ adapter: sqlite3
2
+ database: db/development.sqlite3
@@ -0,0 +1,9 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def change
3
+ create_table :users do |t|
4
+ t.string :login
5
+ t.boolean :admin, default: false
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ class CreateKeys < ActiveRecord::Migration
2
+ def change
3
+ create_table :keys do |t|
4
+ t.references :user
5
+ t.string :name
6
+ t.string :key
7
+ t.timestamps
8
+ end
9
+ add_index :keys, :user_id
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ class CreateRepositories < ActiveRecord::Migration
2
+ def change
3
+ create_table :repositories do |t|
4
+ t.string :name
5
+ t.string :path
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ class CreatePermissions < ActiveRecord::Migration
2
+ def change
3
+ create_table :permissions do |t|
4
+ t.references :user
5
+ t.references :repository
6
+ t.integer :access
7
+ t.timestamps
8
+ end
9
+ add_index :permissions, :user_id
10
+ add_index :permissions, :repository_id
11
+ end
12
+ end
@@ -0,0 +1,93 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "gritano-core"
8
+ s.version = "1.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Igor Bonadio"]
12
+ s.date = "2013-06-08"
13
+ s.description = "Gritano::Core is a library that defines all Gritano's Models. It can be used to develop tools that manage users, repositories and permissions."
14
+ s.email = "igorbonadio@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc",
18
+ "TODO"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ ".rspec",
23
+ ".travis.yml",
24
+ "Gemfile",
25
+ "Gemfile.lock",
26
+ "Guardfile",
27
+ "LICENSE.txt",
28
+ "README.rdoc",
29
+ "Rakefile",
30
+ "VERSION",
31
+ "db/development.yml",
32
+ "db/migrate/001_create_users.rb",
33
+ "db/migrate/002_create_keys.rb",
34
+ "db/migrate/003_create_repositories.rb",
35
+ "db/migrate/004_create_permissions.rb",
36
+ "gritano-core.gemspec",
37
+ "lib/gritano-core.rb",
38
+ "lib/gritano-core/key.rb",
39
+ "lib/gritano-core/migration.rb",
40
+ "lib/gritano-core/permission.rb",
41
+ "lib/gritano-core/repository.rb",
42
+ "lib/gritano-core/user.rb",
43
+ "spec/key.pub",
44
+ "spec/key_spec.rb",
45
+ "spec/migration_spec.rb",
46
+ "spec/permission_spec.rb",
47
+ "spec/repository_spec.rb",
48
+ "spec/spec_helper.rb",
49
+ "spec/user_spec.rb"
50
+ ]
51
+ s.homepage = "http://github.com/igorbonadio/gritano-core"
52
+ s.licenses = ["MIT"]
53
+ s.require_paths = ["lib"]
54
+ s.rubygems_version = "1.8.25"
55
+ s.summary = "Gritano::Core is a library that defines all Gritano's Models."
56
+
57
+ if s.respond_to? :specification_version then
58
+ s.specification_version = 3
59
+
60
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
61
+ s.add_runtime_dependency(%q<activerecord>, ["~> 4.0.0.rc1"])
62
+ s.add_runtime_dependency(%q<grit>, ["~> 2.5.0"])
63
+ s.add_development_dependency(%q<rspec>, ["~> 2.13.0"])
64
+ s.add_development_dependency(%q<rdoc>, ["~> 4.0.0"])
65
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
66
+ s.add_development_dependency(%q<simplecov>, ["~> 0.7.0"])
67
+ s.add_development_dependency(%q<sqlite3>, ["~> 1.3.7"])
68
+ s.add_development_dependency(%q<database_cleaner>, ["~> 1.0.1"])
69
+ s.add_development_dependency(%q<guard-rspec>, [">= 0"])
70
+ else
71
+ s.add_dependency(%q<activerecord>, ["~> 4.0.0.rc1"])
72
+ s.add_dependency(%q<grit>, ["~> 2.5.0"])
73
+ s.add_dependency(%q<rspec>, ["~> 2.13.0"])
74
+ s.add_dependency(%q<rdoc>, ["~> 4.0.0"])
75
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
76
+ s.add_dependency(%q<simplecov>, ["~> 0.7.0"])
77
+ s.add_dependency(%q<sqlite3>, ["~> 1.3.7"])
78
+ s.add_dependency(%q<database_cleaner>, ["~> 1.0.1"])
79
+ s.add_dependency(%q<guard-rspec>, [">= 0"])
80
+ end
81
+ else
82
+ s.add_dependency(%q<activerecord>, ["~> 4.0.0.rc1"])
83
+ s.add_dependency(%q<grit>, ["~> 2.5.0"])
84
+ s.add_dependency(%q<rspec>, ["~> 2.13.0"])
85
+ s.add_dependency(%q<rdoc>, ["~> 4.0.0"])
86
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
87
+ s.add_dependency(%q<simplecov>, ["~> 0.7.0"])
88
+ s.add_dependency(%q<sqlite3>, ["~> 1.3.7"])
89
+ s.add_dependency(%q<database_cleaner>, ["~> 1.0.1"])
90
+ s.add_dependency(%q<guard-rspec>, [">= 0"])
91
+ end
92
+ end
93
+
@@ -0,0 +1,10 @@
1
+ ROOT_PATH = File.expand_path(File.dirname(__FILE__))
2
+
3
+ require 'active_record'
4
+ require 'grit'
5
+
6
+ require File.join(ROOT_PATH, 'gritano-core/user')
7
+ require File.join(ROOT_PATH, 'gritano-core/repository')
8
+ require File.join(ROOT_PATH, 'gritano-core/permission')
9
+ require File.join(ROOT_PATH, 'gritano-core/key')
10
+ require File.join(ROOT_PATH, 'gritano-core/migration')
@@ -0,0 +1,16 @@
1
+ module Gritano
2
+ module Core
3
+ class Key < ActiveRecord::Base
4
+ validates :name, presence: true
5
+ validates :name, uniqueness: {scope: :user_id}
6
+
7
+ validates :key, presence: true
8
+ validates :key, format: /\Assh-(?:dss|rsa) .* /
9
+ validates :user_id, presence: true
10
+
11
+ def key=(pubkey)
12
+ write_attribute(:key, pubkey.scan(/\Assh-(?:dss|rsa) .* /)[0][0..-2])
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ module Gritano
2
+ module Core
3
+ class Migration
4
+ def self.migrate(connection_params)
5
+ ActiveRecord::Base.establish_connection(connection_params)
6
+ ActiveRecord::Migrator.migrate(File.join(File.dirname(__FILE__), '../../db/migrate'), ENV["VERSION"] ? ENV["VERSION"].to_i : nil )
7
+ end
8
+
9
+ def self.[](migration)
10
+ filename = Dir.entries(File.join(File.dirname(__FILE__), '../../db/migrate')).
11
+ select { |file| /\A\d+_#{migration}\.rb\z/.match file }.first
12
+ if filename
13
+ File.open(File.join(File.dirname(__FILE__), '../../db/migrate', filename)).readlines.join
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,45 @@
1
+ module Gritano
2
+ module Core
3
+ class Permission < ActiveRecord::Base
4
+ belongs_to :user
5
+ belongs_to :repository
6
+
7
+ validates :user_id, presence: true
8
+ validates :repository_id, presence: true
9
+ validates :access, presence: true
10
+
11
+ def add_access(type)
12
+ if type == :read
13
+ self.access = READ | (self.access || 0)
14
+ elsif type == :write
15
+ self.access = WRITE | (self.access || 0)
16
+ else
17
+ return false
18
+ end
19
+ return true
20
+ end
21
+
22
+ def remove_access(access)
23
+ if access == :read
24
+ self.access = (self.access || 0) & (~ READ)
25
+ elsif access == :write
26
+ self.access = (self.access || 0) & (~ WRITE)
27
+ else
28
+ return false
29
+ end
30
+ return true
31
+ end
32
+
33
+ def is(type)
34
+ if type == :read
35
+ return (self.access & READ) == READ
36
+ elsif type == :write
37
+ return (self.access & WRITE) == WRITE
38
+ end
39
+ end
40
+
41
+ READ = 1
42
+ WRITE = 2
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,28 @@
1
+ module Gritano
2
+ module Core
3
+ class Repository < ActiveRecord::Base
4
+ has_many :permissions
5
+ has_many :users, through: :permissions
6
+
7
+ validates :name, presence: true
8
+ validates :name, uniqueness: true
9
+ validates :name, format: /\.git\z/
10
+ validates :path, presence: true
11
+
12
+ before_create :create_bare_repo
13
+ after_destroy :destroy_bare_repo
14
+
15
+ def create_bare_repo
16
+ Grit::Repo.init_bare(full_path)
17
+ end
18
+
19
+ def destroy_bare_repo
20
+ FileUtils.rm_r(full_path, force: true)
21
+ end
22
+
23
+ def full_path
24
+ File.join(path, name)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,33 @@
1
+ module Gritano
2
+ module Core
3
+ class User < ActiveRecord::Base
4
+ validates :login, presence: true
5
+ validates :login, uniqueness: true
6
+
7
+ has_many :keys
8
+ has_many :permissions
9
+ has_many :repositories, through: :permissions
10
+
11
+ def add_access(repo, access_type)
12
+ permission = repo.permissions.where(user_id: self.id).first || repo.permissions.new(user_id: self.id)
13
+ permission.add_access(access_type)
14
+ permission.save
15
+ end
16
+
17
+ def remove_access(repo, access_type)
18
+ permission = repo.permissions.where(user_id: self.id).first || repo.permissions.new(user_id: self.id)
19
+ permission.remove_access(access_type)
20
+ permission.save
21
+ end
22
+
23
+ def check_access(repo, access_type)
24
+ permission = repo.permissions.where(user_id: self.id).first
25
+ if permission
26
+ permission.is(access_type)
27
+ else
28
+ false
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
data/spec/key.pub ADDED
@@ -0,0 +1 @@
1
+ ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key
data/spec/key_spec.rb ADDED
@@ -0,0 +1,25 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ module Gritano::Core
4
+ describe Key do
5
+ it "should be invalid without a name" do
6
+ Key.new(user_id: 1, key: File.open('spec/key.pub').readlines.join).should be_invalid
7
+ end
8
+
9
+ it "should be invalid without a ssh key" do
10
+ Key.new(user_id: 1, name: "my_key").should be_invalid
11
+ end
12
+
13
+ it "should belongs to a user" do
14
+ Key.new(name: "my_key", key: File.open('spec/key.pub').readlines.join).should be_invalid
15
+ end
16
+
17
+ it "should have a unique name per user" do
18
+ u1 = User.create(login: 'igorbonadio')
19
+ u2 = User.create(login: 'jessicaeto')
20
+ u1.keys.create(name: 'my_key', key: File.open('spec/key.pub').readlines.join)
21
+ u1.keys.new(name: 'my_key', key: File.open('spec/key.pub').readlines.join).should be_invalid
22
+ u2.keys.new(name: 'my_key', key: File.open('spec/key.pub').readlines.join).should be_valid
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,26 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ require 'pathname'
4
+
5
+ module Gritano::Core
6
+ describe Migration do
7
+ it "should migrate a database" do
8
+ params = {adapter: 'sqlite3', database: 'db/development.sqlite3'}
9
+ root = Pathname.new(File.dirname(__FILE__)).split[0]
10
+ ActiveRecord::Base.should_receive(:establish_connection).with(params)
11
+ ActiveRecord::Migrator.should_receive(:migrate).with(File.join(root, 'lib/gritano-core/', '../../db/migrate'), nil)
12
+ Migration.migrate(params)
13
+ end
14
+
15
+ it "should return a migration file" do
16
+ Migration['create_users'].should be == File.open("db/migrate/001_create_users.rb").readlines.join
17
+ Migration['create_keys'].should be == File.open("db/migrate/002_create_keys.rb").readlines.join
18
+ Migration['create_repositories'].should be == File.open("db/migrate/003_create_repositories.rb").readlines.join
19
+ Migration['create_permissions'].should be == File.open("db/migrate/004_create_permissions.rb").readlines.join
20
+ end
21
+
22
+ it "should return nil if the migration desn't exist" do
23
+ Migration['create_wrong_model'].should be == nil
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,55 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ module Gritano::Core
4
+ describe Permission do
5
+ it "should have a contributor" do
6
+ Permission.new(repository_id: 1, access: 1).should be_invalid
7
+ end
8
+
9
+ it "should have a repository" do
10
+ Permission.new(user_id: 1, access: 1).should be_invalid
11
+ end
12
+
13
+ it "should have a access type" do
14
+ Permission.new(user_id: 1, repository_id: 1).should be_invalid
15
+ end
16
+
17
+ it "should add READ access" do
18
+ permission = Permission.new(user_id: 1, repository_id: 1)
19
+ permission.add_access(:read)
20
+ permission.access.should be == Permission::READ
21
+ end
22
+
23
+ it "should add WRITE access" do
24
+ permission = Permission.new(user_id: 1, repository_id: 1)
25
+ permission.add_access(:write)
26
+ permission.access.should be == Permission::WRITE
27
+ end
28
+
29
+ it "should add WRITE access" do
30
+ permission = Permission.new(user_id: 1, repository_id: 1)
31
+ permission.add_access(:write)
32
+ permission.add_access(:read)
33
+ permission.access.should be == Permission::WRITE | Permission::READ
34
+ end
35
+
36
+ it "should remove READ access" do
37
+ permission = Permission.new(user_id: 1, repository_id: 1, access: 3)
38
+ permission.remove_access(:read)
39
+ permission.access.should be == Permission::WRITE
40
+ end
41
+
42
+ it "should remove WRITE access" do
43
+ permission = Permission.new(user_id: 1, repository_id: 1, access: 3)
44
+ permission.remove_access(:write)
45
+ permission.access.should be == Permission::READ
46
+ end
47
+
48
+ it "should remove WRITE access" do
49
+ permission = Permission.new(user_id: 1, repository_id: 1, access: 3)
50
+ permission.remove_access(:write)
51
+ permission.remove_access(:read)
52
+ permission.access.should be == 0
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,54 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ module Gritano::Core
4
+ describe Repository do
5
+
6
+ before(:each) do
7
+ Grit::Repo.stub(:init_bare)
8
+ end
9
+
10
+ it "should be invalid without a name" do
11
+ Repository.new(path: 'path/to/some/folder').should be_invalid
12
+ end
13
+
14
+ it "should be invalid without a path" do
15
+ Repository.new(name: 'my_repo.git').should be_invalid
16
+ end
17
+
18
+ it "should have a unique name" do
19
+ Repository.create(name: 'my_repo.git', path: 'path/to/some/folder')
20
+ Repository.new(name: 'my_repo.git', path: 'path/to/some/folder').should be_invalid
21
+ end
22
+
23
+ it "should have a name that ends with '.git'" do
24
+ Repository.new(name: 'my_repo', path: 'path/to/some/folder').should be_invalid
25
+ end
26
+
27
+ it "can have users" do
28
+ repo = Repository.create(name: 'my_repo.git', path: 'path/to/some/folder')
29
+
30
+ contributor1 = User.create(login: 'jessicaeto')
31
+ contributor2 = User.create(login: 'arybonadio')
32
+ repo.permissions.create(user_id: contributor1.id, access: 0)
33
+ repo.permissions.create(user_id: contributor2.id, access: 0)
34
+ repo.users.count.should be == 2
35
+ end
36
+
37
+ it "should return its full path" do
38
+ repo = Repository.create(name: 'my_repo.git', path: 'path/to/some/folder')
39
+ repo.full_path.should be == "path/to/some/folder/my_repo.git"
40
+ end
41
+
42
+ it "should create a bare repo when it is created" do
43
+ Grit::Repo.should_receive(:init_bare).with('path/to/some/folder/my_repo.git')
44
+ Repository.create(name: 'my_repo.git', path: 'path/to/some/folder')
45
+ end
46
+
47
+ it "should remove the bare repo when it is destroyed" do
48
+ repo = Repository.create(name: 'my_repo.git', path: 'path/to/some/folder')
49
+ FileUtils.should_receive(:rm_r).with('path/to/some/folder/my_repo.git', force: true)
50
+ repo.destroy.should be_true
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,31 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter "/spec/"
4
+ end
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ require 'rspec'
9
+ require 'gritano-core'
10
+ require 'database_cleaner'
11
+ require 'active_record'
12
+
13
+ # Requires supporting files with custom matchers and macros, etc,
14
+ # in ./support/ and its subdirectories.
15
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
16
+
17
+ RSpec.configure do |config|
18
+ config.before(:suite) do
19
+ ActiveRecord::Base.establish_connection(YAML::load(File.open('db/development.yml')))
20
+ DatabaseCleaner.strategy = :transaction
21
+ DatabaseCleaner.clean_with(:truncation)
22
+ end
23
+
24
+ config.before(:each) do
25
+ DatabaseCleaner.start
26
+ end
27
+
28
+ config.after(:each) do
29
+ DatabaseCleaner.clean
30
+ end
31
+ end
data/spec/user_spec.rb ADDED
@@ -0,0 +1,158 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ module Gritano::Core
4
+ describe User do
5
+
6
+ before(:each) do
7
+ Grit::Repo.stub(:init_bare)
8
+ end
9
+
10
+ it "should be invalid without a login" do
11
+ User.new.should be_invalid
12
+ end
13
+
14
+ it "should be valid with a login" do
15
+ User.new(login: 'igorbonadio').should be_valid
16
+ end
17
+
18
+ it "should have an unique login" do
19
+ User.create(login: 'igorbonadio')
20
+ User.new(login: 'igorbonadio').should be_invalid
21
+ end
22
+
23
+ it "is not admin by default" do
24
+ User.new(login: 'igorbonadio').should_not be_admin
25
+ end
26
+
27
+ it "can be admin" do
28
+ User.new(login: 'igorbonadio', admin: true).should be_admin
29
+ end
30
+
31
+ it "can have many keys" do
32
+ user = User.create(login: 'igorbonadio')
33
+ user.keys.create(name: 'my_first_key', key: File.open('spec/key.pub').readlines.join)
34
+ user.keys.create(name: 'my_second_key', key: File.open('spec/key.pub').readlines.join)
35
+ user.keys.count.should be == 2
36
+ end
37
+
38
+ it "can contribute to repositories" do
39
+ user = User.create(login: 'igorbonadio')
40
+ repo = Repository.create(name: 'my_repo.git', path: 'path/to/some/folder')
41
+
42
+ repo.permissions.create(user_id: user.id, access: 0)
43
+ user.repositories.count.should be == 1
44
+ end
45
+
46
+ it "can receive READ access to a reporitory" do
47
+ user = User.create(login: 'igorbonadio')
48
+ repo = Repository.create(name: 'my_repo.git', path: 'path/to/some/folder')
49
+ user.add_access(repo, :read)
50
+ user.check_access(repo, :read).should be_true
51
+ user.check_access(repo, :write).should be_false
52
+ end
53
+
54
+ it "can receive WRITE access to a reporitory" do
55
+ user = User.create(login: 'igorbonadio')
56
+ repo = Repository.create(name: 'my_repo.git', path: 'path/to/some/folder')
57
+ user.add_access(repo, :write)
58
+ user.check_access(repo, :read).should be_false
59
+ user.check_access(repo, :write).should be_true
60
+ end
61
+
62
+ it "can receive READ and WRITE access to a reporitory" do
63
+ user = User.create(login: 'igorbonadio')
64
+ repo = Repository.create(name: 'my_repo.git', path: 'path/to/some/folder')
65
+ user.add_access(repo, :write)
66
+ user.add_access(repo, :read)
67
+ user.check_access(repo, :read).should be_true
68
+ user.check_access(repo, :write).should be_true
69
+ end
70
+
71
+ it "can not receive UNKNOWN access to a reporitory" do
72
+ user = User.create(login: 'igorbonadio')
73
+ repo = Repository.create(name: 'my_repo.git', path: 'path/to/some/folder')
74
+ user.add_access(repo, :wrong_type).should be_false
75
+ end
76
+
77
+ it "should not allow user without permissions" do
78
+ user = User.create(login: 'igorbonadio')
79
+ repo = Repository.create(name: 'my_repo.git', path: 'path/to/some/folder')
80
+ user.check_access(repo, :write).should be_false
81
+ user.check_access(repo, :read).should be_false
82
+ end
83
+
84
+ it "can loose READ access from a READ repository" do
85
+ user = User.create(login: 'igorbonadio')
86
+ repo = Repository.create(name: 'my_repo.git', path: 'path/to/some/folder')
87
+
88
+ user.add_access(repo, :read)
89
+ user.remove_access(repo, :read)
90
+
91
+ user.check_access(repo, :read).should be_false
92
+ user.check_access(repo, :write).should be_false
93
+ end
94
+
95
+ it "can loose READ access from a WRITE repository" do
96
+ user = User.create(login: 'igorbonadio')
97
+ repo = Repository.create(name: 'my_repo.git', path: 'path/to/some/folder')
98
+
99
+ user.add_access(repo, :write)
100
+ user.remove_access(repo, :read)
101
+
102
+ user.check_access(repo, :read).should be_false
103
+ user.check_access(repo, :write).should be_true
104
+ end
105
+
106
+ it "can loose READ access from a READ/WRITE repository" do
107
+ user = User.create(login: 'igorbonadio')
108
+ repo = Repository.create(name: 'my_repo.git', path: 'path/to/some/folder')
109
+
110
+ user.add_access(repo, :read)
111
+ user.add_access(repo, :write)
112
+ user.remove_access(repo, :read)
113
+
114
+ user.check_access(repo, :read).should be_false
115
+ user.check_access(repo, :write).should be_true
116
+ end
117
+
118
+ it "can loose WRITE access from a READ repository" do
119
+ user = User.create(login: 'igorbonadio')
120
+ repo = Repository.create(name: 'my_repo.git', path: 'path/to/some/folder')
121
+
122
+ user.add_access(repo, :read)
123
+ user.remove_access(repo, :write)
124
+
125
+ user.check_access(repo, :read).should be_true
126
+ user.check_access(repo, :write).should be_false
127
+ end
128
+
129
+ it "can loose WRITE access from a WRITE repository" do
130
+ user = User.create(login: 'igorbonadio')
131
+ repo = Repository.create(name: 'my_repo.git', path: 'path/to/some/folder')
132
+
133
+ user.add_access(repo, :write)
134
+ user.remove_access(repo, :write)
135
+
136
+ user.check_access(repo, :read).should be_false
137
+ user.check_access(repo, :write).should be_false
138
+ end
139
+
140
+ it "can loose WRITE access from a READ/WRITE repository" do
141
+ user = User.create(login: 'igorbonadio')
142
+ repo = Repository.create(name: 'my_repo.git', path: 'path/to/some/folder')
143
+
144
+ user.add_access(repo, :read)
145
+ user.add_access(repo, :write)
146
+ user.remove_access(repo, :write)
147
+
148
+ user.check_access(repo, :read).should be_true
149
+ user.check_access(repo, :write).should be_false
150
+ end
151
+
152
+ it "can not loose UNKNOWN access from a reporitory" do
153
+ user = User.create(login: 'igorbonadio')
154
+ repo = Repository.create(name: 'my_repo.git', path: 'path/to/some/folder')
155
+ user.remove_access(repo, :wrong_type).should be_false
156
+ end
157
+ end
158
+ end
metadata ADDED
@@ -0,0 +1,226 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gritano-core
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Igor Bonadio
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 4.0.0.rc1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 4.0.0.rc1
30
+ - !ruby/object:Gem::Dependency
31
+ name: grit
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.5.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.5.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 2.13.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.13.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: rdoc
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 4.0.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 4.0.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: jeweler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 1.8.4
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 1.8.4
94
+ - !ruby/object:Gem::Dependency
95
+ name: simplecov
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 0.7.0
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 0.7.0
110
+ - !ruby/object:Gem::Dependency
111
+ name: sqlite3
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: 1.3.7
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: 1.3.7
126
+ - !ruby/object:Gem::Dependency
127
+ name: database_cleaner
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ~>
132
+ - !ruby/object:Gem::Version
133
+ version: 1.0.1
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ version: 1.0.1
142
+ - !ruby/object:Gem::Dependency
143
+ name: guard-rspec
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ description: Gritano::Core is a library that defines all Gritano's Models. It can
159
+ be used to develop tools that manage users, repositories and permissions.
160
+ email: igorbonadio@gmail.com
161
+ executables: []
162
+ extensions: []
163
+ extra_rdoc_files:
164
+ - LICENSE.txt
165
+ - README.rdoc
166
+ - TODO
167
+ files:
168
+ - .document
169
+ - .rspec
170
+ - .travis.yml
171
+ - Gemfile
172
+ - Gemfile.lock
173
+ - Guardfile
174
+ - LICENSE.txt
175
+ - README.rdoc
176
+ - Rakefile
177
+ - VERSION
178
+ - db/development.yml
179
+ - db/migrate/001_create_users.rb
180
+ - db/migrate/002_create_keys.rb
181
+ - db/migrate/003_create_repositories.rb
182
+ - db/migrate/004_create_permissions.rb
183
+ - gritano-core.gemspec
184
+ - lib/gritano-core.rb
185
+ - lib/gritano-core/key.rb
186
+ - lib/gritano-core/migration.rb
187
+ - lib/gritano-core/permission.rb
188
+ - lib/gritano-core/repository.rb
189
+ - lib/gritano-core/user.rb
190
+ - spec/key.pub
191
+ - spec/key_spec.rb
192
+ - spec/migration_spec.rb
193
+ - spec/permission_spec.rb
194
+ - spec/repository_spec.rb
195
+ - spec/spec_helper.rb
196
+ - spec/user_spec.rb
197
+ - TODO
198
+ homepage: http://github.com/igorbonadio/gritano-core
199
+ licenses:
200
+ - MIT
201
+ post_install_message:
202
+ rdoc_options: []
203
+ require_paths:
204
+ - lib
205
+ required_ruby_version: !ruby/object:Gem::Requirement
206
+ none: false
207
+ requirements:
208
+ - - ! '>='
209
+ - !ruby/object:Gem::Version
210
+ version: '0'
211
+ segments:
212
+ - 0
213
+ hash: 949175185686534598
214
+ required_rubygems_version: !ruby/object:Gem::Requirement
215
+ none: false
216
+ requirements:
217
+ - - ! '>='
218
+ - !ruby/object:Gem::Version
219
+ version: '0'
220
+ requirements: []
221
+ rubyforge_project:
222
+ rubygems_version: 1.8.25
223
+ signing_key:
224
+ specification_version: 3
225
+ summary: Gritano::Core is a library that defines all Gritano's Models.
226
+ test_files: []