|
| 1 | +defmodule SWAPIWeb.WookieeEncoderTest do |
| 2 | + use SWAPIWeb.ConnCase, async: true |
| 3 | + |
| 4 | + import SWAPI.PlanetsFixtures |
| 5 | + |
| 6 | + alias SWAPIWeb.WookieeEncoder |
| 7 | + |
| 8 | + describe "translate_to_wookie/1" do |
| 9 | + test "converts lowercase letters" do |
| 10 | + assert "aoacwo rqhuahoaor rhrcooohwh wwook shhuscakc oohoworc aoacwo anraufro waoorr" = |
| 11 | + WookieeEncoder.translate_to_wookiee("the quick brown fox jumps over the lazy dog") |
| 12 | + end |
| 13 | + |
| 14 | + test "leaves JSON special characters alone" do |
| 15 | + test_string = "\\{}\"\'[]:" |
| 16 | + assert ^test_string = WookieeEncoder.translate_to_wookiee(test_string) |
| 17 | + end |
| 18 | + |
| 19 | + test "leaves other non-lowercase characters alone" do |
| 20 | + assert "Twocao. 12345" = WookieeEncoder.translate_to_wookiee("Test. 12345") |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + describe "call/2" do |
| 25 | + setup do |
| 26 | + %{id: planet_id} = |
| 27 | + planet_fixture(%{ |
| 28 | + name: "Tatooine" |
| 29 | + }) |
| 30 | + |
| 31 | + {:ok, %{planet_id: planet_id}} |
| 32 | + end |
| 33 | + |
| 34 | + test "converts JSON to wookiee if the format is wookiee", %{conn: conn, planet_id: planet_id} do |
| 35 | + conn = get(conn, ~p"/api/planets/#{planet_id}", format: "wookiee") |
| 36 | + assert %{"whrascwo" => "Traaoooooahwhwo"} = json_response(conn, 200) |
| 37 | + end |
| 38 | + |
| 39 | + test "does not modify the response if the format is not specified", %{ |
| 40 | + conn: conn, |
| 41 | + planet_id: planet_id |
| 42 | + } do |
| 43 | + conn = get(conn, ~p"/api/planets/#{planet_id}") |
| 44 | + assert %{"name" => "Tatooine"} = json_response(conn, 200) |
| 45 | + end |
| 46 | + |
| 47 | + test "does not modify the response if the format is json", %{conn: conn, planet_id: planet_id} do |
| 48 | + conn = get(conn, ~p"/api/planets/#{planet_id}", format: "json") |
| 49 | + assert %{"name" => "Tatooine"} = json_response(conn, 200) |
| 50 | + end |
| 51 | + |
| 52 | + test "does not modify the response if the request fails", %{conn: conn} do |
| 53 | + conn = get(conn, ~p"/api/planets/123456789", format: "wookiee") |
| 54 | + assert %{"detail" => "Not Found"} = json_response(conn, 404) |
| 55 | + end |
| 56 | + end |
| 57 | +end |
0 commit comments