Skip to content

Commit

Permalink
Fix badge 400 and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ruioliveira02 committed Jul 6, 2023
1 parent 57b7d57 commit 1514762
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
6 changes: 1 addition & 5 deletions lib/bokken/uploaders/emblem.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ defmodule Bokken.Uploaders.Emblem do

# Provide a default URL if there hasn't been a file uploaded
def default_url(_version, _scope) do
base_url() <> "/images/default_badge.png"
end

defp base_url do
Application.fetch_env!(:waffle, :asset_host)
"/images/default_badge.png"
end
end
2 changes: 1 addition & 1 deletion lib/bokken_web/controllers/badge_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule BokkenWeb.BadgeController do

action_fallback BokkenWeb.FallbackController

def index(conn, params) when is_map_key(params, :ninja_id) do
def index(conn, params) when is_map_key(params, "ninja_id") do
badges = Gamification.list_badges(params)
render(conn, "index.json", badges: badges)
end
Expand Down
18 changes: 18 additions & 0 deletions test/bokken_web/controllers/badge_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule BokkenWeb.BadgeControllerTest do
use BokkenWeb.ConnCase

import Bokken.GamificationFixtures
import Bokken.Factory

alias Bokken.Gamification.Badge

Expand Down Expand Up @@ -41,6 +42,23 @@ defmodule BokkenWeb.BadgeControllerTest do
conn = get(conn, Routes.badge_path(conn, :index))
assert json_response(conn, 200)["data"] == []
end

test "lists all badges of ninja", %{conn: conn} do
ninja = insert(:ninja)
%{id: badge_id, description: badge_description, name: badge_name} = badge = insert(:badge)

Check warning on line 48 in test/bokken_web/controllers/badge_controller_test.exs

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.14.x

variable "badge_description" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 48 in test/bokken_web/controllers/badge_controller_test.exs

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.14.x

variable "badge_id" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 48 in test/bokken_web/controllers/badge_controller_test.exs

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.14.x

variable "badge_name" is unused (if the variable is not meant to be used, prefix it with an underscore)
badge_ninja = insert(:badge_ninja, badge: badge, ninja: ninja)

Check warning on line 49 in test/bokken_web/controllers/badge_controller_test.exs

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.14.x

variable "badge_ninja" is unused (if the variable is not meant to be used, prefix it with an underscore)

conn = get(conn, Routes.ninja_badge_path(conn, :index, ninja.id))

assert [
%{
"id" => badge_id,

Check warning on line 55 in test/bokken_web/controllers/badge_controller_test.exs

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.14.x

variable "badge_id" is unused (there is a variable with the same name in the context, use the pin operator (^) to match on it or prefix this variable with underscore if it is not meant to be used)
"description" => badge_description,

Check warning on line 56 in test/bokken_web/controllers/badge_controller_test.exs

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.14.x

variable "badge_description" is unused (there is a variable with the same name in the context, use the pin operator (^) to match on it or prefix this variable with underscore if it is not meant to be used)
"image" => "/images/default_badge.png",
"name" => badge_name

Check warning on line 58 in test/bokken_web/controllers/badge_controller_test.exs

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.14.x

variable "badge_name" is unused (there is a variable with the same name in the context, use the pin operator (^) to match on it or prefix this variable with underscore if it is not meant to be used)
}
] = json_response(conn, 200)["data"]
end
end

describe "create badge" do
Expand Down
26 changes: 26 additions & 0 deletions test/support/factories/gamification_factory.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule Bokken.Factories.GamificationFactory do
@moduledoc """
A factory to generate gamification related structs
"""
defmacro __using__(_opts) do
quote do
alias Bokken.Accounts.Ninja
alias Bokken.Gamification.{Badge, BadgeNinja}

def badge_factory do
%Badge{
name: Faker.Commerce.product_name(),
description: Faker.Lorem.sentence(),
image: nil
}
end

def badge_ninja_factory do
%BadgeNinja{
badge: build(:badge),
ninja: build(:ninja)
}
end
end
end
end
3 changes: 2 additions & 1 deletion test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Bokken.Factory do
AccountFactory,
TokenFactory,
CurriculumFactory,
EventFactory
EventFactory,
GamificationFactory
}
end

0 comments on commit 1514762

Please sign in to comment.