diff --git a/Makefile b/Makefile index f89755400..ffa401448 100644 --- a/Makefile +++ b/Makefile @@ -44,6 +44,8 @@ launch.prod: migrate: mix ecto.migrate +migrate.mock: + MIX_ENV=mock mix ecto.migrate rollback: mix ecto.rollback migrate.mock: @@ -63,6 +65,8 @@ gen: @echo "\n" gen.migration: mix ecto.gen.migration $(arg) +gen.migration.mock: + MIX_ENV=mock mix ecto.gen.migration $(arg) gen.context: mix phx.gen.context $(arg) @@ -117,6 +121,8 @@ test.watch: mix test.watch test.watch.wip: mix test.watch --only wip +test.watch.wip2: + mix test.watch --only wip2 test.db_reset: env MIX_ENV=test mix ecto.drop env MIX_ENV=test mix ecto.create diff --git a/lib/mastani_server/accounts/delegates/profile.ex b/lib/mastani_server/accounts/delegates/profile.ex index b45bd8467..29a91c342 100644 --- a/lib/mastani_server/accounts/delegates/profile.ex +++ b/lib/mastani_server/accounts/delegates/profile.ex @@ -112,18 +112,28 @@ defmodule MastaniServer.Accounts.Delegate.Profile do end end - defp create_user(user, :github) do - user = %User{ - nickname: user["login"], - avatar: user["avatar_url"], - bio: user["bio"], - location: user["location"], - email: user["email"], - company: user["company"], + defp create_user(profile, :github) do + attrs = %{ + nickname: profile["login"], + avatar: profile["avatar_url"], + bio: profile["bio"], + location: profile["location"], + email: profile["email"], from_github: true } - Repo.insert(user) + changeset = + case profile |> Map.has_key?("company") do + true -> + %User{} + |> Ecto.Changeset.change(attrs) + |> Ecto.Changeset.put_embed(:work_backgrounds, [%{company: profile["company"]}]) + + false -> + %User{} |> Ecto.Changeset.change(attrs) + end + + Repo.insert(changeset) end defp create_profile(user, github_profile, :github) do diff --git a/lib/mastani_server/accounts/user.ex b/lib/mastani_server/accounts/user.ex index 7f2c8ad86..26c6ebd71 100644 --- a/lib/mastani_server/accounts/user.ex +++ b/lib/mastani_server/accounts/user.ex @@ -21,7 +21,7 @@ defmodule MastaniServer.Accounts.User do alias MastaniServer.CMS @required_fields ~w(nickname avatar)a - @optional_fields ~w(nickname bio sex location email company education qq weichat weibo)a + @optional_fields ~w(nickname bio sex location email qq weichat weibo)a @type t :: %User{} schema "users" do @@ -32,9 +32,6 @@ defmodule MastaniServer.Accounts.User do field(:email, :string) field(:location, :string) - field(:education, :string) - field(:company, :string) - # TODO # field(:twitter, :string) # field(:facebook, :string) @@ -72,7 +69,6 @@ defmodule MastaniServer.Accounts.User do @doc false def changeset(%User{} = user, attrs) do user - |> cast(attrs, @optional_fields ++ @required_fields) |> update_changeset(attrs) |> validate_required(@required_fields) @@ -89,7 +85,6 @@ defmodule MastaniServer.Accounts.User do |> validate_inclusion(:sex, ["dude", "girl"]) |> validate_format(:email, ~r/@/) |> validate_length(:location, min: 2, max: 30) - |> validate_length(:company, min: 3, max: 30) |> validate_length(:qq, min: 8, max: 15) |> validate_length(:weichat, min: 3, max: 30) |> validate_length(:weibo, min: 3, max: 30) diff --git a/lib/mastani_server_web/schema/account/account_misc.ex b/lib/mastani_server_web/schema/account/account_misc.ex index b5afa44a0..d60c50f9d 100644 --- a/lib/mastani_server_web/schema/account/account_misc.ex +++ b/lib/mastani_server_web/schema/account/account_misc.ex @@ -45,9 +45,7 @@ defmodule MastaniServerWeb.Schema.Account.Misc do field(:nickname, :string) field(:bio, :string) field(:sex, :string) - field(:education, :string) field(:location, :string) - field(:company, :string) field(:email, :string) field(:qq, :string) field(:weibo, :string) diff --git a/priv/repo/migrations/20180825231845_remove_education_company_in_users.exs b/priv/repo/migrations/20180825231845_remove_education_company_in_users.exs new file mode 100644 index 000000000..0ed0aad40 --- /dev/null +++ b/priv/repo/migrations/20180825231845_remove_education_company_in_users.exs @@ -0,0 +1,10 @@ +defmodule MastaniServer.Repo.Migrations.RemoveEducationCompanyInUsers do + use Ecto.Migration + + def change do + alter table(:users) do + remove(:company) + remove(:education) + end + end +end diff --git a/test/mastani_server/accounts/accounts_test.exs b/test/mastani_server/accounts/accounts_test.exs index 77ae89af2..787101477 100644 --- a/test/mastani_server/accounts/accounts_test.exs +++ b/test/mastani_server/accounts/accounts_test.exs @@ -35,7 +35,6 @@ defmodule MastaniServer.Test.Accounts do assert updated.sex == attrs.sex end - @tag :wip test "update user education backgorunds with valid attrs" do {:ok, user} = db_insert(:user) @@ -57,7 +56,6 @@ defmodule MastaniServer.Test.Accounts do assert updated.education_backgrounds |> Enum.any?(&(&1.major == "bad ass")) end - @tag :wip test "update user work backgorunds with valid attrs" do {:ok, user} = db_insert(:user) @@ -79,7 +77,6 @@ defmodule MastaniServer.Test.Accounts do assert updated.work_backgrounds |> Enum.any?(&(&1.title == "bad ass")) end - @tag :wip test "update user with invalid attrs fails ?" do {:ok, user} = db_insert(:user) @@ -90,7 +87,7 @@ defmodule MastaniServer.Test.Accounts do end describe "[github login]" do - alias Accounts.{User, GithubUser} + alias Accounts.{GithubUser, User} test "register a valid github user with non-exist in db" do assert {:error, _} = @@ -109,7 +106,10 @@ defmodule MastaniServer.Test.Accounts do assert created_user.bio == @valid_github_profile["bio"] assert created_user.email == @valid_github_profile["email"] - assert created_user.company == @valid_github_profile["company"] + + company_title = created_user.work_backgrounds |> List.first() |> Map.get(:company) + assert company_title == @valid_github_profile["company"] + assert created_user.location == @valid_github_profile["location"] assert created_user.from_github == true diff --git a/test/mastani_server_web/mutation/accounts/account_test.exs b/test/mastani_server_web/mutation/accounts/account_test.exs index 21b6ca9a1..e476733d7 100644 --- a/test/mastani_server_web/mutation/accounts/account_test.exs +++ b/test/mastani_server_web/mutation/accounts/account_test.exs @@ -44,7 +44,6 @@ defmodule MastaniServer.Test.Mutation.Account.Basic do assert updated["nickname"] == "new nickname" end - @tag :wip test "user can update it's own education_backgrounds", ~m(user)a do ownd_conn = simu_conn(:user, user) @@ -75,7 +74,6 @@ defmodule MastaniServer.Test.Mutation.Account.Basic do assert updated["education_backgrounds"] |> Enum.any?(&(&1["major"] == "bad ass")) end - @tag :wip test "user update education_backgrounds with invalid data fails", ~m(user)a do ownd_conn = simu_conn(:user, user) @@ -97,7 +95,6 @@ defmodule MastaniServer.Test.Mutation.Account.Basic do assert ownd_conn |> mutation_get_error?(@update_query, variables) end - @tag :wip test "user can update it's own work backgrounds", ~m(user)a do ownd_conn = simu_conn(:user, user) @@ -128,7 +125,6 @@ defmodule MastaniServer.Test.Mutation.Account.Basic do assert updated["work_backgrounds"] |> Enum.any?(&(&1["title"] == "bad ass")) end - @tag :wip test "user update work backgrounds with invalid data fails", ~m(user)a do ownd_conn = simu_conn(:user, user) diff --git a/test/mastani_server_web/query/accounts/account_test.exs b/test/mastani_server_web/query/accounts/account_test.exs index 6e5bbcab6..c01dd5f19 100644 --- a/test/mastani_server_web/query/accounts/account_test.exs +++ b/test/mastani_server_web/query/accounts/account_test.exs @@ -67,7 +67,6 @@ defmodule MastaniServer.Test.Query.Account.Basic do } } """ - @tag :wip test "guest user can get specific user's info by user's id", ~m(guest_conn user)a do variables = %{id: user.id} results = guest_conn |> query_result(@query, variables, "user")