From 11b072faf7c92b4ef8ac520eb048b84cfba70cb9 Mon Sep 17 00:00:00 2001 From: "Liam P. White" Date: Wed, 28 Aug 2019 13:02:42 -0400 Subject: [PATCH] context: _bans --- lib/philomena/bans.ex | 296 ++++++++++++++++++++++++++++++ lib/philomena/bans/fingerprint.ex | 24 +++ lib/philomena/bans/subnet.ex | 24 +++ lib/philomena/bans/user.ex | 25 +++ test/philomena/bans_test.exs | 176 ++++++++++++++++++ 5 files changed, 545 insertions(+) create mode 100644 lib/philomena/bans.ex create mode 100644 lib/philomena/bans/fingerprint.ex create mode 100644 lib/philomena/bans/subnet.ex create mode 100644 lib/philomena/bans/user.ex create mode 100644 test/philomena/bans_test.exs diff --git a/lib/philomena/bans.ex b/lib/philomena/bans.ex new file mode 100644 index 00000000..f8e38f5b --- /dev/null +++ b/lib/philomena/bans.ex @@ -0,0 +1,296 @@ +defmodule Philomena.Bans do + @moduledoc """ + The Bans context. + """ + + import Ecto.Query, warn: false + alias Philomena.Repo + + alias Philomena.Bans.Fingerprint + + @doc """ + Returns the list of fingerprint_bans. + + ## Examples + + iex> list_fingerprint_bans() + [%Fingerprint{}, ...] + + """ + def list_fingerprint_bans do + Repo.all(Fingerprint) + end + + @doc """ + Gets a single fingerprint. + + Raises `Ecto.NoResultsError` if the Fingerprint does not exist. + + ## Examples + + iex> get_fingerprint!(123) + %Fingerprint{} + + iex> get_fingerprint!(456) + ** (Ecto.NoResultsError) + + """ + def get_fingerprint!(id), do: Repo.get!(Fingerprint, id) + + @doc """ + Creates a fingerprint. + + ## Examples + + iex> create_fingerprint(%{field: value}) + {:ok, %Fingerprint{}} + + iex> create_fingerprint(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_fingerprint(attrs \\ %{}) do + %Fingerprint{} + |> Fingerprint.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a fingerprint. + + ## Examples + + iex> update_fingerprint(fingerprint, %{field: new_value}) + {:ok, %Fingerprint{}} + + iex> update_fingerprint(fingerprint, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_fingerprint(%Fingerprint{} = fingerprint, attrs) do + fingerprint + |> Fingerprint.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a Fingerprint. + + ## Examples + + iex> delete_fingerprint(fingerprint) + {:ok, %Fingerprint{}} + + iex> delete_fingerprint(fingerprint) + {:error, %Ecto.Changeset{}} + + """ + def delete_fingerprint(%Fingerprint{} = fingerprint) do + Repo.delete(fingerprint) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking fingerprint changes. + + ## Examples + + iex> change_fingerprint(fingerprint) + %Ecto.Changeset{source: %Fingerprint{}} + + """ + def change_fingerprint(%Fingerprint{} = fingerprint) do + Fingerprint.changeset(fingerprint, %{}) + end + + alias Philomena.Bans.Subnet + + @doc """ + Returns the list of subnet_bans. + + ## Examples + + iex> list_subnet_bans() + [%Subnet{}, ...] + + """ + def list_subnet_bans do + Repo.all(Subnet) + end + + @doc """ + Gets a single subnet. + + Raises `Ecto.NoResultsError` if the Subnet does not exist. + + ## Examples + + iex> get_subnet!(123) + %Subnet{} + + iex> get_subnet!(456) + ** (Ecto.NoResultsError) + + """ + def get_subnet!(id), do: Repo.get!(Subnet, id) + + @doc """ + Creates a subnet. + + ## Examples + + iex> create_subnet(%{field: value}) + {:ok, %Subnet{}} + + iex> create_subnet(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_subnet(attrs \\ %{}) do + %Subnet{} + |> Subnet.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a subnet. + + ## Examples + + iex> update_subnet(subnet, %{field: new_value}) + {:ok, %Subnet{}} + + iex> update_subnet(subnet, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_subnet(%Subnet{} = subnet, attrs) do + subnet + |> Subnet.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a Subnet. + + ## Examples + + iex> delete_subnet(subnet) + {:ok, %Subnet{}} + + iex> delete_subnet(subnet) + {:error, %Ecto.Changeset{}} + + """ + def delete_subnet(%Subnet{} = subnet) do + Repo.delete(subnet) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking subnet changes. + + ## Examples + + iex> change_subnet(subnet) + %Ecto.Changeset{source: %Subnet{}} + + """ + def change_subnet(%Subnet{} = subnet) do + Subnet.changeset(subnet, %{}) + end + + alias Philomena.Bans.User + + @doc """ + Returns the list of user_bans. + + ## Examples + + iex> list_user_bans() + [%User{}, ...] + + """ + def list_user_bans do + Repo.all(User) + end + + @doc """ + Gets a single user. + + Raises `Ecto.NoResultsError` if the User does not exist. + + ## Examples + + iex> get_user!(123) + %User{} + + iex> get_user!(456) + ** (Ecto.NoResultsError) + + """ + def get_user!(id), do: Repo.get!(User, id) + + @doc """ + Creates a user. + + ## Examples + + iex> create_user(%{field: value}) + {:ok, %User{}} + + iex> create_user(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_user(attrs \\ %{}) do + %User{} + |> User.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a user. + + ## Examples + + iex> update_user(user, %{field: new_value}) + {:ok, %User{}} + + iex> update_user(user, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_user(%User{} = user, attrs) do + user + |> User.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a User. + + ## Examples + + iex> delete_user(user) + {:ok, %User{}} + + iex> delete_user(user) + {:error, %Ecto.Changeset{}} + + """ + def delete_user(%User{} = user) do + Repo.delete(user) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking user changes. + + ## Examples + + iex> change_user(user) + %Ecto.Changeset{source: %User{}} + + """ + def change_user(%User{} = user) do + User.changeset(user, %{}) + end +end diff --git a/lib/philomena/bans/fingerprint.ex b/lib/philomena/bans/fingerprint.ex new file mode 100644 index 00000000..a76cf137 --- /dev/null +++ b/lib/philomena/bans/fingerprint.ex @@ -0,0 +1,24 @@ +defmodule Philomena.Bans.Fingerprint do + use Ecto.Schema + import Ecto.Changeset + + schema "fingerprint_bans" do + belongs_to :banning_user, Philomena.Users.User + + field :reason, :string + field :note, :string + field :enabled, :boolean, default: true + field :valid_until, :naive_datetime + field :fingerprint, :string + field :generated_ban_id, :string + + timestamps(inserted_at: :created_at) + end + + @doc false + def changeset(fingerprint, attrs) do + fingerprint + |> cast(attrs, []) + |> validate_required([]) + end +end diff --git a/lib/philomena/bans/subnet.ex b/lib/philomena/bans/subnet.ex new file mode 100644 index 00000000..7add4880 --- /dev/null +++ b/lib/philomena/bans/subnet.ex @@ -0,0 +1,24 @@ +defmodule Philomena.Bans.Subnet do + use Ecto.Schema + import Ecto.Changeset + + schema "subnet_bans" do + belongs_to :banning_user, Philomena.Users.User + + field :reason, :string + field :note, :string + field :enabled, :boolean, default: true + field :valid_until, :naive_datetime + field :specification, EctoNetwork.INET + field :generated_ban_id, :string + + timestamps(inserted_at: :created_at) + end + + @doc false + def changeset(subnet, attrs) do + subnet + |> cast(attrs, []) + |> validate_required([]) + end +end diff --git a/lib/philomena/bans/user.ex b/lib/philomena/bans/user.ex new file mode 100644 index 00000000..cd72cd20 --- /dev/null +++ b/lib/philomena/bans/user.ex @@ -0,0 +1,25 @@ +defmodule Philomena.Bans.User do + use Ecto.Schema + import Ecto.Changeset + + schema "user_bans" do + belongs_to :user, Philomena.Users.User + belongs_to :banning_user, Philomena.Users.User + + field :reason, :string + field :note, :string + field :enabled, :boolean, default: true + field :valid_until, :naive_datetime + field :generated_ban_id, :string + field :override_ip_ban, :boolean, default: false + + timestamps(inserted_at: :created_at) + end + + @doc false + def changeset(user, attrs) do + user + |> cast(attrs, []) + |> validate_required([]) + end +end diff --git a/test/philomena/bans_test.exs b/test/philomena/bans_test.exs new file mode 100644 index 00000000..d527a09c --- /dev/null +++ b/test/philomena/bans_test.exs @@ -0,0 +1,176 @@ +defmodule Philomena.BansTest do + use Philomena.DataCase + + alias Philomena.Bans + + describe "fingerprint_bans" do + alias Philomena.Bans.Fingerprint + + @valid_attrs %{} + @update_attrs %{} + @invalid_attrs %{} + + def fingerprint_fixture(attrs \\ %{}) do + {:ok, fingerprint} = + attrs + |> Enum.into(@valid_attrs) + |> Bans.create_fingerprint() + + fingerprint + end + + test "list_fingerprint_bans/0 returns all fingerprint_bans" do + fingerprint = fingerprint_fixture() + assert Bans.list_fingerprint_bans() == [fingerprint] + end + + test "get_fingerprint!/1 returns the fingerprint with given id" do + fingerprint = fingerprint_fixture() + assert Bans.get_fingerprint!(fingerprint.id) == fingerprint + end + + test "create_fingerprint/1 with valid data creates a fingerprint" do + assert {:ok, %Fingerprint{} = fingerprint} = Bans.create_fingerprint(@valid_attrs) + end + + test "create_fingerprint/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Bans.create_fingerprint(@invalid_attrs) + end + + test "update_fingerprint/2 with valid data updates the fingerprint" do + fingerprint = fingerprint_fixture() + assert {:ok, %Fingerprint{} = fingerprint} = Bans.update_fingerprint(fingerprint, @update_attrs) + end + + test "update_fingerprint/2 with invalid data returns error changeset" do + fingerprint = fingerprint_fixture() + assert {:error, %Ecto.Changeset{}} = Bans.update_fingerprint(fingerprint, @invalid_attrs) + assert fingerprint == Bans.get_fingerprint!(fingerprint.id) + end + + test "delete_fingerprint/1 deletes the fingerprint" do + fingerprint = fingerprint_fixture() + assert {:ok, %Fingerprint{}} = Bans.delete_fingerprint(fingerprint) + assert_raise Ecto.NoResultsError, fn -> Bans.get_fingerprint!(fingerprint.id) end + end + + test "change_fingerprint/1 returns a fingerprint changeset" do + fingerprint = fingerprint_fixture() + assert %Ecto.Changeset{} = Bans.change_fingerprint(fingerprint) + end + end + + describe "subnet_bans" do + alias Philomena.Bans.Subnet + + @valid_attrs %{} + @update_attrs %{} + @invalid_attrs %{} + + def subnet_fixture(attrs \\ %{}) do + {:ok, subnet} = + attrs + |> Enum.into(@valid_attrs) + |> Bans.create_subnet() + + subnet + end + + test "list_subnet_bans/0 returns all subnet_bans" do + subnet = subnet_fixture() + assert Bans.list_subnet_bans() == [subnet] + end + + test "get_subnet!/1 returns the subnet with given id" do + subnet = subnet_fixture() + assert Bans.get_subnet!(subnet.id) == subnet + end + + test "create_subnet/1 with valid data creates a subnet" do + assert {:ok, %Subnet{} = subnet} = Bans.create_subnet(@valid_attrs) + end + + test "create_subnet/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Bans.create_subnet(@invalid_attrs) + end + + test "update_subnet/2 with valid data updates the subnet" do + subnet = subnet_fixture() + assert {:ok, %Subnet{} = subnet} = Bans.update_subnet(subnet, @update_attrs) + end + + test "update_subnet/2 with invalid data returns error changeset" do + subnet = subnet_fixture() + assert {:error, %Ecto.Changeset{}} = Bans.update_subnet(subnet, @invalid_attrs) + assert subnet == Bans.get_subnet!(subnet.id) + end + + test "delete_subnet/1 deletes the subnet" do + subnet = subnet_fixture() + assert {:ok, %Subnet{}} = Bans.delete_subnet(subnet) + assert_raise Ecto.NoResultsError, fn -> Bans.get_subnet!(subnet.id) end + end + + test "change_subnet/1 returns a subnet changeset" do + subnet = subnet_fixture() + assert %Ecto.Changeset{} = Bans.change_subnet(subnet) + end + end + + describe "user_bans" do + alias Philomena.Bans.User + + @valid_attrs %{} + @update_attrs %{} + @invalid_attrs %{} + + def user_fixture(attrs \\ %{}) do + {:ok, user} = + attrs + |> Enum.into(@valid_attrs) + |> Bans.create_user() + + user + end + + test "list_user_bans/0 returns all user_bans" do + user = user_fixture() + assert Bans.list_user_bans() == [user] + end + + test "get_user!/1 returns the user with given id" do + user = user_fixture() + assert Bans.get_user!(user.id) == user + end + + test "create_user/1 with valid data creates a user" do + assert {:ok, %User{} = user} = Bans.create_user(@valid_attrs) + end + + test "create_user/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Bans.create_user(@invalid_attrs) + end + + test "update_user/2 with valid data updates the user" do + user = user_fixture() + assert {:ok, %User{} = user} = Bans.update_user(user, @update_attrs) + end + + test "update_user/2 with invalid data returns error changeset" do + user = user_fixture() + assert {:error, %Ecto.Changeset{}} = Bans.update_user(user, @invalid_attrs) + assert user == Bans.get_user!(user.id) + end + + test "delete_user/1 deletes the user" do + user = user_fixture() + assert {:ok, %User{}} = Bans.delete_user(user) + assert_raise Ecto.NoResultsError, fn -> Bans.get_user!(user.id) end + end + + test "change_user/1 returns a user changeset" do + user = user_fixture() + assert %Ecto.Changeset{} = Bans.change_user(user) + end + end +end