-
Notifications
You must be signed in to change notification settings - Fork 74
feat: add api for membership management #2821
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
base: main
Are you sure you want to change the base?
Changes from all commits
14260a4
6265cb7
0b02070
1a9f983
a379180
8c8e78d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,8 @@ defmodule LogflareWeb.Api.TeamControllerTest do | |||||
| @moduledoc false | ||||||
| use LogflareWeb.ConnCase | ||||||
|
|
||||||
| alias Logflare.TeamUsers | ||||||
|
|
||||||
| setup do | ||||||
| insert(:plan) | ||||||
| user = insert(:user) | ||||||
|
|
@@ -201,4 +203,100 @@ defmodule LogflareWeb.Api.TeamControllerTest do | |||||
| |> assert_schema("NotFoundResponse") == %{error: "Not Found"} | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| describe "add_member/2" do | ||||||
| test "adds an existing user to a team", %{ | ||||||
| conn: conn, | ||||||
| user: user, | ||||||
| main_team: main_team | ||||||
| } do | ||||||
| new_member = insert(:user) | ||||||
|
|
||||||
| assert conn | ||||||
| |> add_access_token(user, "private") | ||||||
| |> post(~p"/api/teams/#{main_team.token}/members", %{email: new_member.email}) | ||||||
| |> response(204) | ||||||
| |> assert_schema("AcceptedResponse") == "" | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should return created user, should also be a 201 since we're creating a team_user |
||||||
|
|
||||||
| team_users = TeamUsers.list_team_users_by(team_id: main_team.id) | ||||||
|
|
||||||
| assert Enum.any?(team_users, fn tu -> | ||||||
| tu.email == String.downcase(new_member.email) | ||||||
| end) | ||||||
| end | ||||||
|
|
||||||
| test "creates a new team member when adding with non-existent email", %{ | ||||||
| conn: conn, | ||||||
| user: user, | ||||||
| main_team: main_team | ||||||
| } do | ||||||
| new_email = "newuser@example.com" | ||||||
|
|
||||||
| assert conn | ||||||
| |> add_access_token(user, "private") | ||||||
| |> post(~p"/api/teams/#{main_team.token}/members", %{email: new_email}) | ||||||
| |> response(204) | ||||||
| |> assert_schema("AcceptedResponse") == "" | ||||||
|
|
||||||
| team_users = TeamUsers.list_team_users_by(team_id: main_team.id) | ||||||
| assert Enum.any?(team_users, fn tu -> tu.email == String.downcase(new_email) end) | ||||||
| end | ||||||
|
|
||||||
| test "returns not found if doesn't own the team", %{conn: conn, main_team: main_team} do | ||||||
| invalid_user = insert(:user) | ||||||
| new_member = insert(:user) | ||||||
|
|
||||||
| assert conn | ||||||
| |> add_access_token(invalid_user, "private") | ||||||
| |> post(~p"/api/teams/#{main_team.token}/members", %{email: new_member.email}) | ||||||
| |> json_response(404) | ||||||
| |> assert_schema("NotFoundResponse") == %{error: "Not Found"} | ||||||
| end | ||||||
|
Comment on lines
+245
to
+254
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 401 would be more appropriate. would also need a 400 test for bad team_user attrs |
||||||
| end | ||||||
|
|
||||||
| describe "remove_member/2" do | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Also needs a test for team_user with when there is no user record, which is when they are invited without having a prior account. |
||||||
| test "removes a member from a team", %{ | ||||||
| conn: conn, | ||||||
| user: user, | ||||||
| main_team: main_team | ||||||
| } do | ||||||
| member_to_remove = insert(:user) | ||||||
| insert(:team_user, team: main_team, email: member_to_remove.email) | ||||||
|
|
||||||
| assert conn | ||||||
| |> add_access_token(user, "private") | ||||||
| |> delete(~p"/api/teams/#{main_team.token}/members/#{member_to_remove.email}") | ||||||
| |> response(204) | ||||||
| |> assert_schema("AcceptedResponse") == "" | ||||||
|
|
||||||
| team_users = TeamUsers.list_team_users_by(team_id: main_team.id) | ||||||
| refute Enum.any?(team_users, fn tu -> tu.email == member_to_remove.email end) | ||||||
| end | ||||||
|
|
||||||
| test "returns not found if doesn't own the team", %{conn: conn, main_team: main_team} do | ||||||
| invalid_user = insert(:user) | ||||||
| member = insert(:user) | ||||||
| insert(:team_user, team: main_team, email: member.email) | ||||||
|
|
||||||
| assert conn | ||||||
| |> add_access_token(invalid_user, "private") | ||||||
| |> delete(~p"/api/teams/#{main_team.token}/members/#{member.email}") | ||||||
| |> json_response(404) | ||||||
| |> assert_schema("NotFoundResponse") == %{error: "Not Found"} | ||||||
| end | ||||||
|
|
||||||
| test "returns not found if team member doesn't exist", %{ | ||||||
| conn: conn, | ||||||
| user: user, | ||||||
| main_team: main_team | ||||||
| } do | ||||||
| non_existent_email = "nonexistent@example.com" | ||||||
|
|
||||||
| assert conn | ||||||
| |> add_access_token(user, "private") | ||||||
| |> delete(~p"/api/teams/#{main_team.token}/members/#{non_existent_email}") | ||||||
| |> json_response(404) | ||||||
| |> assert_schema("NotFoundResponse") == %{error: "Not Found"} | ||||||
| end | ||||||
| end | ||||||
| end | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also needs case where there is no existing user record and we only create the record on
team_userstable