 
   alias Helix.Account.Factory
 
-  setup_all do
-    {:ok, account: create_account()}
-  end
-
-  defp create_account() do
-    generate_params()
-    |> Account.create_changeset()
-    |> Repo.insert!()
-  end
-
-  # Funnily enough, the very same params can be use to create an account through
-  # the controller and the model
-  defp generate_params do
-    :account
-    |> Factory.build()
-    |> Map.from_struct()
-    |> Map.drop([:display_name, :__meta__])
-  end
-
   describe "account creation" do
     test "succeeds with proper data" do
       params = Factory.params_for(:account)
+
       assert {:ok, _} = AccountController.create(params)
     end
 
-    test "fails when email is already in use", context do
-      account = context.account
-      params = Map.put(generate_params(), :email, account.email)
+    test "fails when email is already in use" do
+      account = Factory.insert(:account)
+      params = %{Factory.params_for(:account) | email: account.email}
 
       assert {:error, changeset} = AccountController.create(params)
       assert :email in Keyword.keys(changeset.errors)
     end
 
-    test "fails when username is already in use", context do
-      account = context.account
-      params = Map.put(generate_params(), :username, account.username)
+    test "fails when username is already in use" do
+      account = Factory.insert(:account)
+      params = %{Factory.params_for(:account) | username: account.username}
 
       assert {:error, changeset} = AccountController.create(params)
       assert :username in Keyword.keys(changeset.errors)
     end
 
     test "fails when password is too short" do
-      params = %{generate_params()| password: "123"}
+      params = %{Factory.params_for(:account) | password: "123"}
 
       assert {:error, changeset} = AccountController.create(params)
       assert :password in Keyword.keys(changeset.errors)

      assert {:error, :notfound} == AccountController.find(Random.pk())
    end

    test "using email", context do
      account = context.account
      assert {:ok, found} = AccountController.find_by(email: account.email)
      assert account.account_id == found.account_id
    end


    test "fails when username is invalid" do
      error = AccountController.login("}<]=inv@líd+(usêr)-nämẽ", "password")

      assert {:error, :notfound} == error
    end

    test "fails when password doesn't match" do
      account = Factory.insert(:account)
      error = AccountController.login(account.email, "incorrect_password")

      assert {:error, :notfound} == error
    end
  end
