diff --git a/lib/code_corps/accounts/accounts.ex b/lib/code_corps/accounts/accounts.ex index b5f7fa333..cf403cd2a 100644 --- a/lib/code_corps/accounts/accounts.ex +++ b/lib/code_corps/accounts/accounts.ex @@ -157,7 +157,7 @@ defmodule CodeCorps.Accounts do existing_user_ids = User - |> where(github_id: ^github_id) + |> where(github_id_was: ^github_id) |> select([u], u.id) |> Repo.all @@ -174,7 +174,7 @@ defmodule CodeCorps.Accounts do existing_user_ids = User - |> where(github_id: ^github_id) + |> where(github_id_was: ^github_id) |> select([u], u.id) |> Repo.all diff --git a/lib/code_corps/accounts/changesets.ex b/lib/code_corps/accounts/changesets.ex index eb7f9d3ef..34c7124fe 100644 --- a/lib/code_corps/accounts/changesets.ex +++ b/lib/code_corps/accounts/changesets.ex @@ -53,6 +53,7 @@ defmodule CodeCorps.Accounts.Changesets do def dissociate_github_user_changeset(struct, %{} = params) do struct |> Changeset.cast(params, [:github_id, :github_id_was]) + |> Changeset.unique_constraint(:github_id_was) |> unique_github_constraint() end diff --git a/priv/repo/migrations/20171110001134_add_index_for_github_id_was_to_users.exs b/priv/repo/migrations/20171110001134_add_index_for_github_id_was_to_users.exs new file mode 100644 index 000000000..2ec03b341 --- /dev/null +++ b/priv/repo/migrations/20171110001134_add_index_for_github_id_was_to_users.exs @@ -0,0 +1,7 @@ +defmodule CodeCorps.Repo.Migrations.AddIndexForGithubIdWasToUsers do + use Ecto.Migration + + def change do + create unique_index(:users, [:github_id_was]) + end +end diff --git a/priv/repo/structure.sql b/priv/repo/structure.sql index 86c3966a4..c82d7030c 100644 --- a/priv/repo/structure.sql +++ b/priv/repo/structure.sql @@ -3299,6 +3299,13 @@ CREATE UNIQUE INDEX users_email_index ON users USING btree (email); CREATE UNIQUE INDEX users_github_id_index ON users USING btree (github_id); +-- +-- Name: users_github_id_was_index; Type: INDEX; Schema: public; Owner: - +-- + +CREATE UNIQUE INDEX users_github_id_was_index ON users USING btree (github_id_was); + + -- -- Name: users_github_user_id_index; Type: INDEX; Schema: public; Owner: - -- @@ -3868,5 +3875,5 @@ ALTER TABLE ONLY users -- PostgreSQL database dump complete -- -INSERT INTO "schema_migrations" (version) VALUES (20160723215749), (20160804000000), (20160804001111), (20160805132301), (20160805203929), (20160808143454), (20160809214736), (20160810124357), (20160815125009), (20160815143002), (20160816020347), (20160816034021), (20160817220118), (20160818000944), (20160818132546), (20160820113856), (20160820164905), (20160822002438), (20160822004056), (20160822011624), (20160822020401), (20160822044612), (20160830081224), (20160830224802), (20160911233738), (20160912002705), (20160912145957), (20160918003206), (20160928232404), (20161003185918), (20161019090945), (20161019110737), (20161020144622), (20161021131026), (20161031001615), (20161121005339), (20161121014050), (20161121043941), (20161121045709), (20161122015942), (20161123081114), (20161123150943), (20161124085742), (20161125200620), (20161126045705), (20161127054559), (20161205024856), (20161207112519), (20161209192504), (20161212005641), (20161214005935), (20161215052051), (20161216051447), (20161218005913), (20161219160401), (20161219163909), (20161220141753), (20161221085759), (20161226213600), (20161231063614), (20170102130055), (20170102181053), (20170104113708), (20170104212623), (20170104235423), (20170106013143), (20170115035159), (20170115230549), (20170121014100), (20170131234029), (20170201014901), (20170201025454), (20170201035458), (20170201183258), (20170220032224), (20170224233516), (20170226050552), (20170228085250), (20170308214128), (20170308220713), (20170308222552), (20170313130611), (20170318032449), (20170318082740), (20170324194827), (20170424215355), (20170501225441), (20170505224222), (20170526095401), (20170602000208), (20170622205732), (20170626231059), (20170628092119), (20170628213609), (20170629183404), (20170630140136), (20170706132431), (20170707213648), (20170711122252), (20170717092127), (20170725060612), (20170727052644), (20170731130121), (20170814131722), (20170913114958), (20170921014405), (20170925214512), (20170925230419), (20170926134646), (20170927100300), (20170928234412), (20171003134956), (20171003225853), (20171006063358), (20171006161407), (20171012215106), (20171012221231), (20171016125229), (20171016125516), (20171016223356), (20171016235656), (20171017235433), (20171019191035), (20171025184225), (20171026010933), (20171027061833), (20171028011642), (20171028173508), (20171030182857), (20171031232023), (20171031234356), (20171101023309), (20171104013543), (20171106045740), (20171106050209), (20171106103153), (20171106200036), (20171109231538); +INSERT INTO "schema_migrations" (version) VALUES (20160723215749), (20160804000000), (20160804001111), (20160805132301), (20160805203929), (20160808143454), (20160809214736), (20160810124357), (20160815125009), (20160815143002), (20160816020347), (20160816034021), (20160817220118), (20160818000944), (20160818132546), (20160820113856), (20160820164905), (20160822002438), (20160822004056), (20160822011624), (20160822020401), (20160822044612), (20160830081224), (20160830224802), (20160911233738), (20160912002705), (20160912145957), (20160918003206), (20160928232404), (20161003185918), (20161019090945), (20161019110737), (20161020144622), (20161021131026), (20161031001615), (20161121005339), (20161121014050), (20161121043941), (20161121045709), (20161122015942), (20161123081114), (20161123150943), (20161124085742), (20161125200620), (20161126045705), (20161127054559), (20161205024856), (20161207112519), (20161209192504), (20161212005641), (20161214005935), (20161215052051), (20161216051447), (20161218005913), (20161219160401), (20161219163909), (20161220141753), (20161221085759), (20161226213600), (20161231063614), (20170102130055), (20170102181053), (20170104113708), (20170104212623), (20170104235423), (20170106013143), (20170115035159), (20170115230549), (20170121014100), (20170131234029), (20170201014901), (20170201025454), (20170201035458), (20170201183258), (20170220032224), (20170224233516), (20170226050552), (20170228085250), (20170308214128), (20170308220713), (20170308222552), (20170313130611), (20170318032449), (20170318082740), (20170324194827), (20170424215355), (20170501225441), (20170505224222), (20170526095401), (20170602000208), (20170622205732), (20170626231059), (20170628092119), (20170628213609), (20170629183404), (20170630140136), (20170706132431), (20170707213648), (20170711122252), (20170717092127), (20170725060612), (20170727052644), (20170731130121), (20170814131722), (20170913114958), (20170921014405), (20170925214512), (20170925230419), (20170926134646), (20170927100300), (20170928234412), (20171003134956), (20171003225853), (20171006063358), (20171006161407), (20171012215106), (20171012221231), (20171016125229), (20171016125516), (20171016223356), (20171016235656), (20171017235433), (20171019191035), (20171025184225), (20171026010933), (20171027061833), (20171028011642), (20171028173508), (20171030182857), (20171031232023), (20171031234356), (20171101023309), (20171104013543), (20171106045740), (20171106050209), (20171106103153), (20171106200036), (20171109231538), (20171110001134); diff --git a/test/lib/code_corps/accounts/accounts_test.exs b/test/lib/code_corps/accounts/accounts_test.exs index f63220476..29cc0eb6b 100644 --- a/test/lib/code_corps/accounts/accounts_test.exs +++ b/test/lib/code_corps/accounts/accounts_test.exs @@ -4,7 +4,7 @@ defmodule CodeCorps.AccountsTest do use CodeCorps.BackgroundProcessingCase use CodeCorps.DbAccessCase - alias CodeCorps.{Accounts, User, GitHub.TestHelpers} + alias CodeCorps.{Accounts, Comment, Task, User, GitHub.TestHelpers} alias Ecto.Changeset describe "create_from_github/1" do @@ -66,7 +66,7 @@ defmodule CodeCorps.AccountsTest do end describe "update_from_github_oauth/3" do - test "updates proper user from provided payload" do + test "updates proper user and associations given GitHub payload" do user = insert(:user) %{"id" => github_id} = params = TestHelpers.load_endpoint_fixture("user") token = "random_token" @@ -75,6 +75,9 @@ defmodule CodeCorps.AccountsTest do params |> Accounts.create_from_github() + comment = insert(:comment, user: user_for_github_user) + task = insert(:task, user: user_for_github_user) + {:ok, %User{} = user} = user |> Accounts.update_from_github_oauth(params, token) @@ -82,15 +85,24 @@ defmodule CodeCorps.AccountsTest do wait_for_supervisor() user_for_github_user = Repo.get(User, user_for_github_user.id) + comment = Repo.get(Comment, comment.id) + task = Repo.get(Task, task.id) + # Unsets the old user's github_id assert user_for_github_user.sign_up_context == "github" assert user_for_github_user.github_id_was == github_id refute user_for_github_user.github_id + + # Sets the new user data assert user.id assert user.github_auth_token == token assert user.github_id == github_id assert user.sign_up_context == "default" assert user.type == "user" + + # Changes associations + assert comment.user_id == user.id + assert task.user_id == user.id end test "does not update their image if it already exists" do