Skip to content

Commit

Permalink
fix/unit tests (#38)
Browse files Browse the repository at this point in the history
* fix: moved  tests to  folders of correspondent context, and  tests to ; Refactored all moved tests to match the changes.

* Latest commit didn't catch all the changes.

* feat: new CI pipeline ✨

* chore: rename modules

* chore: rename modulo_pesquisa module names

* fix: context tests 🐛

Co-authored-by: Zoey de Souza Pessanha <[email protected]>
  • Loading branch information
mooreotavio and Zoey de Souza Pessanha authored May 23, 2022
1 parent 6389f58 commit ef1e2d3
Show file tree
Hide file tree
Showing 79 changed files with 855 additions and 1,210 deletions.
26 changes: 0 additions & 26 deletions .github/workflows/ci.yml

This file was deleted.

215 changes: 215 additions & 0 deletions .github/workflows/elixir.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
name: Elixir CI

on: push

jobs:
deps:
runs-on: ubuntu-latest
env:
MIX_ENV: test
strategy:
matrix:
elixir: [1.13.4]
otp: [24.1.6]
steps:
- name: Cancel Previous Runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}

- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Setup
uses: erlef/setup-beam@v1
with:
elixir-version: ${{ matrix.elixir }}
otp-version: ${{ matrix.otp }}

- name: Retrieve Cached Dependencies
uses: actions/cache@v2
id: mix-cache
with:
path: |
deps
_build
key: ${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-${{ hashFiles('mix.lock') }}

- name: Install Dependencies
run: |
mix local.rebar --force
mix local.hex --force
mix deps.get
mix deps.compile
format:
runs-on: ubuntu-latest
needs: deps
strategy:
matrix:
elixir: [1.13.4]
otp: [24.1.6]
steps:
- name: Cancel Previous Runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}

- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Setup
uses: erlef/setup-beam@v1
with:
elixir-version: ${{ matrix.elixir }}
otp-version: ${{ matrix.otp }}

- name: Retrieve Cached Dependencies
uses: actions/cache@v2
id: mix-cache
with:
path: |
deps
_build
key: ${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-${{ hashFiles('mix.lock') }}

- name: Run Formater
run: mix format --check-formatted

static_code_analysis:
runs-on: ubuntu-latest
needs: deps
strategy:
matrix:
elixir: [1.13.4]
otp: [24.1.6]
steps:
- name: Cancel Previous Runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}

- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Setup
uses: erlef/setup-beam@v1
with:
elixir-version: ${{ matrix.elixir }}
otp-version: ${{ matrix.otp }}

- name: Retrieve Cached Dependencies
uses: actions/cache@v2
id: mix-cache
with:
path: |
deps
_build
key: ${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-${{ hashFiles('mix.lock') }}

- name: Run Credo
run: mix credo --strict

unit_tests:
runs-on: ubuntu-latest
needs: deps
strategy:
matrix:
elixir: [1.13.4]
otp: [24.1.6]
services:
db:
env:
POSTGRES_USER: pescarte
POSTGRES_PASSWORD: pescarte
POSTGRES_DB: fuschia_test
image: postgres:13.5
ports: ['5432:5432']
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Cancel Previous Runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}

- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Setup
uses: erlef/setup-beam@v1
with:
elixir-version: ${{ matrix.elixir }}
otp-version: ${{ matrix.otp }}

- name: Retrieve Cached Dependencies
uses: actions/cache@v2
id: mix-cache
with:
path: |
deps
_build
key: ${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-${{ hashFiles('mix.lock') }}

- name: Run Unit Tests
run: mix test --only unit

integration_tests:
runs-on: ubuntu-latest
needs: deps
strategy:
matrix:
elixir: [1.13.4]
otp: [24.1.6]
services:
db:
env:
POSTGRES_USER: pescarte
POSTGRES_PASSWORD: pescarte
POSTGRES_DB: fuschia_test
image: postgres:13.5
ports: ['5432:5432']
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Cancel Previous Runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}

- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Setup
uses: erlef/setup-beam@v1
with:
elixir-version: ${{ matrix.elixir }}
otp-version: ${{ matrix.otp }}

- name: Retrieve Cached Dependencies
uses: actions/cache@v2
id: mix-cache
with:
path: |
deps
_build
key: ${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-${{ hashFiles('mix.lock') }}

- name: Run Integration Tests
run: mix test --only integration
14 changes: 9 additions & 5 deletions lib/fuschia/accounts/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ defmodule Fuschia.Accounts do

alias Fuschia.Repo

alias Fuschia.Accounts.Logic.{UserLogic, UserTokenLogic}
alias Fuschia.Accounts.Models.{AuthLogModel, UserModel, UserTokenModel}
alias Fuschia.Accounts.Logic.User, as: UserLogic
alias Fuschia.Accounts.Logic.UserToken, as: UserTokenLogic
alias Fuschia.Accounts.Models.AuthLog
alias Fuschia.Accounts.Models.User, as: UserModel
alias Fuschia.Accounts.Models.UserToken, as: UserTokenModel
alias Fuschia.Accounts.Queries
alias Fuschia.Accounts.Queries.{UserQueries, UserTokenQueries}
alias Fuschia.Accounts.Queries.User, as: UserQueries
alias Fuschia.Accounts.Queries.UserToken, as: UserTokenQueries
alias Fuschia.Accounts.UserNotifier
alias Fuschia.Database

Expand Down Expand Up @@ -135,8 +139,8 @@ defmodule Fuschia.Accounts do

@spec create_auth_log(map) :: :ok
def create_auth_log(attrs) do
%AuthLogModel{}
|> AuthLogModel.changeset(attrs)
%AuthLog{}
|> AuthLog.changeset(attrs)
|> Database.insert()

:ok
Expand Down
6 changes: 3 additions & 3 deletions lib/fuschia/accounts/adapters/contato.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule Fuschia.Accounts.Adapters.ContatoAdapter do
defmodule Fuschia.Accounts.Adapters.Contato do
@moduledoc false

alias Fuschia.Accounts.Models.ContatoModel
alias Fuschia.Accounts.Models.Contato

def to_map(%ContatoModel{} = struct) do
def to_map(%Contato{} = struct) do
%{
id: struct.id,
celular: struct.celular,
Expand Down
8 changes: 4 additions & 4 deletions lib/fuschia/accounts/adapters/user.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
defmodule Fuschia.Accounts.Adapters.UserAdapter do
defmodule Fuschia.Accounts.Adapters.User do
@moduledoc """
Define funções que convertem um usuário para
outros schemas ou mapas
"""

alias Fuschia.Accounts.Models.UserModel
alias Fuschia.Accounts.Models.User

def for_jwt(%UserModel{} = struct) do
def for_jwt(%User{} = struct) do
%{
email: struct.contato.email,
endereco: struct.contato.endereco,
Expand All @@ -20,7 +20,7 @@ defmodule Fuschia.Accounts.Adapters.UserAdapter do
}
end

def to_map(%UserModel{} = struct) do
def to_map(%User{} = struct) do
%{
nome_completo: struct.nome_completo,
perfil: struct.role,
Expand Down
2 changes: 1 addition & 1 deletion lib/fuschia/accounts/logic/contato.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Fuschia.Accounts.Logic.ContatoLogic do
defmodule Fuschia.Accounts.Logic.Contato do
@moduledoc false

import Ecto.Changeset,
Expand Down
8 changes: 4 additions & 4 deletions lib/fuschia/accounts/logic/user.ex
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
defmodule Fuschia.Accounts.Logic.UserLogic do
defmodule Fuschia.Accounts.Logic.User do
@moduledoc false

import Ecto.Changeset, only: [add_error: 3]

alias Fuschia.Accounts.Models.UserModel
alias Fuschia.Accounts.Models.User

@doc """
Verifica a senha.
Se não houver usuário ou o usuário não tiver uma senha, chamamos
`Bcrypt.no_user_verify/0` para evitar ataques de tempo.
"""
def valid_password?(%UserModel{password_hash: password_hash}, password)
def valid_password?(%User{password_hash: password_hash}, password)
when is_binary(password_hash) and byte_size(password) > 0 do
Bcrypt.verify_pass(password, password_hash)
end
Expand All @@ -31,7 +31,7 @@ defmodule Fuschia.Accounts.Logic.UserLogic do
end
end

def put_permissions(%UserModel{} = user) do
def put_permissions(%User{} = user) do
# TODO
Map.put(user, :permissoes, nil)
end
Expand Down
8 changes: 4 additions & 4 deletions lib/fuschia/accounts/logic/user_token.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule Fuschia.Accounts.Logic.UserTokenLogic do
defmodule Fuschia.Accounts.Logic.UserToken do
@moduledoc """
Regras de negócio para tokens de Usuário
"""

alias Fuschia.Accounts.Models.UserTokenModel
alias Fuschia.Accounts.Models.UserToken

@hash_algorithm :sha256
@rand_size 32
Expand All @@ -29,7 +29,7 @@ defmodule Fuschia.Accounts.Logic.UserTokenLogic do
"""
def build_session_token(user) do
token = :crypto.strong_rand_bytes(@rand_size)
{token, %UserTokenModel{token: token, context: "session", user_cpf: user.cpf}}
{token, %UserToken{token: token, context: "session", user_cpf: user.cpf}}
end

@doc """
Expand All @@ -55,7 +55,7 @@ defmodule Fuschia.Accounts.Logic.UserTokenLogic do
hashed_token = :crypto.hash(@hash_algorithm, token)

{Base.url_encode64(token, padding: false),
%UserTokenModel{
%UserToken{
token: hashed_token,
context: context,
sent_to: sent_to,
Expand Down
2 changes: 1 addition & 1 deletion lib/fuschia/accounts/models/api_key.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Fuschia.Accounts.Models.ApiKeyModel do
defmodule Fuschia.Accounts.Models.ApiKey do
@moduledoc """
API Key Schema
"""
Expand Down
Loading

0 comments on commit ef1e2d3

Please sign in to comment.