Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Board controller authorizations #79

Merged
merged 24 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a3e20b6
feat(controllers/board): add authorizations for Boards by category
akinsey Oct 31, 2023
5c4b3f7
feat(controllers/board): add authorizations acl check for Boards find
akinsey Oct 31, 2023
cdd1cca
feat(controllers/board): add authorizations checking if authed user c…
akinsey Oct 31, 2023
b3e5bee
style(format): run mix format
akinsey Oct 31, 2023
29e316c
fix(controller/board): resolve issue with wrong error being thrown af…
akinsey Oct 31, 2023
0f71c54
Merge remote-tracking branch 'origin/session-test' into board-control…
unenglishable Nov 1, 2023
54ee774
test(seed+conn_case): seed private user and prepare in conn_case
unenglishable Nov 2, 2023
f2364d5
test(controllers/board): check by_category errors on invalid permissions
unenglishable Nov 2, 2023
12f4d80
test(controllers/board): check invalid board read access for unauthen…
unenglishable Nov 2, 2023
3ce8b88
test(seed/users): add seed for every role type
akinsey Nov 2, 2023
8d43e8e
test(conn_case): add all new roles from user seed to conn case
akinsey Nov 2, 2023
88ffd0c
test(session_test): updating tests with banned tag to use banned user
akinsey Nov 2, 2023
db74461
test(models/mention): update mention test to user private role instea…
akinsey Nov 2, 2023
59d9649
style(format): run mix format
akinsey Nov 2, 2023
241e510
test(controllers/board): update unauthenticated test description
unenglishable Nov 2, 2023
46e87d0
test(controllers/board): check board find when authenticated
unenglishable Nov 2, 2023
95ed14e
test(controllers/board): check board find when authenticated as admin
unenglishable Nov 2, 2023
55a1e6c
test(controllers/board): test super admin board find
unenglishable Nov 2, 2023
17ba087
Merge remote-tracking branch 'origin/main' into board-controller-auth…
unenglishable Nov 3, 2023
4d85649
Merge remote-tracking branch 'origin/board-controller-authorizations'…
unenglishable Nov 6, 2023
57bc64d
test(session): use new banned role in test
unenglishable Nov 6, 2023
5855fd9
test(controllers/board): mix format
unenglishable Nov 6, 2023
fe38cd6
test(support/conn_case): fix merge
unenglishable Nov 6, 2023
0564914
Merge pull request #83 from epochtalk/board-controller-authorizations…
akinsey Nov 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions lib/epochtalk_server_web/controllers/board.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ defmodule EpochtalkServerWeb.Controllers.Board do
Used to retrieve categorized boards
"""
def by_category(conn, attrs) do
with stripped <- Validate.cast(attrs, "stripped", :boolean, default: false),
with :ok <- ACL.allow!(conn, "boards.allCategories"),
stripped <- Validate.cast(attrs, "stripped", :boolean, default: false),
user_priority <- ACL.get_user_priority(conn),
board_mapping <- BoardMapping.all(stripped: stripped),
board_moderators <- BoardModerator.all(),
Expand All @@ -36,8 +37,11 @@ defmodule EpochtalkServerWeb.Controllers.Board do
Used to find a specific board
"""
def find(conn, attrs) do
with id <- Validate.cast(attrs, "id", :integer, required: true),
with :ok <- ACL.allow!(conn, "boards.find"),
id <- Validate.cast(attrs, "id", :integer, required: true),
user_priority <- ACL.get_user_priority(conn),
{:can_read, {:ok, true}} <-
{:can_read, Board.get_read_access_by_id(id, user_priority)},
board_mapping <- BoardMapping.all(),
board_moderators <- BoardModerator.all(),
{:board, [_board]} <-
Expand All @@ -49,8 +53,22 @@ defmodule EpochtalkServerWeb.Controllers.Board do
user_priority: user_priority
})
else
{:board, []} -> ErrorHelpers.render_json_error(conn, 400, "Error, board does not exist")
_ -> ErrorHelpers.render_json_error(conn, 400, "Error, cannot fetch boards")
# if user can't read board, return 404, user doesn't need to know hidden board exists
{:can_read, {:ok, false}} ->
ErrorHelpers.render_json_error(
conn,
404,
"Board not found"
)

{:can_read, {:error, :board_does_not_exist}} ->
ErrorHelpers.render_json_error(conn, 400, "Error, board does not exist")

{:board, []} ->
ErrorHelpers.render_json_error(conn, 400, "Error, board does not exist")

_ ->
ErrorHelpers.render_json_error(conn, 400, "Error, cannot fetch boards")
end
end

Expand Down
8 changes: 4 additions & 4 deletions test/epochtalk_server/models/mention_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ defmodule Test.EpochtalkServer.Models.Mention do
assert result["mentioned_ids"] == []
end

@tag :banned
test "given a user without acl permission, errors", %{thread: thread} do
{:ok, user} = EpochtalkServer.Models.User.by_username("user")

test "given a user without acl permission, errors", %{
thread: thread,
users: %{private_user: user}
} do
attrs = %{
"thread" => thread.id,
"title" => "title",
Expand Down
10 changes: 5 additions & 5 deletions test/epochtalk_server/session_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ defmodule Test.EpochtalkServer.Session do
@tag :banned
test "without remember me, handles baninfo ttl and ban_expiration (< 1 day ttl)", %{
conn: conn,
user_attrs: %{user: user_attrs},
users: %{user: user}
user_attrs: %{banned_user: user_attrs},
users: %{banned_user: user}
} do
pre_ban_baninfo_ttl = Redix.command!(:redix, ["TTL", "user:#{user.id}:baninfo"])

Expand Down Expand Up @@ -284,8 +284,8 @@ defmodule Test.EpochtalkServer.Session do
@tag :banned
test "with remember me, handles baninfo ttl and ban_expiration (< 4 weeks ttl)", %{
conn: conn,
user_attrs: %{user: user_attrs},
users: %{user: user}
user_attrs: %{banned_user: user_attrs},
users: %{banned_user: user}
} do
pre_ban_baninfo_ttl = Redix.command!(:redix, ["TTL", "user:#{user.id}:baninfo"])

Expand Down Expand Up @@ -447,7 +447,7 @@ defmodule Test.EpochtalkServer.Session do
assert banned_resource_user.ban_expiration == @max_date
end

@tag [authenticated: true, banned: true]
@tag [authenticated: :banned, banned: true]
test "given a banned user's id, when user is unbanned, deletes banned role", %{
conn: conn,
authed_user: authed_user
Expand Down
96 changes: 95 additions & 1 deletion test/epochtalk_server_web/controllers/board_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ defmodule Test.EpochtalkServerWeb.Controllers.Board do
end

describe "by_category/2" do
@tag authenticated: :private
test "when authenticated with invalid permissions, raises InvalidPermission error", %{
conn: conn
} do
assert_raise InvalidPermission,
~r/^Forbidden, invalid permissions to perform this action/,
fn ->
get(conn, Routes.board_path(conn, :by_category))
end
end

test "finds all active boards", %{
conn: conn,
category: category,
Expand Down Expand Up @@ -130,7 +141,23 @@ defmodule Test.EpochtalkServerWeb.Controllers.Board do
assert response["message"] == "Error, board does not exist"
end

test "given an existing id, finds a board", %{conn: conn, parent_board: board} do
test "when unauthenticated, given an existing id above read access, errors", %{
conn: conn,
admin_board: admin_board
} do
response =
conn
|> get(Routes.board_path(conn, :find, admin_board.id))
|> json_response(404)

assert response["error"] == "Not Found"
assert response["message"] == "Board not found"
end

test "when unauthenticated, given an existing id within read access, finds a board", %{
conn: conn,
parent_board: board
} do
response =
conn
|> get(Routes.board_path(conn, :find, board.id))
Expand All @@ -146,6 +173,73 @@ defmodule Test.EpochtalkServerWeb.Controllers.Board do
assert response["disable_post_edit"] == board.meta["disable_post_edit"]
assert response["disable_signature"] == board.meta["disable_signature"]
end

@tag :authenticated
test "when authenticated, given an existing id above read access, errors", %{
conn: conn,
admin_board: admin_board
} do
response =
conn
|> get(Routes.board_path(conn, :find, admin_board.id))
|> json_response(404)

assert response["error"] == "Not Found"
assert response["message"] == "Board not found"
end

@tag :authenticated
test "when authenticated, given an existing id at read access, finds board", %{
conn: conn,
parent_board: board
} do
response =
conn
|> get(Routes.board_path(conn, :find, board.id))
|> json_response(200)

assert response["name"] == board.name
end

@tag authenticated: :admin
test "when authenticated as admin, given an existing id at read access, finds board", %{
conn: conn,
admin_board: admin_board
} do
response =
conn
|> get(Routes.board_path(conn, :find, admin_board.id))
|> json_response(200)

assert response["name"] == admin_board.name
end

@tag authenticated: :admin
test "when authenticated as admin, given an existing id above read access, errors", %{
conn: conn,
super_admin_board: super_admin_board
} do
response =
conn
|> get(Routes.board_path(conn, :find, super_admin_board.id))
|> json_response(404)

assert response["error"] == "Not Found"
assert response["message"] == "Board not found"
end

@tag authenticated: :super_admin
test "when authenticated as super admin, given an existing id at read access, finds board", %{
conn: conn,
super_admin_board: super_admin_board
} do
response =
conn
|> get(Routes.board_path(conn, :find, super_admin_board.id))
|> json_response(200)

assert response["name"] == super_admin_board.name
end
end

describe "slug_to_id/2" do
Expand Down
78 changes: 78 additions & 0 deletions test/seed/users.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,38 @@ test_admin_user_username = "admin"
test_admin_user_email = "[email protected]"
test_admin_user_password = "password"

test_global_mod_user_username = "globalmod"
test_global_mod_user_email = "[email protected]"
test_global_mod_user_password = "password"

test_mod_user_username = "mod"
test_mod_user_email = "[email protected]"
test_mod_user_password = "password"

test_user_username = "user"
test_user_email = "[email protected]"
test_user_password = "password"

test_patroller_user_username = "patroller"
test_patroller_user_email = "[email protected]"
test_patroller_user_password = "password"

test_newbie_user_username = "newbie"
test_newbie_user_email = "[email protected]"
test_newbie_user_password = "password"

test_banned_user_username = "banned"
test_banned_user_email = "[email protected]"
test_banned_user_password = "password"

test_anonymous_user_username = "anonymous"
test_anonymous_user_email = "[email protected]"
test_anonymous_user_password = "password"

test_private_user_username = "private"
test_private_user_email = "[email protected]"
test_private_user_password = "password"

test_no_login_user_username = "no_login"
test_no_login_user_email = "[email protected]"
test_no_login_user_password = "password"
Expand All @@ -30,12 +58,62 @@ build(:user,
)
|> with_role_id(2)

build(:user,
username: test_global_mod_user_username,
email: test_global_mod_user_email,
password: test_global_mod_user_password
)
|> with_role_id(3)

build(:user,
username: test_mod_user_username,
email: test_mod_user_email,
password: test_mod_user_password
)
|> with_role_id(4)

build(:user,
username: test_user_username,
email: test_user_email,
password: test_user_password
)

build(:user,
username: test_patroller_user_username,
email: test_patroller_user_email,
password: test_patroller_user_password
)
|> with_role_id(6)

build(:user,
username: test_newbie_user_username,
email: test_newbie_user_email,
password: test_newbie_user_password
)
|> with_role_id(7)

# TODO(akinsey): actually ban the user, this user only has banned role
build(:user,
username: test_banned_user_username,
email: test_banned_user_email,
password: test_banned_user_password
)
|> with_role_id(8)

build(:user,
username: test_anonymous_user_username,
email: test_anonymous_user_email,
password: test_anonymous_user_password
)
|> with_role_id(9)

build(:user,
username: test_private_user_username,
email: test_private_user_email,
password: test_private_user_password
)
|> with_role_id(10)

build(:user,
username: test_no_login_user_username,
email: test_no_login_user_email,
Expand Down
Loading
Loading