Skip to content

Commit

Permalink
Fix 400 on ninja badges index (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruioliveira02 authored Jul 25, 2023
1 parent 085d42c commit 1a0baa7
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)
insert(:badge_ninja, badge: badge, ninja: ninja)

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

assert [
%{
"id" => ^badge_id,
"description" => ^badge_description,
"image" => "/images/default_badge.png",
"name" => ^badge_name
}
] = 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 1a0baa7

Please sign in to comment.