File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed
Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,9 @@ defmodule Cog.Models.ChatHandle do
2525
2626 model
2727 |> cast ( params , @ required_fields , @ optional_fields )
28+ |> unique_constraint ( :handle ,
29+ name: :chat_handles_provider_id_handle_index ,
30+ message: "Another user has claimed this chat handle" )
2831 end
2932
3033 # Normally we would use an `update_change` call for something like this. But,
Original file line number Diff line number Diff line change 1+ defmodule Cog.Models.ChatHandleRepoTest do
2+ @ moduledoc """
3+ Test side-effecting ChatHandle model code
4+ """
5+ use Cog.ModelCase , async: false
6+ alias Cog.Models.ChatHandle
7+
8+ test "chat handles are unique for a provider" do
9+ first = user ( "first_user" )
10+ second = user ( "second_user" )
11+
12+
13+ attrs = % { "handle" => "test_handle" ,
14+ "provider_id" => 1 ,
15+ "chat_provider_user_id" => "my_slack_id" }
16+
17+
18+ assert % ChatHandle { } = % ChatHandle { }
19+ |> ChatHandle . changeset ( Map . merge ( attrs , % { "user_id" => first . id } ) )
20+ |> Repo . insert!
21+
22+ { :error , changeset } = % ChatHandle { }
23+ |> ChatHandle . changeset ( Map . merge ( attrs , % { "user_id" => second . id } ) )
24+ |> Repo . insert
25+
26+ assert { :handle , { "Another user has claimed this chat handle" , [ ] } } in changeset . errors
27+ end
28+
29+ end
You can’t perform that action at this time.
0 commit comments