Skip to content

Commit

Permalink
alteracoes nos model de varias tabelas
Browse files Browse the repository at this point in the history
  • Loading branch information
sahudy committed Feb 9, 2024
1 parent 181c5f5 commit 5ffaf11
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lib/pescarte/modulo_pesquisa/repository.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ defmodule Pescarte.ModuloPesquisa.Repository do

@impl true
def fetch_midia_by_id_publico(id) do
case Pescarte.Database.fetch_by(Midia, id_publico: id) do
case Pescarte.Database.fetch_by(Midia, id: id) do
{:ok, midia} -> {:ok, Repo.replica().preload(midia, :tags)}
error -> error
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pescarte_web/graphql/resolver/login.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule PescarteWeb.GraphQL.Resolver.Login do
def resolve(%{input: %{cpf: cpf, senha: password}}, _resolution) do
case UsuarioHandler.fetch_usuario_by_cpf_and_password(cpf, password) do
{:ok, user} ->
token = Phoenix.Token.sign(PescarteWeb.Endpoint, @token_salt, user.id_publico)
token = Phoenix.Token.sign(PescarteWeb.Endpoint, @token_salt, user.id)
{:ok, %{usuario: user, token: token}}

error ->
Expand Down
10 changes: 8 additions & 2 deletions test/pescarte/identidades/handlers/usuario_handler_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,21 @@ defmodule Identidades.Handlers.UsuarioHandlerTest do
UsuarioHandler.fetch_usuario_by_email_and_password("123", user.senha)

assert {:error, :invalid_password} =
UsuarioHandler.fetch_usuario_by_email_and_password(user.contato.email_principal, "123")
UsuarioHandler.fetch_usuario_by_email_and_password(
user.contato.email_principal,
"123"
)
end

test "quando o email e a senha são válidos" do
# user = insert(:usuario)
user = Repo.preload(insert(:usuario), :contato)

assert {:ok, fetched} =
UsuarioHandler.fetch_usuario_by_email_and_password(user.contato.email_principal, user.senha)
UsuarioHandler.fetch_usuario_by_email_and_password(
user.contato.email_principal,
user.senha
)

assert fetched.id == user.id
assert fetched.cpf == user.cpf
Expand Down
4 changes: 2 additions & 2 deletions test/pescarte/identidades/models/contato_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule Identidades.Models.ContatoTest do
celular_principal: "22123456789",
emails_adicionais: ["[email protected]", "[email protected]"],
celulares_adicionais: ["22987654321", "22987654322"],
endereco_id: endereco.cep
endereco_id: endereco.id
}

changeset = Contato.changeset(%Contato{}, attrs)
Expand All @@ -25,7 +25,7 @@ defmodule Identidades.Models.ContatoTest do
assert get_change(changeset, :celular_principal) == "22123456789"
assert get_change(changeset, :emails_adicionais) == ["[email protected]", "[email protected]"]
assert get_change(changeset, :celulares_adicionais) == ["22987654321", "22987654322"]
assert get_change(changeset, :endereco_id) == endereco.cep
assert get_change(changeset, :endereco_id) == endereco.id
end

test "changeset com emails e celulares adicionais vazios" do
Expand Down
6 changes: 3 additions & 3 deletions test/pescarte_web/authentication_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ defmodule PescarteWeb.AuthenticationTest do
|> put_session(:user_token, user_token)
|> Authentication.fetch_current_user([])

assert conn.assigns.current_user.id_publico == user.id_publico
assert conn.assigns.current_user.id == user.id
end

test "autentica o usuário a partir dos cookies", %{conn: conn, user: user} do
Expand All @@ -123,7 +123,7 @@ defmodule PescarteWeb.AuthenticationTest do
|> put_req_cookie(@remember_me_cookie, signed_token)
|> Authentication.fetch_current_user([])

assert conn.assigns.current_user.id_publico == user.id_publico
assert conn.assigns.current_user.id == user.id
assert get_session(conn, :user_token) == user_token

assert get_session(conn, :live_socket_id) ==
Expand All @@ -150,7 +150,7 @@ defmodule PescarteWeb.AuthenticationTest do
{:cont, updated_socket} =
Authentication.on_mount(:mount_current_user, %{}, session, %LiveView.Socket{})

assert updated_socket.assigns.current_user.id_publico == user.id_publico
assert updated_socket.assigns.current_user.id == user.id
end

test "se o token for invalido, atribui nil a current_user", %{conn: conn} do
Expand Down
34 changes: 17 additions & 17 deletions test/pescarte_web/graphql/midia_schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ defmodule PescarteWeb.GraphQL.MidiaSchemaTest do
autor = insert(:usuario)
categoria = insert(:categoria)
tag = insert(:tag, categoria_nome: categoria.nome)
midia = insert(:midia, autor_id: autor.id_publico, tags: [tag])
midia = insert(:midia, autor_id: autor.id, tags: [tag])

conn = post(conn, "/api", %{"query" => @list_midias_query})

assert %{"data" => %{"listarMidias" => [listed]}} = json_response(conn, 200)
assert listed["id"] == midia.id_publico
assert listed["id"] == midia.id
assert listed["nomeArquivo"] == midia.nome_arquivo
assert listed["autor"]["id"] == autor.id_publico
assert listed["autor"]["id"] == autor.id

[tag_listed] = listed["tags"]
assert tag_listed["id"] == tag.id_publico
Expand Down Expand Up @@ -91,18 +91,18 @@ defmodule PescarteWeb.GraphQL.MidiaSchemaTest do
autor = insert(:usuario)
categoria = insert(:categoria)
tag = insert(:tag, categoria_nome: categoria.nome)
midia = insert(:midia, autor_id: autor.id_publico, tags: [tag])
midia = insert(:midia, autor_id: autor.id, tags: [tag])

conn =
post(conn, "/api", %{
"query" => @get_midia_query,
"variables" => %{"id" => midia.id_publico}
"variables" => %{"id" => midia.id}
})

assert %{"data" => %{"buscarMidia" => fetched}} = json_response(conn, 200)
assert fetched["id"] == midia.id_publico
assert fetched["id"] == midia.id
assert fetched["nomeArquivo"] == midia.nome_arquivo
assert fetched["autor"]["id"] == autor.id_publico
assert fetched["autor"]["id"] == autor.id

[tag_listed] = fetched["tags"]
assert tag_listed["id"] == tag.id_publico
Expand Down Expand Up @@ -146,7 +146,7 @@ defmodule PescarteWeb.GraphQL.MidiaSchemaTest do
autor = insert(:usuario)

params = %{
"autorId" => autor.id_publico,
"autorId" => autor.id,
"nomeArquivo" => "imagem.png",
"link" => "http://localhost:4000/imagem.png",
"tipo" => "IMAGEM",
Expand All @@ -172,7 +172,7 @@ defmodule PescarteWeb.GraphQL.MidiaSchemaTest do
assert %{"data" => %{"criarMidia" => created}} = json_response(conn, 200)
assert created["id"]
assert created["nomeArquivo"] == params["nomeArquivo"]
assert created["autor"]["id"] == autor.id_publico
assert created["autor"]["id"] == autor.id
assert length(created["tags"]) == 2
end
end
Expand Down Expand Up @@ -209,7 +209,7 @@ defmodule PescarteWeb.GraphQL.MidiaSchemaTest do
conn =
post(conn, "/api", %{
"query" => @remove_midia_tags_mutation,
"variables" => %{"input" => %{"midiaId" => midia.id_publico, "tagsId" => []}}
"variables" => %{"input" => %{"midiaId" => midia.id, "tagsId" => []}}
})

assert %{"data" => %{"removeMidiaTags" => []}} = json_response(conn, 200)
Expand All @@ -221,7 +221,7 @@ defmodule PescarteWeb.GraphQL.MidiaSchemaTest do
conn =
post(conn, "/api", %{
"query" => @remove_midia_tags_mutation,
"variables" => %{"input" => %{"midiaId" => midia.id_publico, "tagsId" => ["123"]}}
"variables" => %{"input" => %{"midiaId" => midia.id, "tagsId" => ["123"]}}
})

assert %{"data" => %{"removeMidiaTags" => []}} = json_response(conn, 200)
Expand All @@ -236,7 +236,7 @@ defmodule PescarteWeb.GraphQL.MidiaSchemaTest do
"query" => @remove_midia_tags_mutation,
"variables" => %{
"input" => %{
"midiaId" => midia.id_publico,
"midiaId" => midia.id,
"tagsId" => Enum.map(tags, & &1.id_publico)
}
}
Expand All @@ -245,7 +245,7 @@ defmodule PescarteWeb.GraphQL.MidiaSchemaTest do
assert %{"data" => %{"removeMidiaTags" => []}} = json_response(conn, 200)

# Logo, as tags não pertencem mais a Midia
assert {:ok, fetched} = MidiasHandler.fetch_midia(midia.id_publico)
assert {:ok, fetched} = MidiasHandler.fetch_midia(midia.id)
assert Enum.empty?(fetched.tags)

# Porém elas ainda existem
Expand Down Expand Up @@ -285,7 +285,7 @@ defmodule PescarteWeb.GraphQL.MidiaSchemaTest do
conn =
post(conn, "/api", %{
"query" => @add_midia_tags_mutation,
"variables" => %{"input" => %{"midiaId" => midia.id_publico, "tagsId" => []}}
"variables" => %{"input" => %{"midiaId" => midia.id, "tagsId" => []}}
})

assert %{"data" => %{"adicionaMidiaTags" => []}} = json_response(conn, 200)
Expand All @@ -297,7 +297,7 @@ defmodule PescarteWeb.GraphQL.MidiaSchemaTest do
conn =
post(conn, "/api", %{
"query" => @add_midia_tags_mutation,
"variables" => %{"input" => %{"midiaId" => midia.id_publico, "tagsId" => ["123"]}}
"variables" => %{"input" => %{"midiaId" => midia.id, "tagsId" => ["123"]}}
})

assert %{"data" => %{"adicionaMidiaTags" => []}} = json_response(conn, 200)
Expand All @@ -312,7 +312,7 @@ defmodule PescarteWeb.GraphQL.MidiaSchemaTest do
"query" => @add_midia_tags_mutation,
"variables" => %{
"input" => %{
"midiaId" => midia.id_publico,
"midiaId" => midia.id,
"tagsId" => Enum.map(tags, & &1.id_publico)
}
}
Expand All @@ -322,7 +322,7 @@ defmodule PescarteWeb.GraphQL.MidiaSchemaTest do
assert %{"data" => %{"adicionaMidiaTags" => ^tags_result}} = json_response(conn, 200)

# Logo, as tags devem ser adicionadas à Midia
assert {:ok, fetched} = MidiasHandler.fetch_midia(midia.id_publico)
assert {:ok, fetched} = MidiasHandler.fetch_midia(midia.id)
assert length(fetched.tags) == 2
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/support/conn_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ defmodule PescarteWeb.ConnCase do
"""
def register_and_generate_jwt_token(%{conn: conn}) do
user = Pescarte.Fixtures.insert(:usuario)
token = Phoenix.Token.sign(PescarteWeb.Endpoint, @token_salt, user.id_publico)
token = Phoenix.Token.sign(PescarteWeb.Endpoint, @token_salt, user.id)
%{conn: Plug.Conn.put_req_header(conn, "authorization", "Bearer " <> token), user: user}
end

Expand Down

0 comments on commit 5ffaf11

Please sign in to comment.