Skip to content

Commit

Permalink
create error validation page
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonLab committed Mar 14, 2022
1 parent e7bea54 commit 2f4df0b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/hits_web/controllers/hit_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ defmodule HitsWeb.HitController do
render_invalid_badge(conn)
end
else
render(conn, "index.html", params)
if user_valid?(user) and repository_valid?(repository) do
render(conn, "index.html", params)
else
redirect(conn, to: "/error/#{user}/#{repository}")
end
end
end

Expand Down
4 changes: 4 additions & 0 deletions lib/hits_web/controllers/page_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ defmodule HitsWeb.PageController do
def index(conn, _params) do
render(conn, "index.html")
end

def error(conn, %{"user" => user, "repository" => repository} = params) do
render(conn, "error.html", user: user, repository: repository)
end
end
1 change: 1 addition & 0 deletions lib/hits_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ defmodule HitsWeb.Router do
pipe_through(:browser)

get("/", PageController, :index)
get("/error/:user/:repository", PageController, :error)

get("/:user/:repository", HitController, :index)
get("/:etc/:user/:repository", HitController, :edgecase)
Expand Down
26 changes: 26 additions & 0 deletions lib/hits_web/templates/page/error.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<h2 class="bg-teal white h-25 tc ttu f1 lh-title lh-solid mt0 pa2 pb3 mb0 pb0">
Validation Error!
</h2>

<div class="pa3">
<p>Please make sure the username and the repository values are valid:</p>

<ul>
<li><strong><%=@user%></strong> - username may only contain alphanumeric characters or single hyphens, and cannot begin or end with a hyphen</li>
<li><strong><%=@repository%></strong> - repository may contain alphanumeric values and the following characters: "-", "_" "."</li>
</ul>

<%= link "Create a new badge", to: "/", class: "f6 link dim br2 ph3 pv2 mb2 dib white bg-teal" %>
</div>

<style>
.teal {
color: #4DB6AC;
}
.bg-teal {
background: #4DB6AC;
}
body { /* dwyl font */
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
}
</style>
15 changes: 15 additions & 0 deletions test/hits_web/controllers/page_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,19 @@ defmodule HitsWeb.PageControllerTest do
conn = get(conn, "/")
assert html_response(conn, 200) =~ "Hits!"
end

test "GET /user/repo", %{conn: conn} do
conn = get(conn, "/user/repo")
assert html_response(conn, 200) =~ "dashboard"
end

test "GET /--user/repo$", %{conn: conn} do
conn = get(conn, "/--user/repo$")
assert html_response(conn, 302)
end

test "GET /error/--user/repo$", %{conn: conn} do
conn = get(conn, "/error/--user/repo$")
assert html_response(conn, 200) =~ "Validation Error!"
end
end

0 comments on commit 2f4df0b

Please sign in to comment.