diff --git a/lib/bandit/context/config_context.ex b/lib/bandit/context/config_context.ex index 39e2cfb..29da64f 100644 --- a/lib/bandit/context/config_context.ex +++ b/lib/bandit/context/config_context.ex @@ -8,16 +8,17 @@ defmodule Bandit.Context.ConfigContext do """ import Ecto.Query + alias Bandit.Repo alias Bandit.Model.Config @doc """ Get a new config """ - def new_config(config \\ %{}) do + def new_config(attrs \\ %{}) do %{ - name: config.name, - value: config.value, + name: attrs.name, + value: attrs.value, uuid: Ecto.UUID.generate() } end diff --git a/lib/bandit/context/team_context.ex b/lib/bandit/context/team_context.ex index 9b43a9e..76c0ea2 100644 --- a/lib/bandit/context/team_context.ex +++ b/lib/bandit/context/team_context.ex @@ -8,17 +8,18 @@ defmodule Bandit.Context.TeamContext do """ import Ecto.Query + alias Bandit.Repo alias Bandit.Model.{Team, TeamMeta} @doc """ Get a new team """ - def new_team(team \\ %{}) do + def new_team(attrs \\ %{}) do %{ - slug: team.slug, - name: team.name, - description: team.description, + slug: attrs.slug, + name: attrs.name, + description: attrs.description, uuid: Ecto.UUID.generate() } end diff --git a/lib/bandit/context/user_context.ex b/lib/bandit/context/user_context.ex index 4e09e40..622b9ec 100644 --- a/lib/bandit/context/user_context.ex +++ b/lib/bandit/context/user_context.ex @@ -15,15 +15,15 @@ defmodule Bandit.Context.UserContext do @doc """ Get a new user """ - def new_user(user \\ %{}) do + def new_user(attrs \\ %{}) do %{ - email: user.email, - name: user.name, - password_hash: user.password_hash, - verified: user.verified, - last_seen: user.last_seen, - role: user.role, - api_key: user.api_key, + email: attrs.email, + name: attrs.name, + password_hash: attrs.password_hash, + verified: attrs.verified, + last_seen: attrs.last_seen, + role: attrs.role, + api_key: attrs.api_key, uuid: Ecto.UUID.generate() } end diff --git a/test/bandit/context/config_context_test.exs b/test/bandit/context/config_context_test.exs index e13deea..9912fc8 100644 --- a/test/bandit/context/config_context_test.exs +++ b/test/bandit/context/config_context_test.exs @@ -6,9 +6,16 @@ defmodule Bandit.Context.ConfigContextTest do @moduledoc """ Lock Context Test Cases """ - use Bandit.DataCase + + use ExUnit.Case + alias Bandit.Context.ConfigContext, as: ConfigContext + # Init + setup do + :ok = Ecto.Adapters.SQL.Sandbox.checkout(Brandit.Repo) + end + describe "new_config/1" do # new_config/1 test "new_config/1 test cases" do diff --git a/test/bandit/context/env_context_test.exs b/test/bandit/context/env_context_test.exs index 32d6d9e..e796865 100644 --- a/test/bandit/context/env_context_test.exs +++ b/test/bandit/context/env_context_test.exs @@ -6,12 +6,18 @@ defmodule Bandit.Context.EnvironmentContextTest do @moduledoc """ Environment Context Test Cases """ - use Bandit.DataCase + + use ExUnit.Case alias Bandit.Context.EnvironmentContext alias Bandit.Context.ProjectContext alias Bandit.Context.TeamContext + # Init + setup do + :ok = Ecto.Adapters.SQL.Sandbox.checkout(Brandit.Repo) + end + describe "new_env/1" do test "returns a new environment with a UUID" do team_item = diff --git a/test/bandit/context/lock_context.exs b/test/bandit/context/lock_context.exs index 62c4310..bb50d41 100644 --- a/test/bandit/context/lock_context.exs +++ b/test/bandit/context/lock_context.exs @@ -1,8 +1,21 @@ +# Copyright 2023 Clivern. All rights reserved. +# Use of this source code is governed by the MIT +# license that can be found in the LICENSE file. + defmodule Bandit.Context.LockContextTest do + @moduledoc """ + Lock Context Test Cases + """ + use ExUnit.Case alias Bandit.Context.LockContext + # Init + setup do + :ok = Ecto.Adapters.SQL.Sandbox.checkout(Brandit.Repo) + end + describe "new_lock/1" do test "returns a new lock" do lock = diff --git a/test/bandit/context/project_context.exs b/test/bandit/context/project_context.exs index 1593034..f7a6071 100644 --- a/test/bandit/context/project_context.exs +++ b/test/bandit/context/project_context.exs @@ -1,8 +1,21 @@ +# Copyright 2023 Clivern. All rights reserved. +# Use of this source code is governed by the MIT +# license that can be found in the LICENSE file. + defmodule Bandit.Context.ProjectContextTest do + @moduledoc """ + Project Context Test Cases + """ + use ExUnit.Case alias Bandit.Context.ProjectContext + # Init + setup do + :ok = Ecto.Adapters.SQL.Sandbox.checkout(Brandit.Repo) + end + describe "new_project/1" do test "returns a new project" do team_item = diff --git a/test/bandit/context/state_context.exs b/test/bandit/context/state_context.exs index 0ba1304..ddb79bf 100644 --- a/test/bandit/context/state_context.exs +++ b/test/bandit/context/state_context.exs @@ -1,8 +1,21 @@ +# Copyright 2023 Clivern. All rights reserved. +# Use of this source code is governed by the MIT +# license that can be found in the LICENSE file. + defmodule Bandit.Context.StateContextTest do + @moduledoc """ + State Context Test Cases + """ + use ExUnit.Case alias Bandit.Context.StateContext + # Init + setup do + :ok = Ecto.Adapters.SQL.Sandbox.checkout(Brandit.Repo) + end + describe "new_state/1" do test "returns a new state" do state = StateContext.new_state(%{name: "State 1", value: "Value 1", project_id: 1}) diff --git a/test/bandit/context/team_context.exs b/test/bandit/context/team_context.exs index faa7dc9..8a6b967 100644 --- a/test/bandit/context/team_context.exs +++ b/test/bandit/context/team_context.exs @@ -1,96 +1,172 @@ +# Copyright 2023 Clivern. All rights reserved. +# Use of this source code is governed by the MIT +# license that can be found in the LICENSE file. + defmodule Bandit.Context.TeamContextTest do + @moduledoc """ + Team Context Test Cases + """ + use ExUnit.Case alias Bandit.Context.TeamContext + # Init + setup do + :ok = Ecto.Adapters.SQL.Sandbox.checkout(Bandit.Repo) + end + + # Test Cases describe "new_team/1" do test "returns a new team" do - team = TeamContext.new_team(%{slug: "team-1", name: "Team 1", description: "Description 1"}) + team = + TeamContext.new_team(%{ + name: "Team 1", + description: "Description 1", + slug: "team-1" + }) - assert team.slug == "team-1" assert team.name == "Team 1" assert team.description == "Description 1" assert is_binary(team.uuid) end end + describe "new_meta/1" do + test "returns a new team meta" do + meta = + TeamContext.new_meta(%{ + key: "meta_key", + value: "meta_value", + team_id: 1 + }) + + assert meta.key == "meta_key" + assert meta.value == "meta_value" + assert meta.team_id == 1 + end + end + describe "create_team/1" do test "creates a new team" do - attrs = %{slug: "team-1", name: "Team 1", description: "Description 1"} - team = TeamContext.create_team(attrs) + attrs = + TeamContext.new_team(%{ + name: "Team 2", + description: "Description 2", + slug: "team-2" + }) - assert team.slug == "team-1" - assert team.name == "Team 1" - assert team.description == "Description 1" + {status, team} = TeamContext.create_team(attrs) + + assert status == :ok + + assert team.name == "Team 2" + assert team.description == "Description 2" assert is_binary(team.uuid) end end describe "get_team_by_id/1" do test "returns the team with the given ID" do - attrs = %{slug: "team-1", name: "Team 1", description: "Description 1"} - team = TeamContext.create_team(attrs) + attrs = + TeamContext.new_team(%{ + name: "Team 3", + description: "Description 3", + slug: "team-3" + }) - result = TeamContext.get_team_by_id(team.id) + {status1, team1} = TeamContext.create_team(attrs) - assert result == team + assert status1 == :ok + + team2 = TeamContext.get_team_by_id(team1.id) + + assert team1 == team2 end end describe "get_team_by_uuid/1" do test "returns the team with the given UUID" do - attrs = %{slug: "team-1", name: "Team 1", description: "Description 1"} - team = TeamContext.create_team(attrs) + attrs = + TeamContext.new_team(%{ + name: "Team 4", + description: "Description 4", + slug: "team-4" + }) - result = TeamContext.get_team_by_uuid(team.uuid) + {status1, team1} = TeamContext.create_team(attrs) - assert result == team - end - end - - describe "get_team_by_slug/1" do - test "returns the team with the given slug" do - attrs = %{slug: "team-1", name: "Team 1", description: "Description 1"} - team = TeamContext.create_team(attrs) + assert status1 == :ok - result = TeamContext.get_team_by_slug(team.slug) + team2 = TeamContext.get_team_by_uuid(team1.uuid) - assert result == team + assert team2 == team1 end end describe "update_team/2" do test "updates the team with the given attributes" do - attrs = %{slug: "team-1", name: "Team 1", description: "Description 1"} - team = TeamContext.create_team(attrs) + attrs = + TeamContext.new_team(%{ + name: "Team 5", + description: "Description 5", + slug: "team-5" + }) + + {status1, team1} = TeamContext.create_team(attrs) + + assert status1 == :ok - updated_attrs = %{slug: "team-2", name: "Team 2", description: "Description 2"} - updated_team = TeamContext.update_team(team, updated_attrs) + updated_attrs = %{name: "Team 6", description: "Description 6"} - assert updated_team.slug == "team-2" - assert updated_team.name == "Team 2" - assert updated_team.description == "Description 2" - assert updated_team.uuid == team.uuid + {status2, updated_team} = TeamContext.update_team(team1, updated_attrs) + + assert status2 == :ok + assert updated_team.name == "Team 6" + assert updated_team.description == "Description 6" + assert updated_team.uuid == team1.uuid end end describe "delete_team/1" do test "deletes the given team" do - attrs = %{slug: "team-1", name: "Team 1", description: "Description 1"} - team = TeamContext.create_team(attrs) + attrs = + TeamContext.new_team(%{ + name: "Team 1", + description: "Description 1", + slug: "team-1" + }) + + {status1, team1} = TeamContext.create_team(attrs) + + assert status1 == :ok - TeamContext.delete_team(team) + TeamContext.delete_team(team1) - assert TeamContext.get_team_by_id(team.id) == nil + assert TeamContext.get_team_by_id(team1.id) == nil end end describe "get_teams/0" do test "returns all teams" do - attrs1 = %{slug: "team-1", name: "Team 1", description: "Description 1"} - attrs2 = %{slug: "team-2", name: "Team 2", description: "Description 2"} - team1 = TeamContext.create_team(attrs1) - team2 = TeamContext.create_team(attrs2) + attrs1 = + TeamContext.new_team(%{ + name: "Team 1", + description: "Description 1", + slug: "team-1" + }) + + attrs2 = + TeamContext.new_team(%{ + name: "Team 2", + description: "Description 2" + }) + + {status1, team1} = TeamContext.create_team(attrs1) + {status2, team2} = TeamContext.create_team(attrs2) + + assert status1 == :ok + assert status2 == :ok result = TeamContext.get_teams() @@ -100,12 +176,34 @@ defmodule Bandit.Context.TeamContextTest do describe "get_teams/2" do test "returns the teams with the given offset and limit" do - attrs1 = %{slug: "team-1", name: "Team 1", description: "Description 1"} - attrs2 = %{slug: "team-2", name: "Team 2", description: "Description 2"} - attrs3 = %{slug: "team-3", name: "Team 3", description: "Description 3"} - team1 = TeamContext.create_team(attrs1) - team2 = TeamContext.create_team(attrs2) - team3 = TeamContext.create_team(attrs3) + attrs1 = + TeamContext.new_team(%{ + name: "Team 1", + description: "Description 1", + slug: "team-1" + }) + + attrs2 = + TeamContext.new_team(%{ + name: "Team 2", + description: "Description 2", + slug: "team-2" + }) + + attrs3 = + TeamContext.new_team(%{ + name: "Team 3", + description: "Description 3", + slug: "team-3" + }) + + {status1, _} = TeamContext.create_team(attrs1) + {status2, team2} = TeamContext.create_team(attrs2) + {status3, _} = TeamContext.create_team(attrs3) + + assert status1 == :ok + assert status2 == :ok + assert status3 == :ok result = TeamContext.get_teams(1, 1) @@ -115,19 +213,53 @@ defmodule Bandit.Context.TeamContextTest do describe "create_team_meta/1" do test "creates a new team meta" do - attrs = %{key: "foo", value: "bar", team_id: 1} - team_meta = TeamContext.create_team_meta(attrs) + attrs1 = + TeamContext.new_team(%{ + name: "Team 1", + description: "Description 1", + slug: "team-1" + }) + + {_, team} = TeamContext.create_team(attrs1) + + attrs = + TeamContext.new_meta(%{ + key: "foo", + value: "bar", + team_id: team.id + }) + + {status, team_meta} = TeamContext.create_team_meta(attrs) + + assert status == :ok assert team_meta.key == "foo" assert team_meta.value == "bar" - assert team_meta.team_id == 1 + assert team_meta.team_id == team.id end end describe "get_team_meta_by_id/1" do test "returns the team meta with the given ID" do - attrs = %{key: "foo", value: "bar", team_id: 1} - team_meta = TeamContext.create_team_meta(attrs) + attrs1 = + TeamContext.new_team(%{ + name: "Team 1", + description: "Description 1", + slug: "team-1" + }) + + {_, team} = TeamContext.create_team(attrs1) + + attrs = + TeamContext.new_meta(%{ + key: "foo", + value: "bar", + team_id: team.id + }) + + {status, team_meta} = TeamContext.create_team_meta(attrs) + + assert status == :ok result = TeamContext.get_team_meta_by_id(team_meta.id) @@ -137,26 +269,123 @@ defmodule Bandit.Context.TeamContextTest do describe "update_team_meta/2" do test "updates the team meta with the given attributes" do - attrs = %{key: "foo", value: "bar", team_id: 1} - team_meta = TeamContext.create_team_meta(attrs) - - updated_attrs = %{key: "baz", value: "qux", team_id: 2} - updated_team_meta = TeamContext.update_team_meta(team_meta, updated_attrs) + attrs1 = + TeamContext.new_team(%{ + name: "Team 1", + description: "Description 1", + slug: "team-1" + }) + + {_, team1} = TeamContext.create_team(attrs1) + + attrs = + TeamContext.new_meta(%{ + key: "foo", + value: "bar", + team_id: team1.id + }) + + {_, team_meta} = TeamContext.create_team_meta(attrs) + + attrs2 = + TeamContext.new_team(%{ + name: "Team 2", + description: "Description 2", + slug: "team-2" + }) + + {_, team2} = TeamContext.create_team(attrs2) + + updated_attrs = + TeamContext.new_meta(%{ + key: "baz", + value: "qux", + team_id: team2.id + }) + + {_, updated_team_meta} = TeamContext.update_team_meta(team_meta, updated_attrs) assert updated_team_meta.key == "baz" assert updated_team_meta.value == "qux" - assert updated_team_meta.team_id == 2 + assert updated_team_meta.team_id == team2.id end end describe "delete_team_meta/1" do test "deletes the given team meta" do - attrs = %{key: "foo", value: "bar", team_id: 1} - team_meta = TeamContext.create_team_meta(attrs) + attrs1 = + TeamContext.new_team(%{ + name: "Team 1", + description: "Description 1", + slug: "team-1" + }) + + {_, team} = TeamContext.create_team(attrs1) + + attrs = + TeamContext.new_meta(%{ + key: "foo", + value: "bar", + team_id: team.id + }) + + {_, team_meta} = TeamContext.create_team_meta(attrs) TeamContext.delete_team_meta(team_meta) assert TeamContext.get_team_meta_by_id(team_meta.id) == nil end end + + describe "get_team_meta_by_id_key/2" do + test "get team meta by team id and key" do + attrs1 = + TeamContext.new_team(%{ + name: "team_111", + description: "description_111", + slug: "team_111" + }) + + {_, team} = TeamContext.create_team(attrs1) + + attrs = + TeamContext.new_meta(%{ + key: "foo_111", + value: "bar_111", + team_id: team.id + }) + + {_, team_meta} = TeamContext.create_team_meta(attrs) + + result = TeamContext.get_team_meta_by_id_key(team.id, team_meta.key) + + assert result == team_meta + end + end + + describe "get_team_metas/1" do + test "get all metas for a team" do + attrs1 = + TeamContext.new_team(%{ + name: "team_222", + description: "description_222", + slug: "team_222" + }) + + {_, team} = TeamContext.create_team(attrs1) + + attrs = + TeamContext.new_meta(%{ + key: "foo_222", + value: "bar_222", + team_id: team.id + }) + + {_, team_meta} = TeamContext.create_team_meta(attrs) + + result = TeamContext.get_team_metas(team.id) + + assert result == [team_meta] + end + end end diff --git a/test/bandit/context/user_context.exs b/test/bandit/context/user_context.exs index 5502dd0..8f8192b 100644 --- a/test/bandit/context/user_context.exs +++ b/test/bandit/context/user_context.exs @@ -2,10 +2,1118 @@ # Use of this source code is governed by the MIT # license that can be found in the LICENSE file. -defmodule Bandit.Context.UserContextTest do +defmodule Brandit.Context.UserContextTest do @moduledoc """ - Lock Context Test Cases + User Context Test Cases """ - use Bandit.DataCase - alias Bandit.Context.UserContext, as: UserContext + + use ExUnit.Case + + alias Brandit.Context.UserContext + alias Brandit.Context.TeamContext + + # Init + setup do + :ok = Ecto.Adapters.SQL.Sandbox.checkout(Brandit.Repo) + end + + # Test Cases + describe "new_user/1" do + test "test new_user" do + user = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + assert user.email == "hello@clivern.com" + assert user.name == "Clivern" + assert user.password_hash == "27hd7wh2" + assert user.verified == true + assert user.role == "super" + assert user.api_key == "x-x-x-x-x" + assert is_binary(user.uuid) == true + end + end + + describe "new_meta/1" do + test "test new_meta" do + meta = + UserContext.new_meta(%{ + key: "meta_key", + value: "meta_value", + user_id: 1 + }) + + assert meta.user_id == 1 + assert meta.key == "meta_key" + assert meta.value == "meta_value" + end + end + + describe "new_session/1" do + test "test new_session" do + session = + UserContext.new_session(%{ + expire_at: "expire_at", + value: "meta_value", + user_id: 1 + }) + + assert session.user_id == 1 + assert session.expire_at == "expire_at" + assert session.value == "meta_value" + end + end + + describe "create_user/1" do + test "test create_user" do + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {status, user} = UserContext.create_user(attr) + + assert status == :ok + assert user.email == "hello@clivern.com" + assert is_binary(user.uuid) + end + end + + describe "get_user_by_id/1" do + test "test get_user_by_id" do + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {status, user} = UserContext.create_user(attr) + + assert status == :ok + assert user.email == "hello@clivern.com" + assert is_binary(user.uuid) + + result = UserContext.get_user_by_id(user.id) + + assert result == user + end + end + + describe "get_user_by_uuid/1" do + test "test get_user_by_uuid" do + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {status, user} = UserContext.create_user(attr) + + assert status == :ok + assert user.email == "hello@clivern.com" + assert is_binary(user.uuid) + + result = UserContext.get_user_by_uuid(user.uuid) + + assert result == user + end + end + + describe "get_user_by_api_key/1" do + test "test get_user_by_api_key" do + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {status, user} = UserContext.create_user(attr) + + assert status == :ok + assert user.email == "hello@clivern.com" + assert is_binary(user.uuid) + + result = UserContext.get_user_by_api_key(user.api_key) + + assert result == user + end + end + + describe "get_user_by_email/1" do + test "test get_user_by_email" do + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {status, user} = UserContext.create_user(attr) + + assert status == :ok + assert user.email == "hello@clivern.com" + assert is_binary(user.uuid) + + result = UserContext.get_user_by_email(user.email) + + assert result == user + end + end + + describe "update_user/2" do + test "test update_user" do + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {status, user} = UserContext.create_user(attr) + + assert status == :ok + assert user.email == "hello@clivern.com" + assert is_binary(user.uuid) + + {_, result} = UserContext.update_user(user, %{email: "hi@clivern.com"}) + + assert result.email == "hi@clivern.com" + end + end + + describe "delete_user/1" do + test "test delete_user" do + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {_, user} = UserContext.create_user(attr) + + UserContext.delete_user(user) + + assert UserContext.get_user_by_id(user.id) == nil + end + end + + describe "get_users/0" do + test "test get_users" do + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {_, user} = UserContext.create_user(attr) + + assert UserContext.get_users() == [user] + end + end + + describe "get_users/2" do + test "test get_users" do + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {_, user} = UserContext.create_user(attr) + + assert UserContext.get_users(0, 1) == [user] + assert UserContext.get_users(1, 1) == [] + end + end + + describe "count_users/0" do + test "test count_users" do + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + UserContext.create_user(attr) + + assert UserContext.count_users() == 1 + end + end + + describe "create_user_meta/1" do + test "test create_user_meta" do + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {_, user} = UserContext.create_user(attr) + + attr = + UserContext.new_meta(%{ + key: "meta_key", + value: "meta_value", + user_id: user.id + }) + + {status, meta} = UserContext.create_user_meta(attr) + + assert status == :ok + assert meta.key == "meta_key" + assert meta.value == "meta_value" + assert meta.user_id == user.id + assert meta.id > 0 == true + end + end + + describe "create_user_session/1" do + test "test create_user_session" do + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {_, user} = UserContext.create_user(attr) + + dt = DateTime.utc_now() + + attr = + UserContext.new_session(%{ + expire_at: dt, + value: "session_value", + user_id: user.id + }) + + {status, session} = UserContext.create_user_session(attr) + + assert status == :ok + assert session.value == "session_value" + assert session.user_id == user.id + assert session.id > 0 == true + end + end + + describe "get_user_meta_by_id/1" do + test "test get_user_meta_by_id" do + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {_, user} = UserContext.create_user(attr) + + attr = + UserContext.new_meta(%{ + key: "meta_key", + value: "meta_value", + user_id: user.id + }) + + {status, meta} = UserContext.create_user_meta(attr) + + result = UserContext.get_user_meta_by_id(meta.id) + + assert status == :ok + assert meta == result + end + end + + describe "update_user_meta/2" do + test "test update_user_meta" do + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {_, user} = UserContext.create_user(attr) + + attr = + UserContext.new_meta(%{ + key: "meta_key", + value: "meta_value", + user_id: user.id + }) + + {status, meta} = UserContext.create_user_meta(attr) + + assert status == :ok + assert meta.key == "meta_key" + assert meta.value == "meta_value" + + {status, meta} = + UserContext.update_user_meta( + meta, + %{key: "new_meta_key", value: "new_meta_value"} + ) + + assert status == :ok + assert meta.key == "new_meta_key" + assert meta.value == "new_meta_value" + + result = UserContext.get_user_meta_by_id(meta.id) + + assert status == :ok + assert result.key == "new_meta_key" + assert result.value == "new_meta_value" + end + end + + describe "update_user_session/2" do + test "test update_user_session" do + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {_, user} = UserContext.create_user(attr) + + dt = DateTime.utc_now() + + attr = + UserContext.new_session(%{ + expire_at: dt, + value: "session_value", + user_id: user.id + }) + + {status, session} = UserContext.create_user_session(attr) + + assert status == :ok + assert session.value == "session_value" + assert session.user_id == user.id + assert session.id > 0 == true + + {status, session} = UserContext.update_user_session(session, %{value: "new_session_value"}) + + assert status == :ok + assert session.value == "new_session_value" + assert session.user_id == user.id + assert session.id > 0 == true + end + end + + describe "delete_user_meta/1" do + test "test delete_user_meta" do + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {_, user} = UserContext.create_user(attr) + + attr = + UserContext.new_meta(%{ + key: "meta_key", + value: "meta_value", + user_id: user.id + }) + + {_, meta} = UserContext.create_user_meta(attr) + + UserContext.delete_user_meta(meta) + + result = UserContext.get_user_meta_by_id(meta.id) + + assert result == nil + end + end + + describe "delete_user_session/1" do + test "test delete_user_session" do + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {_, user} = UserContext.create_user(attr) + + attr = + UserContext.new_session(%{ + expire_at: DateTime.utc_now(), + value: "session_value", + user_id: user.id + }) + + {_, session} = UserContext.create_user_session(attr) + + UserContext.delete_user_session(session) + + result = UserContext.get_user_session_by_id_value(user.id, "session_value") + + assert result == nil + end + end + + describe "delete_user_sessions/1" do + test "test delete_user_sessions" do + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {_, user} = UserContext.create_user(attr) + + attr = + UserContext.new_session(%{ + expire_at: DateTime.utc_now(), + value: "session_value", + user_id: user.id + }) + + UserContext.create_user_session(attr) + + UserContext.delete_user_sessions(user.id) + + result = UserContext.get_user_session_by_id_value(user.id, "session_value") + + assert result == nil + end + end + + describe "get_user_meta_by_id_key/2" do + test "test case" do + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {_, user} = UserContext.create_user(attr) + + attr = + UserContext.new_meta(%{ + key: "meta_key", + value: "meta_value", + user_id: user.id + }) + + UserContext.create_user_meta(attr) + + result = UserContext.get_user_meta_by_id_key(user.id, "meta_key") + + assert result.key == "meta_key" + assert result.value == "meta_value" + assert result.user_id == user.id + end + end + + describe "get_user_session_by_id_key/2" do + test "test get_user_session_by_id_key" do + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {_, user} = UserContext.create_user(attr) + + attr = + UserContext.new_session(%{ + expire_at: DateTime.utc_now(), + value: "session_value", + user_id: user.id + }) + + UserContext.create_user_session(attr) + + result = UserContext.get_user_session_by_id_value(user.id, "session_value") + + assert result.value == "session_value" + assert result.user_id == user.id + end + end + + describe "get_user_sessions/1" do + test "test get_user_sessions" do + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {_, user} = UserContext.create_user(attr) + + dt = DateTime.utc_now() + + attr = + UserContext.new_session(%{ + expire_at: dt, + value: "session_value", + user_id: user.id + }) + + {_, session} = UserContext.create_user_session(attr) + + result = UserContext.get_user_sessions(user.id) + + assert result == [session] + + result = UserContext.get_user_sessions(1000) + + assert result == [] + end + end + + describe "get_user_metas/1" do + test "test get_user_metas" do + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {_, user} = UserContext.create_user(attr) + + attr = + UserContext.new_meta(%{ + key: "meta_key", + value: "meta_value", + user_id: user.id + }) + + {_, meta} = UserContext.create_user_meta(attr) + + result = UserContext.get_user_metas(user.id) + + assert result == [meta] + + result = UserContext.get_user_metas(1000) + + assert result == [] + end + end + + describe "add_user_to_team/2" do + test "test add_user_to_team" do + attrs = + TeamContext.new_team(%{ + name: "team_a", + description: "team_a" + }) + + {status_a, team_a} = TeamContext.create_team(attrs) + + assert status_a == :ok + assert team_a.name == "team_a" + assert team_a.description == "team_a" + assert is_binary(team_a.uuid) + + attrs = + TeamContext.new_team(%{ + name: "team_b", + description: "team_b" + }) + + {status_b, team_b} = TeamContext.create_team(attrs) + + assert status_b == :ok + assert team_b.name == "team_b" + assert team_b.description == "team_b" + assert is_binary(team_b.uuid) + + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {_, user} = UserContext.create_user(attr) + + {status1, result1} = UserContext.add_user_to_team(user.id, team_a.id) + {status2, result2} = UserContext.add_user_to_team(user.id, team_b.id) + + assert status1 == :ok + assert status2 == :ok + + assert result1.user_id == user.id + assert result2.user_id == user.id + + assert result1.team_id == team_a.id + assert result2.team_id == team_b.id + + result = UserContext.get_user_teams(user.id) + + assert result == [team_a, team_b] + + assert UserContext.get_user_teams(10000) == [] + end + end + + describe "remove_user_from_team/2" do + test "test remove_user_from_team" do + attrs = + TeamContext.new_team(%{ + name: "team_a", + description: "team_a" + }) + + {status_a, team_a} = TeamContext.create_team(attrs) + + assert status_a == :ok + assert team_a.name == "team_a" + assert team_a.description == "team_a" + assert is_binary(team_a.uuid) + + attrs = + TeamContext.new_team(%{ + name: "team_b", + description: "team_b" + }) + + {status_b, team_b} = TeamContext.create_team(attrs) + + assert status_b == :ok + assert team_b.name == "team_b" + assert team_b.description == "team_b" + assert is_binary(team_b.uuid) + + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {_, user} = UserContext.create_user(attr) + + {status1, result1} = UserContext.add_user_to_team(user.id, team_a.id) + {status2, result2} = UserContext.add_user_to_team(user.id, team_b.id) + + assert status1 == :ok + assert status2 == :ok + + assert result1.user_id == user.id + assert result2.user_id == user.id + + assert result1.team_id == team_a.id + assert result2.team_id == team_b.id + + # Remove user from team a + UserContext.remove_user_from_team(user.id, team_a.id) + + result = UserContext.get_user_teams(user.id) + + assert result == [team_b] + + assert UserContext.get_user_teams(10000) == [] + end + end + + describe "remove_user_from_team_by_uuid/1" do + test "test remove_user_from_team_by_uuid" do + attrs = + TeamContext.new_team(%{ + name: "team_a", + description: "team_a" + }) + + {status_a, team_a} = TeamContext.create_team(attrs) + + assert status_a == :ok + assert team_a.name == "team_a" + assert team_a.description == "team_a" + assert is_binary(team_a.uuid) + + attrs = + TeamContext.new_team(%{ + name: "team_b", + description: "team_b" + }) + + {status_b, team_b} = TeamContext.create_team(attrs) + + assert status_b == :ok + assert team_b.name == "team_b" + assert team_b.description == "team_b" + assert is_binary(team_b.uuid) + + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {_, user} = UserContext.create_user(attr) + + {status1, result1} = UserContext.add_user_to_team(user.id, team_a.id) + {status2, result2} = UserContext.add_user_to_team(user.id, team_b.id) + + assert status1 == :ok + assert status2 == :ok + + assert result1.user_id == user.id + assert result2.user_id == user.id + + assert result1.team_id == team_a.id + assert result2.team_id == team_b.id + + UserContext.remove_user_from_team_by_uuid(result1.uuid) + + result = UserContext.get_user_teams(user.id) + + assert result == [team_b] + + assert UserContext.get_user_teams(10000) == [] + end + end + + describe "get_user_teams/1" do + test "test get_user_teams" do + attrs = + TeamContext.new_team(%{ + name: "team_a", + description: "team_a" + }) + + {status_a, team_a} = TeamContext.create_team(attrs) + + assert status_a == :ok + assert team_a.name == "team_a" + assert team_a.description == "team_a" + assert is_binary(team_a.uuid) + + attrs = + TeamContext.new_team(%{ + name: "team_b", + description: "team_b" + }) + + {status_b, team_b} = TeamContext.create_team(attrs) + + assert status_b == :ok + assert team_b.name == "team_b" + assert team_b.description == "team_b" + assert is_binary(team_b.uuid) + + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {_, user} = UserContext.create_user(attr) + + {status1, result1} = UserContext.add_user_to_team(user.id, team_a.id) + {status2, result2} = UserContext.add_user_to_team(user.id, team_b.id) + + assert status1 == :ok + assert status2 == :ok + + assert result1.user_id == user.id + assert result2.user_id == user.id + + assert result1.team_id == team_a.id + assert result2.team_id == team_b.id + + # Remove user from team a + UserContext.remove_user_from_team(user.id, team_a.id) + + result = UserContext.get_user_teams(user.id) + + assert result == [team_b] + + assert UserContext.get_user_teams(10000) == [] + end + end + + describe "get_team_users/1" do + test "test get_team_users" do + attrs = + TeamContext.new_team(%{ + name: "team_a", + description: "team_a" + }) + + {status_a, team_a} = TeamContext.create_team(attrs) + + assert status_a == :ok + assert team_a.name == "team_a" + assert team_a.description == "team_a" + assert is_binary(team_a.uuid) + + attrs = + TeamContext.new_team(%{ + name: "team_b", + description: "team_b" + }) + + {status_b, team_b} = TeamContext.create_team(attrs) + + assert status_b == :ok + assert team_b.name == "team_b" + assert team_b.description == "team_b" + assert is_binary(team_b.uuid) + + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {_, user} = UserContext.create_user(attr) + + {status1, result1} = UserContext.add_user_to_team(user.id, team_a.id) + {status2, result2} = UserContext.add_user_to_team(user.id, team_b.id) + + assert status1 == :ok + assert status2 == :ok + + assert result1.user_id == user.id + assert result2.user_id == user.id + + assert result1.team_id == team_a.id + assert result2.team_id == team_b.id + + result = UserContext.get_team_users(team_a.id) + + assert result == [user] + + result = UserContext.get_team_users(team_b.id) + + assert result == [user] + + assert UserContext.get_team_users(10000) == [] + end + end + + describe "count_team_users/1" do + test "test count_team_users" do + attrs = + TeamContext.new_team(%{ + name: "team_a", + description: "team_a" + }) + + {status_a, team_a} = TeamContext.create_team(attrs) + + assert status_a == :ok + assert team_a.name == "team_a" + assert team_a.description == "team_a" + assert is_binary(team_a.uuid) + + attrs = + TeamContext.new_team(%{ + name: "team_b", + description: "team_b" + }) + + {status_b, team_b} = TeamContext.create_team(attrs) + + assert status_b == :ok + assert team_b.name == "team_b" + assert team_b.description == "team_b" + assert is_binary(team_b.uuid) + + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {_, user} = UserContext.create_user(attr) + + UserContext.add_user_to_team(user.id, team_a.id) + UserContext.add_user_to_team(user.id, team_b.id) + + assert UserContext.count_team_users(team_a.id) == 1 + assert UserContext.count_team_users(team_b.id) == 1 + assert UserContext.count_team_users(100) == 0 + end + end + + describe "validate_user_id/1" do + test "test validate_user_id" do + attr = + UserContext.new_user(%{ + email: "hello@clivern.com", + name: "Clivern", + password_hash: "27hd7wh2", + verified: true, + last_seen: DateTime.utc_now(), + role: "super", + api_key: "x-x-x-x-x" + }) + + {status, user} = UserContext.create_user(attr) + + assert status == :ok + assert user.email == "hello@clivern.com" + assert is_binary(user.uuid) + + assert UserContext.validate_user_id(user.id) == true + end + end + + describe "validate_team_id/1" do + test "test validate_team_id" do + attrs = + TeamContext.new_team(%{ + name: "Team 2", + description: "Description 2" + }) + + {status, team} = TeamContext.create_team(attrs) + + assert status == :ok + + assert team.name == "Team 2" + assert team.description == "Description 2" + assert is_binary(team.uuid) + + assert UserContext.validate_team_id(team.id) == true + end + end end