...
 
Commits (4)
......@@ -426,7 +426,7 @@ defmodule Webapp.Machines do
machine.last_status != "Bootloader"
:stop ->
machine.last_status == "Running"
machine.last_status == "Running" || machine.last_status == "Bootloader"
:poweroff ->
machine.last_status != "Stopped" && machine.last_status != "Creating"
......
......@@ -14,7 +14,7 @@ defmodule Webapp.Sessions do
"""
def list_sessions(%User{} = user) do
sessions = Repo.preload(user, :sessions).sessions
Enum.filter(sessions, &(&1.expires_at > DateTime.utc_now()))
Enum.filter(sessions, &(DateTime.compare(&1.expires_at,DateTime.utc_now()) == :gt))
end
@doc """
......
......@@ -16,7 +16,7 @@ defmodule WebappWeb.TeamContext do
_opts
) do
case Enum.find(user.teams, fn user_team -> team.id == user_team.id end) do
team ->
%Team{} = team ->
member = Enum.find(user.memberships, fn membership -> membership.team_id == team.id end)
conn
......@@ -26,6 +26,7 @@ defmodule WebappWeb.TeamContext do
conn
|> put_flash(:error, "You are not authorized to view this page")
|> redirect(to: build_path(conn.path_info))
|> halt()
end
end
......
......@@ -20,7 +20,7 @@ defmodule WebappWeb.AuthCase do
team_namespace: "team_" <> name
}
{:ok, %{user: user}} = Accounts.register_user(user)
{:ok, %{user: user, team: team}} = Accounts.register_user(user)
user
end
......
......@@ -22,6 +22,7 @@ defmodule WebappWeb.ConnCase do
alias WebappWeb.Router.Helpers, as: Routes
import Webapp.TestHelpers
import WebappWeb.ViewHelpers
# The default endpoint for testing
@endpoint WebappWeb.Endpoint
......
......@@ -4,50 +4,63 @@ defmodule Webapp.AccountsTeamTest do
alias Webapp.Accounts
alias Webapp.Accounts.Team
@valid_team %{name: "some name", namespace: %{namespace: "another-company"}}
@valid_team %{name: "some name", namespace: "another-company"}
@invalid_team %{name: nil}
@update_team %{name: "some updated name"}
def team_fixture(attrs \\ %{}) do
{:ok, team} =
attrs
|> Enum.into(@valid_team)
|> Accounts.create_team()
{:ok, fixtures} = Accounts.register_user(%{
user_email: "fred@example.com",
user_password: "reallyHard2gue$$",
user_name: "fred",
team_name: "company",
team_namespace: "company"
})
team
# {:ok, team} =
# attrs
# |> Enum.into(@valid_team)
# |> Accounts.create_team()
fixtures
end
# test "list_teams/0 returns all teams" do
# team = team_fixture()
# assert Accounts.list_teams([:namespace]) == [team]
# end
#
# test "get_team!/1 returns the team with given id" do
# team = team_fixture()
# assert Accounts.get_team!(team.id, [:namespace]) == team
# end
#
# test "create_team/1 with valid data creates a team" do
# assert {:ok, %Team{} = team} = Accounts.create_team(@valid_team)
# assert team.name == "some name"
# end
#
# test "create_team/1 with invalid data returns error changeset" do
# assert {:error, %Ecto.Changeset{}} = Accounts.create_team(@invalid_team)
# end
#
# test "update_team/2 with valid data updates the team" do
# team = team_fixture()
# assert {:ok, %Team{} = team} = Accounts.update_team(team, @update_team)
# assert team.name == "some updated name"
# end
#
# test "update_team/2 with invalid data returns error changeset" do
# team = team_fixture()
# assert {:error, %Ecto.Changeset{}} = Accounts.update_team(team, @invalid_team)
# assert team == Accounts.get_team!(team.id, [:namespace])
# end
test "list_teams/0 returns all teams" do
%{team: team} = team_fixture()
assert Accounts.list_teams() == [team]
end
test "get_team!/1 returns the team with given id" do
%{team: team} = team_fixture()
assert Accounts.get_team!(team.id) == team
end
test "create_team/1 with valid data creates a team" do
%{team: team, user: user} = team_fixture()
team_params =
Map.merge(@valid_team, %{members: [%{user_id: user.id, role: "Administrator"}]})
assert {:ok, %Team{} = team} = Accounts.create_team(team_params)
assert team.name == "some name"
end
test "create_team/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Accounts.create_team(@invalid_team)
end
test "update_team/2 with valid data updates the team" do
%{team: team, user: user} = team_fixture()
assert {:ok, %Team{} = team} = Accounts.update_team(team, @update_team)
assert team.name == "some updated name"
end
test "update_team/2 with invalid data returns error changeset" do
%{team: team, user: user} = team_fixture()
assert {:error, %Ecto.Changeset{}} = Accounts.update_team(team, @invalid_team)
assert team == Accounts.get_team!(team.id)
end
#
# test "delete_team/1 deletes the team" do
# team = team_fixture()
......@@ -55,8 +68,8 @@ defmodule Webapp.AccountsTeamTest do
# assert_raise Ecto.NoResultsError, fn -> Accounts.get_team!(team.id) end
# end
#
# test "change_team/1 returns a team changeset" do
# team = team_fixture()
# assert %Ecto.Changeset{} = Accounts.change_team(team)
# end
test "change_team/1 returns a team changeset" do
%{team: team, user: user} = team_fixture()
assert %Ecto.Changeset{} = Accounts.change_team(team)
end
end
defmodule WebappWeb.MachineControllerTest do
use WebappWeb.ConnCase
import WebappWeb.AuthCase
alias Webapp.Hypervisors
@create_attrs %{name: "some name", template: "some template"}
@update_attrs %{name: "some updated name", template: "some updated template"}
@invalid_attrs %{name: nil, template: nil}
# setup do
# conn = build_conn() |> bypass_through(WebappWeb.Router, [:browser]) |> get("/")
# user = add_user("user@example.com")
# conn_user =
# conn
# |> add_session(user)
# |> send_resp(:ok, "/")
# admin = add_admin("admin@example.com")
# conn_anon =
# conn
# |> send_resp(:ok, "/")
# {:ok, conn: conn, conn_anon: conn_anon}
# end
# describe "index" do
# test "lists all machines", %{conn: conn} do
# conn = get(conn, team_path(:machine_path, conn, :index))
......
defmodule WebappWeb.TeamContextTest do
use WebappWeb.ConnCase
import WebappWeb.AuthCase
alias Webapp.{
Accounts
}
setup %{conn: conn} do
# prepare connection
conn = conn
|> bypass_through(WebappWeb.Router, [:browser_auth])
|> get("/")
# create user
user = add_user_confirmed("user@example.com")
another_user = add_user_confirmed("another_user@example.com")
# create team
{:ok, team} = Accounts.create_team(%{
name: "some name",
namespace: "user-company",
members: [%{"user_id" => user.id, "role" => "Administrator"}]
})
{:ok, another_team} = Accounts.create_team(%{
name: "other name",
namespace: "other-company",
members: [%{"user_id" => another_user.id, "role" => "Administrator"}]
})
conn = conn |> add_session(user) |> send_resp(:ok, "/")
{:ok, %{conn: conn, team: team, user: user}}
end
test "first user team is assigned as current_team if namespace is not provided in url", %{conn: conn, user: user} do
conn = get(conn, "/")
team = Accounts.user_first_team(user)
assert conn.assigns[:current_team] == team
end
test "current_team is taken form url", %{conn: conn, user: user, team: team} do
conn = get(conn, "/user-company/machines")
# current_team is loaded without members
team = Accounts.get_team!(team.id)
assert conn.assigns[:current_team] == team
end
test "user can not access other teams", %{conn: conn, user: user, team: team} do
conn = get(conn, "/other-company/machines")
assert conn.halted
assert conn.status == 302
assert redirected_to(conn) == "/machines"
end
end
defmodule WebappWeb.TeamPrefixTest do
use WebappWeb.ConnCase
import WebappWeb.AuthCase
alias Webapp.{
Accounts
}
setup %{conn: conn} do
# prepare connection
conn = conn
|> bypass_through(WebappWeb.Router, [:browser_auth])
|> get("/")
# create user
user = add_user_confirmed("user@example.com")
another_user = add_user_confirmed("another_user@example.com")
# create team
{:ok, team} = Accounts.create_team(%{
name: "some name",
namespace: "user-company",
members: [%{"user_id" => user.id, "role" => "Administrator"}]
})
{:ok, another_team} = Accounts.create_team(%{
name: "other name",
namespace: "other-company",
members: [%{"user_id" => another_user.id, "role" => "Administrator"}]
})
conn = conn |> add_session(user) |> send_resp(:ok, "/")
{:ok, %{conn: conn, team: team, user: user}}
end
test "assigns user team as current_team form url", %{conn: conn, user: user, team: team} do
conn = get(conn, "/user-company/machines")
# current_team is loaded without members
team = Accounts.get_team!(team.id)
assert conn.assigns[:current_team] == team
end
test "team namespace is removed from path_info", %{conn: conn} do
conn = get(conn, "/user-company/machines")
assert conn.path_info == ["machines"]
end
test "restricted namespaces are not removed from path_info", %{conn: conn} do
conn = get(conn, "/health")
assert conn.path_info == ["health"]
end
test "responses with not found if team not exists", %{conn: conn} do
assert_raise Phoenix.Router.NoRouteError, ~r/no route found for GET \/non-existing-company\/machines/, fn ->
get(conn, "/non-existing-company/machines")
end
end
end