Skip to content

Commit 5456c06

Browse files
committed
scopes uniqueness check to the user
- technically we can scope the uniqueness check per user, we do not care if another user has the same token for whatever reason. - also adds a test
1 parent 95d3112 commit 5456c06

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

app/models/expo_push_token.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
class ExpoPushToken < ActiveRecord::Base
22
belongs_to :user
33

4-
validates_presence_of :token
5-
validates_uniqueness_of :token
6-
validates_presence_of :user
4+
validates :token, presence: true, uniqueness: { scope: :user }
5+
validates :user, presence: true
76
end
87

test/integration/expo_push_tokens_test.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ class ExpoPushTokensTest < Redmine::ApiTest::Base
3434
end
3535
end
3636

37+
test 'should not create same token twice' do
38+
assert_difference "ExpoPushToken.count" do
39+
post "/expo_push_tokens.json",
40+
params: @payload,
41+
headers: {"CONTENT_TYPE" => 'application/json'}.merge(@creds)
42+
assert_response 201
43+
post "/expo_push_tokens.json",
44+
params: @payload,
45+
headers: {"CONTENT_TYPE" => 'application/json'}.merge(@creds)
46+
assert_response 422
47+
end
48+
end
49+
3750
test 'should require token' do
3851
assert_no_difference "ExpoPushToken.count" do
3952
post "/expo_push_tokens.json",
@@ -79,7 +92,7 @@ class ExpoPushTokensTest < Redmine::ApiTest::Base
7992
assert_difference "ExpoPushToken.count", -1 do
8093
delete "/expo_push_tokens", params: { user_id: @jsmith.id }
8194
end
82-
assert_redirected_to "/users/#{@jsmith.id}"
95+
assert_redirected_to "/users/#{@jsmith.id}/edit"
8396

8497
assert_raise ActiveRecord::RecordNotFound do
8598
@t2.reload

0 commit comments

Comments
 (0)