Skip to content

Commit

Permalink
Merge pull request #28 from NunoAlexandre/dev
Browse files Browse the repository at this point in the history
on authorised request to the api, extract the client id. #23
  • Loading branch information
NunoAlexandre committed May 7, 2017
2 parents 83b5a09 + c813169 commit 4e605d4
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 37 deletions.
4 changes: 2 additions & 2 deletions priv/repo/migrations/20170415202037_create_user_day.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ defmodule Broccoli.Repo.Migrations.CreateUserDay do
add :day, :date, null: false
add :level, :level, null: false
add :note, :string, null: false, size: 700, default: ""
add :user_id, :string, null: false
add :uid, :string, null: false


timestamps()
end

create index(:user_day, [:level])
create unique_index(:user_day, [:user_id, :day], name: :single_user_day)
create unique_index(:user_day, [:uid, :day], name: :single_user_day)
end

def down do
Expand Down
36 changes: 24 additions & 12 deletions test/controllers/user_day_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ defmodule Broccoli.UserDayControllerTest do
use Broccoli.ConnCase

alias Broccoli.UserDay
@valid_attrs %{user_id: "5908e4718fcb61np387e23f2", day: %{day: 7, month: 5, year: 2017}, level: :eight, note: "some content"}

@valid_attrs %{day: %{day: 7, month: 5, year: 2017}, level: :eight, note: "some content"}
@invalid_attrs %{day: %{day: 17, month: 4, year: 2010}, level: :seven, note: "some content"}
@valid_auth0_token "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJicm9jY29saS5uYWxleGFuZHJlLnRlc3QiLCJpYXQiOjE0OTQxMDQzOTIsImV4cCI6MzMwODI1NDkxOTIsImF1ZCI6InRlc3QiLCJzdWIiOiJhdXRoMHw1OTA4ZTQ3MThmY2I2MW5wMzg3ZTIzZjIifQ.urFUtu9x6dHH2A4UDAAefIgGZSk_2eTWGE-c1DYTEfs"

Expand All @@ -13,19 +14,30 @@ defmodule Broccoli.UserDayControllerTest do
{:ok, conn: conn}
end

test "lists all entries on index", %{conn: conn} do
test "empty list when fetching all users entries", %{conn: conn} do
conn = get conn, user_day_path(conn, :index)
assert json_response(conn, 200)["data"] == []
end

test "showing all user entries", %{conn: conn} do
inserted_user_day("5908e47otheruserid7e23f2")
user_day = inserted_user_day("5908e4718fcb61np387e23f2")
conn = get conn, user_day_path(conn, :index)
assert json_response(conn, 200)["data"] == [%{"id" => user_day.id,
"day" => to_string(user_day.day),
"level" => to_string(user_day.level),
"note" => user_day.note,
"uid" => user_day.uid}]
end

test "shows chosen resource", %{conn: conn} do
user_day = inserted_user_day()
user_day = inserted_user_day("5908e4718fcb61np387e23f2")
conn = get conn, user_day_path(conn, :show, user_day)
assert json_response(conn, 200)["data"] == %{"id" => user_day.id,
"user_id" => user_day.user_id,
"day" => to_string(user_day.day),
"level" => to_string(user_day.level),
"note" => user_day.note }
assert json_response(conn, 200)["data"] == %{ "id" => user_day.id,
"uid" => user_day.uid,
"day" => to_string(user_day.day),
"level" => to_string(user_day.level),
"note" => user_day.note }
end

test "renders page not found when id is nonexistent", %{conn: conn} do
Expand All @@ -46,19 +58,19 @@ defmodule Broccoli.UserDayControllerTest do
end

test "does not update chosen resource and renders errors when data is invalid", %{conn: conn} do
conn = put conn, user_day_path(conn, :update, inserted_user_day()), user_day: @invalid_attrs
conn = put conn, user_day_path(conn, :update, inserted_user_day("5908e4718fcb61np387e23f2")), user_day: @invalid_attrs
assert json_response(conn, 422)["errors"] != %{}
end

test "deletes chosen resource", %{conn: conn} do
user_day = inserted_user_day()
user_day = inserted_user_day("5908e4718fcb61np387e23f2")
conn = delete conn, user_day_path(conn, :delete, user_day)
assert response(conn, 204)
refute Repo.get(UserDay, user_day.id)
end


defp inserted_user_day do
Repo.insert! Broccoli.UserDay.changeset(%UserDay{}, @valid_attrs())
defp inserted_user_day(uid) do
Repo.insert! UserDay.changeset(%UserDay{uid: uid}, @valid_attrs)
end
end
6 changes: 3 additions & 3 deletions test/models/user_day_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ defmodule Broccoli.UserDayTest do
alias Broccoli.UserDay

@empty_attrs %{}
@valid_attrs %{user_id: "1jkehfgrjdvb", day: %{day: 17, month: 4, year: 2010}, level: :eight, note: "some content"}
@invalid_attrs %{user_id: 837438792, day: %{day: 17, month: 4, year: 2010}, level: :seven, note: "some content"}
@valid_attrs %{uid: "1jkehfgrjdvb", day: %{day: 17, month: 4, year: 2010}, level: :eight, note: "some content"}
@invalid_attrs %{uid: 837438792, day: %{day: 17, month: 4, year: 2010}, level: :seven, note: "some content"}

test "changeset with valid attributes" do
changeset = UserDay.changeset(%UserDay{}, @valid_attrs)
Expand All @@ -17,7 +17,7 @@ defmodule Broccoli.UserDayTest do
refute changeset.valid?
end

test "changeset with incorrect user_id and invalid level" do
test "changeset with incorrect uid and invalid level" do
changeset = UserDay.changeset(%UserDay{}, @invalid_attrs)
refute changeset.valid?
end
Expand Down
4 changes: 2 additions & 2 deletions web/channels/user_socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule Broccoli.UserSocket do
# verification, you can put default assigns into
# the socket that will be set for all channels, ie
#
# {:ok, assign(socket, :user_id, verified_user_id)}
# {:ok, assign(socket, :uid, verified_uid)}
#
# To deny connection, return `:error`.
#
Expand All @@ -26,7 +26,7 @@ defmodule Broccoli.UserSocket do

# Socket id's are topics that allow you to identify all sockets for a given user:
#
# def id(socket), do: "users_socket:#{socket.assigns.user_id}"
# def id(socket), do: "users_socket:#{socket.assigns.uid}"
#
# Would allow you to broadcast a "disconnect" event and terminate
# all active sockets and channels for a given user:
Expand Down
10 changes: 5 additions & 5 deletions web/controllers/user_day_controller.ex
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
defmodule Broccoli.UserDayController do
use Broccoli.Web, :controller

alias Broccoli.UserDay
alias Broccoli.{UserDay, User}

def index(conn, _params) do
user_day = Repo.all(UserDay)
render(conn, "index.json", user_day: user_day)
def index(conn, _) do
user_days = Repo.all(UserDay.by_uid(User.id(conn)))
render(conn, "index.json", user_day: user_days)
end

def create(conn, %{"user_day" => user_day_params}) do
changeset = UserDay.changeset(%UserDay{}, user_day_params)
changeset = UserDay.changeset(%UserDay{uid: User.id(conn)}, user_day_params)

case Repo.insert(changeset) do
{:ok, user_day} ->
Expand Down
13 changes: 8 additions & 5 deletions web/models/user_day.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
defmodule Broccoli.UserDay do
use Broccoli.Web, :model

@derive {Poison.Encoder, only: [:user_id, :day, :note, :level]}
alias Broccoli.UserDay
@derive {Poison.Encoder, only: [:uid, :day, :note, :level]}

schema "user_day" do
field :day, Ecto.Date
field :level, Level
field :note, :string
field :user_id, :string
field :uid, :string


timestamps()
Expand All @@ -16,8 +16,11 @@ defmodule Broccoli.UserDay do

def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:note, :day, :level, :user_id])
|> validate_required([:user_id, :day, :level])
|> cast(params, [:note, :day, :level, :uid])
|> validate_required([:uid, :day, :level])
|> unique_constraint(:single_user_day, name: :single_user_day)
end

def by_uid(uid), do: from ud in UserDay, where: ud.uid == ^uid

end
5 changes: 5 additions & 0 deletions web/modules/user.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule Broccoli.User do

def id(conn), do: Guardian.Plug.current_resource(conn)

end
4 changes: 2 additions & 2 deletions web/static/js/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ let socket = new Socket("/socket", {params: {token: window.userToken}})
// def connect(%{"token" => token}, socket) do
// # max_age: 1209600 is equivalent to two weeks in seconds
// case Phoenix.Token.verify(socket, "user socket", token, max_age: 1209600) do
// {:ok, user_id} ->
// {:ok, assign(socket, :user, user_id)}
// {:ok, uid} ->
// {:ok, assign(socket, :user, uid)}
// {:error, reason} ->
// :error
// end
Expand Down
6 changes: 3 additions & 3 deletions web/templates/user_day/form.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@


<div class="form-group">
<%= label f, :user_id, class: "control-label" %>
<%= text_input f, :user_id, class: "form-control" %>
<%= error_tag f, :user_id %>
<%= label f, :uid, class: "control-label" %>
<%= text_input f, :uid, class: "form-control" %>
<%= error_tag f, :uid %>
</div>

<div class="form-group">
Expand Down
2 changes: 1 addition & 1 deletion web/templates/user_day/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<tbody>
<%= for user_day <- @user_day do %>
<tr>
<td><%= user_day.user_id %></td>
<td><%= user_day.uid %></td>
<td><%= user_day.level %></td>
<td><%= user_day.note %></td>

Expand Down
2 changes: 1 addition & 1 deletion web/templates/user_day/show.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<li>
<strong>User:</strong>
<%= @user_day.user_id %>
<%= @user_day.uid %>
</li>

<li>
Expand Down
2 changes: 1 addition & 1 deletion web/views/user_day_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule Broccoli.UserDayView do

def render("user_day.json", %{user_day: user_day}) do
%{id: user_day.id,
user_id: user_day.user_id,
uid: user_day.uid,
day: user_day.day,
level: user_day.level,
note: user_day.note}
Expand Down

0 comments on commit 4e605d4

Please sign in to comment.