Page MenuHomeGRNET

No OneTemporary

File Metadata

Created
Mon, Nov 25, 2:58 AM
diff --git a/app/models/membership.rb b/app/models/membership.rb
index 359c757..bac65e5 100644
--- a/app/models/membership.rb
+++ b/app/models/membership.rb
@@ -1,7 +1,8 @@
class Membership < ActiveRecord::Base
belongs_to :group
belongs_to :user
validates_presence_of :group
validates_presence_of :user
+ validates_uniqueness_of :user_id, scope: :group_id
end
diff --git a/test/factories/group.rb b/test/factories/group.rb
index cd92dc8..03a9cb4 100644
--- a/test/factories/group.rb
+++ b/test/factories/group.rb
@@ -1,5 +1,11 @@
FactoryGirl.define do
factory :group do
sequence(:name) { |n| "group-#{n}" }
+
+ factory :group_with_users do
+ after(:create) do |group|
+ create_list(:user, 2).each { |u| group.users << u }
+ end
+ end
end
end
diff --git a/test/factories/user.rb b/test/factories/user.rb
new file mode 100644
index 0000000..d095dcd
--- /dev/null
+++ b/test/factories/user.rb
@@ -0,0 +1,6 @@
+FactoryGirl.define do
+ factory :user do
+ sequence(:email) { |n| "user-#{n}@example.com" }
+ password 'password'
+ end
+end
diff --git a/test/models/membership_test.rb b/test/models/membership_test.rb
new file mode 100644
index 0000000..f61ed09
--- /dev/null
+++ b/test/models/membership_test.rb
@@ -0,0 +1,13 @@
+require 'test_helper'
+
+class MembershipTest < ActiveSupport::TestCase
+
+ test 'single membership for a group' do
+ group = create(:group_with_users)
+ user = group.users.first
+
+ assert_raise ActiveRecord::RecordInvalid do
+ group.users << user
+ end
+ end
+end

Event Timeline