So I have a module that has a private method.
defp encrypt_token(token) do
:crypto.hmac(:sha256, BeffectWeb.Endpoint.config(:secret_key_base), token)
|> Base.encode16(case: :lower)
end
its used in a method that I would like to write a test for.
def find_invite!(invite_token) do
Repo.get_by!(User, invite_token: encrypt_token(invite_token))
end
My test would need to create a fake user who has a invite_token that was encrypted the same way the private encrypt_token/1
encodes
test "find_invite!/1 returns a user with a given invite_token" do
token = "SHOULD_BE_HMAC"
# token needs to be encrypted the same way as the encrypt_token
# when passing it to the fixture, but I can't invoke the private method encrypt_token/1
user = user_fixture(invite_token: token) # invite_token: encrypt_token(token)
assert Accounts.find_invite!(token) == user
end
My question is what is the best pattern for this since I don’t want to test private functions but I do want to make sure my test uses the same methods as the real method so that way my tests don’t become brittle.