Invalid byte sequence for encoding "UTF8": 0xb3 error

Create_user function is throwing wire following error.

 `** (Postgrex.Error) ERROR 22021 (character_not_in_repertoire): invalid byte sequence for encoding "UTF8": 0xb3`

Its basic scaffold code generated from context. Error is thrown only while running test, works fine in console or while seeding data.

This is the data I am passing

%{
  "confirmation_token" => "1b66163b-c29a-483e-b37c-a72ac1194a82",
  "email" => "email-0@example.com",
  "fullname" => "User name",
  "password" => "password",
  "password_confirmation" => "password",
  "state" => "incomplete",
  "username" => "username"
}

User schema:

schema "users" do
    field(:about, :string)
    field(:confirmation_sent_at, :string)
    field(:confirmation_token, :binary_id, read_after_write: true)
    field(:confirmed_at, :utc_datetime)
    field(:email, :string)
    field(:failed_attempts, :integer)
    field(:fullname, :string)
    field(:locked_at, :utc_datetime)
    field(:password_hash, :string)
    field(:profile_picture, :string)
    field(:reset_password_sent_at, :string)
    field(:reset_password_token, :string)
    field(:sign_in_count, :integer)
    field(:state, :string, default: "incomplete")
    field(:unlock_token, :string)
    field(:username, :string)
    field(:password, :string, virtual: true)
    field(:password_confirmation, :string, virtual: true)

    timestamps()
  end

Test code

attrs = params_for(:user)
assert {:ok, user} = Account.create_account(attrs)

Create account code

def create_account(attrs, opts \\ []) do
    attrs
    |> Zion.Utils.reduce_map_keys_to_string()
    |> Map.take(["fullname", "email", "password", "password_confirmation", "username"])
    |> add_state(opts)
    |> add_confirmation_token
    |> UserRepo.create_user()
    |> send_confirmation_email(opts)
  end

Anyone???

We need some more information to understand what is going on. What is in your UserRepo.create_user function? Also exactly what input is being passed that function? Also what are the postgres types of the fields in your users table?

Any chance you are using bcrypt_elixir through comeonin? This is a long standing issue that only seems to show up in testing environments.

There is an outstanding issue that has a ton of detail. They seem to be close to fixing it, but the issue is still open.

One workaround I have used is a hard-coded encrypted password value. That prevents bcrypt from encrypting in the test environment and everything runs smoothly.

Thanks, that was the issue.