|
| 1 | +defmodule CodeCorps.Helpers.PolicyTest do |
| 2 | + use CodeCorps.ModelCase |
| 3 | + alias Ecto.Changeset |
| 4 | + alias CodeCorps.{ |
| 5 | + Organization, User, Helpers.Policy, |
| 6 | + ProjectUser |
| 7 | + } |
| 8 | + |
| 9 | + def create_project_user_with_role(role) do |
| 10 | + user = insert(:user) |
| 11 | + project = insert(:project) |
| 12 | + insert(:project_user, project: project, user: user, role: role) |
| 13 | + {project, user} |
| 14 | + end |
| 15 | + |
| 16 | + describe "owned_by/2" do |
| 17 | + test "returns false when organization is not owned by user" do |
| 18 | + refute Policy.owned_by?(%Organization{owner_id: 1}, %User{id: 2}) |
| 19 | + end |
| 20 | + |
| 21 | + test "returns false when invalid arguments are passed" do |
| 22 | + refute Policy.owned_by?(nil, 2) |
| 23 | + end |
| 24 | + |
| 25 | + test "returns false if a project is not owned by the user" do |
| 26 | + project = insert(:project) |
| 27 | + some_other_user = %User{id: 1} |
| 28 | + refute Policy.owned_by?(project, some_other_user) |
| 29 | + end |
| 30 | + |
| 31 | + test "returns true if a project is owned by the user" do |
| 32 | + {project, user} = create_project_user_with_role("owner") |
| 33 | + assert Policy.owned_by?(project, user) |
| 34 | + end |
| 35 | + |
| 36 | + test "returns false if a project is admined by the user" do |
| 37 | + {project, user} = create_project_user_with_role("admin") |
| 38 | + refute Policy.owned_by?(project, user) |
| 39 | + end |
| 40 | + |
| 41 | + test "returns false if a project is contributed by the user" do |
| 42 | + {project, user} = create_project_user_with_role("contributor") |
| 43 | + refute Policy.owned_by?(project, user) |
| 44 | + end |
| 45 | + |
| 46 | + test "returns false if a project user role is pending" do |
| 47 | + {project, user} = create_project_user_with_role("pending") |
| 48 | + refute Policy.owned_by?(project, user) |
| 49 | + end |
| 50 | + |
| 51 | + test "returns true when organization is owned by user" do |
| 52 | + assert Policy.owned_by?(%Organization{owner_id: 1}, %User{id: 1}) |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + describe "administered_by?/2" do |
| 57 | + |
| 58 | + test "returns false if given invalid arguments" do |
| 59 | + refute Policy.administered_by?(nil, 2) |
| 60 | + end |
| 61 | + |
| 62 | + test "returns true if the user is an admin" do |
| 63 | + {project, user} = create_project_user_with_role("admin") |
| 64 | + assert Policy.administered_by?(project, user) |
| 65 | + end |
| 66 | + |
| 67 | + test "returns true if the user is an owner" do |
| 68 | + {project, user} = create_project_user_with_role("admin") |
| 69 | + assert Policy.administered_by?(project, user) |
| 70 | + end |
| 71 | + |
| 72 | + test "returns false if the user is a contributor" do |
| 73 | + {project, user} = create_project_user_with_role("contributor") |
| 74 | + refute Policy.administered_by?(project, user) |
| 75 | + end |
| 76 | + |
| 77 | + test "returns false if the user is pending" do |
| 78 | + {project, user} = create_project_user_with_role("pending") |
| 79 | + refute Policy.administered_by?(project, user) |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + describe "contributed_by?/2" do |
| 84 | + |
| 85 | + test "returns false if given invalid arguments" do |
| 86 | + refute Policy.contributed_by?(nil, 2) |
| 87 | + end |
| 88 | + |
| 89 | + test "returns true if the user is an admin" do |
| 90 | + {project, user} = create_project_user_with_role("admin") |
| 91 | + assert Policy.contributed_by?(project, user) |
| 92 | + end |
| 93 | + |
| 94 | + test "returns true if the user is an owner" do |
| 95 | + {project, user} = create_project_user_with_role("admin") |
| 96 | + assert Policy.contributed_by?(project, user) |
| 97 | + end |
| 98 | + |
| 99 | + test "returns true if the user is a contributor" do |
| 100 | + {project, user} = create_project_user_with_role("contributor") |
| 101 | + assert Policy.contributed_by?(project, user) |
| 102 | + end |
| 103 | + |
| 104 | + test "returns false if the user is pending" do |
| 105 | + {project, user} = create_project_user_with_role("pending") |
| 106 | + refute Policy.contributed_by?(project, user) |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + describe "get_organization/1" do |
| 111 | + test "return organization if the organization_id is defined on the struct" do |
| 112 | + organization = insert(:organization) |
| 113 | + project = insert(:project, organization: organization) |
| 114 | + result = Policy.get_organization(project) |
| 115 | + assert result.id == organization.id |
| 116 | + assert result.name == organization.name |
| 117 | + end |
| 118 | + |
| 119 | + test "return organization if the organization_id is defined on the changeset" do |
| 120 | + organization = insert(:organization) |
| 121 | + changeset = %Changeset{changes: %{organization_id: organization.id}} |
| 122 | + result = Policy.get_organization(changeset) |
| 123 | + assert result.id == organization.id |
| 124 | + assert result.name == organization.name |
| 125 | + end |
| 126 | + |
| 127 | + test "return nil for structs with no organization_id" do |
| 128 | + assert Policy.get_organization(%{foo: "bar"}) == nil |
| 129 | + end |
| 130 | + |
| 131 | + test "return nil for any" do |
| 132 | + assert Policy.get_organization("foo") == nil |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + |
| 137 | + describe "get_project/1" do |
| 138 | + test "return project if the project_id is defined on the struct" do |
| 139 | + project = insert(:project) |
| 140 | + project_category = insert(:project_category, project: project) |
| 141 | + result = Policy.get_project(project_category) |
| 142 | + assert result.id == project.id |
| 143 | + assert result.title == project.title |
| 144 | + end |
| 145 | + |
| 146 | + test "return project if the project_id is defined on the changeset" do |
| 147 | + project = insert(:project) |
| 148 | + changeset = %Changeset{changes: %{project_id: project.id}} |
| 149 | + result = Policy.get_project(changeset) |
| 150 | + assert result.id == project.id |
| 151 | + assert result.title == project.title |
| 152 | + end |
| 153 | + |
| 154 | + test "return nil for structs with no project_id" do |
| 155 | + assert Policy.get_project(%{foo: "bar"}) == nil |
| 156 | + end |
| 157 | + |
| 158 | + test "return nil for any" do |
| 159 | + assert Policy.get_project("foo") == nil |
| 160 | + end |
| 161 | + end |
| 162 | + |
| 163 | + describe "get_role/1" do |
| 164 | + test "should return a project user's role if it's defined" do |
| 165 | + assert Policy.get_role(%ProjectUser{role: "admin"}) == "admin" |
| 166 | + end |
| 167 | + |
| 168 | + test "should return a changeset's role if it's defined" do |
| 169 | + assert Policy.get_role(%Changeset{data: %{role: "contributor"}, types: %{role: :string}}) == "contributor" |
| 170 | + end |
| 171 | + |
| 172 | + test "should return nil if no role is defined on a project user" do |
| 173 | + assert Policy.get_role(%ProjectUser{}) == nil |
| 174 | + end |
| 175 | + |
| 176 | + test "should return nil if no role is defined on a changeset" do |
| 177 | + assert Policy.get_role(%Changeset{data: %{role: nil}, types: %{role: :string}}) == nil |
| 178 | + end |
| 179 | + |
| 180 | + test "should return nil if nil is passed in" do |
| 181 | + assert Policy.get_role(nil) == nil |
| 182 | + end |
| 183 | + end |
| 184 | + |
| 185 | + describe "get_task/1" do |
| 186 | + test "should return task of a TaskSkill" do |
| 187 | + task = insert(:task) |
| 188 | + task_skill = insert(:task_skill, task: task) |
| 189 | + result = Policy.get_task(task_skill) |
| 190 | + assert result.id == task.id |
| 191 | + end |
| 192 | + |
| 193 | + test "should return task of a UserTask" do |
| 194 | + task = insert(:task) |
| 195 | + user_task = insert(:user_task, task: task) |
| 196 | + result = Policy.get_task(user_task) |
| 197 | + assert result.id == task.id |
| 198 | + end |
| 199 | + |
| 200 | + test "should return task of a Changeset" do |
| 201 | + task = insert(:task) |
| 202 | + changeset = %Changeset{changes: %{task_id: task.id}} |
| 203 | + result = Policy.get_task(changeset) |
| 204 | + assert result.id == task.id |
| 205 | + end |
| 206 | + end |
| 207 | + |
| 208 | + describe "task_authored_by?/1" do |
| 209 | + test "returns true if the user is the author of the task" do |
| 210 | + user = insert(:user) |
| 211 | + task = insert(:task, user: user) |
| 212 | + assert Policy.task_authored_by?(task, user) |
| 213 | + end |
| 214 | + |
| 215 | + test "returns false if the user is not the author of the task" do |
| 216 | + user = insert(:user) |
| 217 | + other_user = insert(:user) |
| 218 | + task = insert(:task, user: user) |
| 219 | + refute Policy.task_authored_by?(task, other_user) |
| 220 | + end |
| 221 | + end |
| 222 | + |
| 223 | +end |
0 commit comments