penpot/backend/test/backend_tests/rpc_team_test.clj
Andrey Antukh a7f66b18f9
✨ Honor trusted alternate origins for backend links and OIDC (#11892)
* ✨ Honor trusted alternate origins for backend links and OIDC

Add PENPOT_TRUSTED_ORIGINS, an optional list of bare scheme://host[:port]
alternate origins. A request whose Origin matches (exact, trimmed and
lowercased) uses that origin as the base for links generated while serving
it: emails, asset URLs, audit and webhook link fields, and redirects.
Unlisted origins, requests without Origin, and background or server-to-server
work keep the canonical public-uri (fail-closed). Malformed entries warn at
startup and never match.

Stamp the trusted origin on the request with a global http middleware that
binds config public-uri for the request extent; expose the chain as
root-middleware so tests assert it runs before auth and errors. Build every
backend link through the canonical cf/get-public-uri.

Carry the origin in the signed OIDC state (auth and organization SSO) so the
callback, a navigation without Origin, re-applies it with with-public-uri for
the token-exchange redirect_uri and every redirect, including logout. Verify
the state token once per callback and reuse the claims; build redirect URIs
with ensure-path-slash plus join and append-query-param for hash routes. A
removed origin does not invalidate states signed before it (up to four hours).

Bring MCP OAuth into scope: wrap its routes, stage the origin in the Redis
request record at authorize time, and re-apply it in decision so issuer,
resource, the approval redirect and the code-exchange iss agree on one base.

Keep the log-capture watch installed while waiting for the async rejection
record in the save tests; a watch removed first lost late records and read
nil. Add a delayed-executor regression test that makes the race deterministic
and document the rule in the backend testing memory.

Closes #11625

AI-assisted-by: deepseek-v4.1-flash
AI-assisted-by: qwen3.7-plus
AI-assisted-by: gpt-5.6-luna
AI-assisted-by: muse-spark-1.3-contributor

* ✨ Serve trusted alternate origins across frontend and backend

Let one installation serve a canonical domain plus trusted alternate
origins, without letting request headers pick arbitrary link bases.

The backend normalizes `PENPOT_TRUSTED_ORIGINS` on load: it drops malformed
entries with a warning and warns about HTTP origins outside localhost. It
selects the first trusted origin from `X-Penpot-Origin`, then `Origin`, and
applies only scheme, host and port to the public URI, keeping the configured
path. MCP OAuth keeps the canonical base because its authorize navigation
has no usable origin.

The frontend reads the same list from `config.js`, filled by the container
entrypoint, and selects the browser origin only on an exact match. It sends
`X-Penpot-Origin` on RPC calls (including GETs and multipart) and OIDC
initiation, not on asset, font or external requests. SSE captures the request
bindings, and export jobs store the effective base so the download link uses
it after the request ends. A closed export creation schema excludes
`:public-uri`, which the backend adds only after validation.

Register the OIDC callback for each alias. Removing an alias does not stop
already signed OIDC state, queued mail or export jobs from using it.

Add regression tests across backend, frontend and the nginx entrypoint, and
document the proxy setup, worker upgrade and rollback, and alias retirement.

Refs #11625

AI-assisted-by: hy4-flash-free
AI-assisted-by: gpt-6-luna
AI-assisted-by: gpt-6.2-sol
2026-11-24 20:02:29 +01:00

2704 lines
122 KiB
Clojure

;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS SUBSIDIARY SL
(ns backend-tests.rpc-team-test
(:require
[app.common.data :as d]
[app.common.logging :as l]
[app.common.time :as ct]
[app.common.types.team :as types.team]
[app.common.uuid :as uuid]
[app.config :as cf]
[app.db :as db]
[app.email.blacklist :as email.blacklist]
[app.http :as http]
[app.loggers.audit :as audit]
[app.nitrate :as nitrate]
[app.rpc :as-alias rpc]
[app.rpc.commands.teams :as teams]
[app.rpc.helpers :as rph]
[app.storage :as sto]
[app.tokens :as tokens]
[backend-tests.helpers :as th]
[clojure.test :as t]
[datoteka.fs :as fs]
[mockery.core :refer [with-mocks]]
[yetti.request]))
(t/use-fixtures :once th/state-init)
(t/use-fixtures :each th/database-reset)
;; Minimal request stub, so the audit event can be built the way the backend
;; builds it: with the client ip in the event context.
(def ^:private http-request
(reify
yetti.request/IRequest
(get-header [_ header]
(case header
"x-forwarded-for" "127.0.0.44"
"x-real-ip" "127.0.0.43"
nil))))
(t/deftest create-team-invitations
(with-mocks [mock {:target 'app.email/send-template :return nil}]
(let [profile1 (th/create-profile* 1 {:is-active true})
profile2 (th/create-profile* 2 {:is-active true})
profile3 (th/create-profile* 3 {:is-active true :is-muted true})
team (th/create-team* 1 {:profile-id (:id profile1)})
pool (:app.db/pool th/*system*)
data {::th/type :create-team-invitations
::rpc/profile-id (:id profile1)
:team-id (:id team)
:role :editor}]
;; invite external user without complaints
(let [data (assoc data :emails ["foo@bar.com"])
out (th/command! data)
;; retrieve the value from the database and check its content
invitations (th/db-query :team-invitation
{:team-id (:team-id data)
:email-to "foo@bar.com"})]
;; (th/print-result! out)
(t/is (th/success? out))
(t/is (= 1 (:call-count (deref mock))))
(t/is (= 1 (count invitations))))
;; invite internal user without complaints
(th/reset-mock! mock)
(let [data (assoc data :emails [(:email profile2)])
out (th/command! data)]
(t/is (th/success? out))
(t/is (= 1 (:call-count (deref mock)))))
;; invite user with complaint
(th/create-global-complaint-for pool {:type :complaint :email "foo@bar.com"})
(th/reset-mock! mock)
(let [data (assoc data :emails ["foo@bar.com"])
out (th/command! data)]
(t/is (not (th/success? out)))
(t/is (= 0 (:call-count (deref mock)))))
;; get invitation token
(let [params {::th/type :get-team-invitation-token
::rpc/profile-id (:id profile1)
:team-id (:id team)
:email "foo@bar.com"}
out (th/command! params)]
(t/is (th/success? out))
(let [result (:result out)]
(contains? result :token)))
;; invite user with bounce
(th/reset-mock! mock)
(th/create-global-complaint-for pool {:type :bounce :email "foo@bar.com"})
(let [data (assoc data :emails ["foo@bar.com"])
out (th/command! data)]
(t/is (not (th/success? out)))
(t/is (= 0 (:call-count @mock)))
(let [edata (-> out :error ex-data)]
(t/is (= :restriction (:type edata)))
(t/is (= :email-has-permanent-bounces (:code edata)))))
;; invite internal user that is muted
(th/reset-mock! mock)
(let [data (assoc data :emails [(:email profile3)])
out (th/command! data)]
(t/is (not (th/success? out)))
(t/is (= 0 (:call-count @mock)))
(let [edata (-> out :error ex-data)]
(t/is (= :validation (:type edata)))
(t/is (= :member-is-muted (:code edata))))))))
(t/deftest create-and-update-team-invitations-include-organization-props
(with-mocks [email-mock {:target 'app.email/send-template :return nil}
audit-mock {:target 'app.loggers.audit/submit :return nil}]
(let [owner (th/create-profile* 101 {:is-active true})
invitee (th/create-profile* 102 {:is-active true})
organization-team (th/create-team* 101 {:profile-id (:id owner)})
plain-team (th/create-team* 102 {:profile-id (:id owner)})
organization-id (uuid/random)
organization {:id organization-id
:name "Acme"
:slug "acme"
:owner-id (:id owner)
:avatar-bg-url "https://example.com/avatar.svg"
:permissions {:new-team-members "anyone"}}
nitrate-call
(fn [_cfg method params]
(case method
:get-team-organization
(if (= (:team-id params) (:id organization-team))
{:organization organization :is-your-penpot false}
{:organization nil :is-your-penpot false})
:get-organization-members
[(:id invitee)]
nil))
invite! (fn [team email]
(th/command! {::th/type :create-team-invitations
::rpc/profile-id (:id owner)
:team-id (:id team)
:role :editor
:emails [email]}))]
(with-redefs [cf/flags (conj cf/flags :admin-console :email-verification)
nitrate/call nitrate-call]
(t/is (th/success? (invite! organization-team (:email invitee))))
(t/is (th/success? (invite! organization-team (:email invitee))))
(t/is (th/success? (invite! plain-team "external@example.com"))))
(let [events (mapv second (:call-args-list @audit-mock))
create-organization (first (filter #(and (= "create-team-invitation" (:name %))
(= (:email invitee)
(get-in % [:props :member-email])))
events))
update-organization (first (filter #(= "update-team-invitation" (:name %)) events))
create-plain (first (filter #(and (= "create-team-invitation" (:name %))
(= "external@example.com"
(get-in % [:props :member-email])))
events))]
(doseq [event [create-organization update-organization]]
(t/is (= (str (:id owner))
(get-in event [:props :user-who-send-invitation])))
(t/is (true? (get-in event [:props :team-belongs-to-organization])))
(t/is (true? (get-in event [:props :adds-invitee-to-organization])))
(t/is (true? (get-in event [:props :invitee-already-organization-member]))))
(t/is (= (str (:id owner))
(get-in create-plain [:props :user-who-send-invitation])))
(t/is (false? (get-in create-plain [:props :team-belongs-to-organization])))
(t/is (false? (get-in create-plain [:props :adds-invitee-to-organization])))
(t/is (false? (get-in create-plain [:props :invitee-already-organization-member])))))))
(t/deftest create-team-invitations-follows-send-invitations-restriction
(with-mocks [email-mock {:target 'app.email/send :return nil}
audit-mock {:target 'app.loggers.audit/submit :return nil}]
(let [org-owner (th/create-profile* 420 {:is-active true})
admin (th/create-profile* 421 {:is-active true})
invitee (th/create-profile* 422 {:is-active true})
team (th/create-team* 420 {:profile-id (:id org-owner)})
_ (th/create-team-role* {:team-id (:id team)
:profile-id (:id admin)
:role :admin})
organization-id (uuid/random)
team-org-params (atom nil)
make-organization
(fn [restrictions-met]
{:id organization-id
:name "Acme"
:slug "acme"
:owner-id (:id org-owner)
:avatar-bg-url "https://example.com/avatar.svg"
:permissions {:send-invitations "ownersAndAdmins"
:new-team-members "anyone"}
:restrictions-met restrictions-met})
make-nitrate-call
(fn [restrictions-met]
(fn [_cfg method params]
(case method
:get-team-organization
(do
(reset! team-org-params params)
(if (= (:team-id params) (:id team))
{:organization (make-organization restrictions-met) :is-your-penpot false}
{:organization nil :is-your-penpot false}))
:get-organization-members
[(:id invitee)]
nil)))
invite!
(fn []
(th/command! {::th/type :create-team-invitations
::rpc/profile-id (:id admin)
:team-id (:id team)
:role :editor
:emails [(:email invitee)]}))]
;; Restriction not met: a team admin (not the org owner) is denied.
(with-redefs [cf/flags (conj cf/flags :admin-console :email-verification)
nitrate/call (make-nitrate-call {:send-invitations false})]
(let [out (invite!)]
(t/is (not (th/success? out)))
(t/is (= :insufficient-permissions (th/ex-code (:error out))))
;; the caller's profile-id travels to Nitrate on the team lookup
(t/is (= (:id admin) (::rpc/profile-id @team-org-params)))))
;; Restriction met: the same team admin can invite.
(with-redefs [cf/flags (conj cf/flags :admin-console :email-verification)
nitrate/call (make-nitrate-call {:send-invitations true})]
(t/is (th/success? (invite!)))))))
(t/deftest create-team-invitations-fails-closed-when-organization-lookup-fails
(with-mocks [email-mock {:target 'app.email/send :return nil}
audit-mock {:target 'app.loggers.audit/submit :return nil}]
(let [owner (th/create-profile* 430 {:is-active true})
admin (th/create-profile* 431 {:is-active true})
invitee (th/create-profile* 432 {:is-active true})
team (th/create-team* 430 {:profile-id (:id owner)})
_ (th/create-team-role* {:team-id (:id team)
:profile-id (:id admin)
:role :admin})
make-nitrate-call
(fn [get-team-organization]
(fn [_cfg method params]
(case method
:get-team-organization (get-team-organization params)
nil)))
invite!
(fn []
(th/command! {::th/type :create-team-invitations
::rpc/profile-id (:id admin)
:team-id (:id team)
:role :editor
:emails [(:email invitee)]}))]
;; A 4xx other than 404 must not look like a team without organization.
(with-redefs [cf/flags (conj cf/flags :admin-console :email-verification)
nitrate/call (make-nitrate-call
(fn [_]
(throw (ex-info "forbidden"
{:type :nitrate-http-error
:status 403}))))]
(let [out (invite!)]
(t/is (not (th/success? out)))
(t/is (= :nitrate-http-error (th/ex-type (:error out))))))
;; An invalid response body (the client returns nil) is rejected too.
(with-redefs [cf/flags (conj cf/flags :admin-console :email-verification)
nitrate/call (make-nitrate-call (constantly nil))]
(let [out (invite!)]
(t/is (not (th/success? out)))
(t/is (= :nitrate-unavailable (th/ex-type (:error out))))))
;; A 404 confirms the team has no organization: team-level rules apply.
(let [lookups (atom [])]
(with-redefs [cf/flags (conj cf/flags :admin-console :email-verification)
nitrate/call (make-nitrate-call
(fn [params]
(swap! lookups conj params)
(throw (ex-info "not found"
{:type :nitrate-http-error
:status 404}))))]
(t/is (th/success? (invite!)))
;; one strict lookup serves every organization rule
(t/is (= [true] (mapv :throw-on-error? @lookups))))))))
(t/deftest create-team-invitations-blacklisted-domain
(with-mocks [mock {:target 'app.email/send-template :return nil}]
(let [profile1 (th/create-profile* 1 {:is-active true})
team (th/create-team* 1 {:profile-id (:id profile1)})
data {::th/type :create-team-invitations
::rpc/profile-id (:id profile1)
:team-id (:id team)
:role :editor}]
;; invite from a directly blacklisted domain should fail
(with-redefs [email.blacklist/enabled? (constantly true)
email.blacklist/contains? (fn [_ email]
(clojure.string/ends-with? email "@blacklisted.com"))]
(let [out (th/command! (assoc data :emails ["user@blacklisted.com"]))]
(t/is (not (th/success? out)))
(t/is (= 0 (:call-count @mock)))
(let [edata (-> out :error ex-data)]
(t/is (= :restriction (:type edata)))
(t/is (= :email-domain-is-not-allowed (:code edata))))))
;; invite from a subdomain of a blacklisted domain should also fail
(th/reset-mock! mock)
(with-redefs [email.blacklist/enabled? (constantly true)
email.blacklist/contains? (fn [_ email]
(clojure.string/ends-with? email "@sub.blacklisted.com"))]
(let [out (th/command! (assoc data :emails ["user@sub.blacklisted.com"]))]
(t/is (not (th/success? out)))
(t/is (= 0 (:call-count @mock)))
(let [edata (-> out :error ex-data)]
(t/is (= :restriction (:type edata)))
(t/is (= :email-domain-is-not-allowed (:code edata))))))
;; invite from a non-blacklisted domain should succeed
(th/reset-mock! mock)
(with-redefs [email.blacklist/enabled? (constantly true)
email.blacklist/contains? (constantly false)]
(let [out (th/command! (assoc data :emails ["user@allowed.com"]))]
(t/is (th/success? out))
(t/is (= 1 (:call-count @mock))))))))
(t/deftest create-team-invitations-with-request-access
(with-mocks [mock {:target 'app.email/send-template :return nil}]
(let [profile1 (th/create-profile* 1 {:is-active true})
requester (th/create-profile* 2 {:is-active true :email "requester@example.com"})
team (th/create-team* 1 {:profile-id (:id profile1)})
proj (th/create-project* 1 {:profile-id (:id profile1)
:team-id (:id team)})
file (th/create-file* 1 {:profile-id (:id profile1)
:project-id (:id proj)})]
(let [data {::th/type :create-team-access-request
::rpc/profile-id (:id requester)
:file-id (:id file)}
out (th/command! data)]
(t/is (th/success? out))
(t/is (= 1 (:call-count @mock))))
(th/reset-mock! mock)
(let [data {::th/type :create-team-invitations
::rpc/profile-id (:id profile1)
:team-id (:id team)
:role :editor
:emails ["requester@example.com"]}
out (th/command! data)]
(t/is (th/success? out))
(t/is (= 1 (:call-count @mock)))
;; Check that request is properly removed
(let [requests (th/db-query :team-access-request
{:requester-id (:id requester)})]
(t/is (= 0 (count requests))))
(let [rows (th/db-query :team-profile-rel {:team-id (:id team)})]
(t/is (= 2 (count rows))))))))
(t/deftest create-team-invitations-with-request-access-2
(with-mocks [mock {:target 'app.email/send-template :return nil}]
(let [profile1 (th/create-profile* 1 {:is-active true})
requester (th/create-profile* 2 {:is-active true
:email "requester@example.com"})
team (th/create-team* 1 {:profile-id (:id profile1)})
proj (th/create-project* 1 {:profile-id (:id profile1)
:team-id (:id team)})
file (th/create-file* 1 {:profile-id (:id profile1)
:project-id (:id proj)})]
;; Create the first access request
(let [data {::th/type :create-team-access-request
::rpc/profile-id (:id requester)
:file-id (:id file)}
out (th/command! data)]
(t/is (th/success? out))
(t/is (= 1 (:call-count @mock))))
(th/reset-mock! mock)
;; Proceed to delete the requester user
(th/db-update! :profile
{:deleted-at (ct/in-past "1h")}
{:id (:id requester)})
;; Create a new profile with the same email
(let [requester' (th/create-profile* 3 {:is-active true :email "requester@example.com"})]
;; Create a request access with new requester
(let [data {::th/type :create-team-access-request
::rpc/profile-id (:id requester')
:file-id (:id file)}
out (th/command! data)]
(t/is (th/success? out))
(t/is (= 1 (:call-count @mock))))
(th/reset-mock! mock)
;; Create an invitation for the requester email
(let [data {::th/type :create-team-invitations
::rpc/profile-id (:id profile1)
:team-id (:id team)
:role :editor
:emails ["requester@example.com"]}
out (th/command! data)]
(t/is (th/success? out))
(t/is (= 1 (:call-count @mock))))
;; Check that request is properly removed
(let [requests (th/db-query :team-access-request
{:requester-id (:id requester')})]
(t/is (= 0 (count requests))))
(let [[r1 r2 :as rows] (th/db-query :team-profile-rel
{:team-id (:id team)}
{:order-by [:created-at]})]
(t/is (= 2 (count rows)))
(t/is (= (:profile-id r1) (:id profile1)))
(t/is (= (:profile-id r2) (:id requester'))))))))
(t/deftest invitation-tokens
(with-mocks [mock {:target 'app.email/send-template :return nil}]
(let [profile1 (th/create-profile* 1 {:is-active true})
profile2 (th/create-profile* 2 {:is-active true})
team (th/create-team* 1 {:profile-id (:id profile1)})
pool (:app.db/pool th/*system*)]
;; Try to invite a not existing user
(let [data {::th/type :create-team-invitations
::rpc/profile-id (:id profile1)
:emails ["notexisting@example.com"]
:team-id (:id team)
:role :editor}
out (th/command! data)]
;; (th/print-result! out)
(t/is (th/success? out))
(t/is (= 1 (:call-count @mock)))
(t/is (= 1 (-> out :result :total)))
(let [token (-> out :result :invitations first)
claims (tokens/decode th/*system* token)]
(t/is (= :team-invitation (:iss claims)))
(t/is (= (:id profile1) (:profile-id claims)))
(t/is (= :editor (:role claims)))
(t/is (= (:id team) (:team-id claims)))
(t/is (= (first (:emails data)) (:member-email claims)))
(t/is (nil? (:member-id claims)))))
(th/reset-mock! mock)
;; Try to invite existing user
(let [data {::th/type :create-team-invitations
::rpc/profile-id (:id profile1)
:emails [(:email profile2)]
:team-id (:id team)
:role :editor}
out (th/command! data)]
;; (th/print-result! out)
(t/is (th/success? out))
(t/is (= 1 (:call-count @mock)))
(t/is (= 1 (-> out :result :total)))
(let [token (-> out :result :invitations first)
claims (tokens/decode th/*system* token)]
(t/is (= :team-invitation (:iss claims)))
(t/is (= (:id profile1) (:profile-id claims)))
(t/is (= :editor (:role claims)))
(t/is (= (:id team) (:team-id claims)))
(t/is (= (first (:emails data)) (:member-email claims)))
(t/is (= (:id profile2) (:member-id claims))))))))
(t/deftest get-team-invitation-token-requires-admin-permissions
;; The token is a bearer credential: whoever holds it accepts the
;; invitation with the role of the invitation row. Minting it takes the
;; same permissions as the rest of the invitation commands, so an editor
;; (the lowest role above viewer) must not get it.
(let [owner (th/create-profile* 1 {:is-active true})
viewer (th/create-profile* 2 {:is-active true})
editor (th/create-profile* 3 {:is-active true})
admin (th/create-profile* 4 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})
pool (:app.db/pool th/*system*)]
(th/create-team-role* {:team-id (:id team)
:profile-id (:id viewer)
:role :viewer})
(th/create-team-role* {:team-id (:id team)
:profile-id (:id editor)
:role :editor})
(th/create-team-role* {:team-id (:id team)
:profile-id (:id admin)
:role :admin})
(db/insert! pool :team-invitation
{:team-id (:id team)
:email-to "victim@example.com"
:role "editor"
:created-by (:id owner)
:valid-until (ct/in-future "48h")})
(let [request (fn [profile-id]
{::th/type :get-team-invitation-token
::rpc/profile-id (:id profile-id)
:team-id (:id team)
:email "victim@example.com"})]
(t/testing "a viewer cannot get the token"
(let [out (th/command! (request viewer))]
(t/is (not (th/success? out)))
(t/is (= :validation (-> out :error ex-data :type)))
(t/is (th/ex-of-code? (:error out) :insufficient-permissions))))
(t/testing "an editor cannot get the token"
(let [out (th/command! (request editor))]
(t/is (not (th/success? out)))
(t/is (= :validation (-> out :error ex-data :type)))
(t/is (th/ex-of-code? (:error out) :insufficient-permissions))))
(t/testing "an admin can get the token"
(let [out (th/command! (request admin))]
(t/is (th/success? out))
(let [claims (tokens/decode th/*system* (-> out :result :token))]
(t/is (= :team-invitation (:iss claims)))
(t/is (= :editor (:role claims)))
(t/is (= (:id team) (:team-id claims)))
(t/is (= "victim@example.com" (:member-email claims))))))
(t/testing "the team owner can get the token"
(let [out (th/command! (request owner))]
(t/is (th/success? out)))))))
(t/deftest get-team-invitation-token-owner-invitation-requires-team-owner
;; An admin cannot create nor promote an owner invitation, so it cannot
;; mint the token of one either: only the team owner can.
(let [owner (th/create-profile* 1 {:is-active true})
admin (th/create-profile* 2 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})
pool (:app.db/pool th/*system*)]
(th/create-team-role* {:team-id (:id team)
:profile-id (:id admin)
:role :admin})
(db/insert! pool :team-invitation
{:team-id (:id team)
:email-to "new-owner@example.com"
:role "owner"
:created-by (:id owner)
:valid-until (ct/in-future "48h")})
(let [request (fn [profile-id]
{::th/type :get-team-invitation-token
::rpc/profile-id (:id profile-id)
:team-id (:id team)
:email "new-owner@example.com"})]
(t/testing "an admin cannot get the token of an owner invitation"
(let [out (th/command! (request admin))]
(t/is (not (th/success? out)))
(t/is (th/ex-of-code? (:error out) :cant-promote-to-owner))))
(t/testing "the team owner can get the token of an owner invitation"
(let [out (th/command! (request owner))]
(t/is (th/success? out))
(let [claims (tokens/decode th/*system* (-> out :result :token))]
(t/is (= :owner (:role claims)))
(t/is (= "new-owner@example.com" (:member-email claims)))))))))
(t/deftest get-team-invitation-token-emits-audit-event
;; Minting the token is an action, not a read: the event is named as such
;; and carries the granted role, so the extraction of the token does not go
;; unnoticed in the audit log.
(let [owner (th/create-profile* 1 {:is-active true})
admin (th/create-profile* 2 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})
pool (:app.db/pool th/*system*)]
(th/create-team-role* {:team-id (:id team)
:profile-id (:id admin)
:role :admin})
(let [invitation (db/insert! pool :team-invitation
{:team-id (:id team)
:email-to "victim@example.com"
:role "editor"
:created-by (:id owner)
:valid-until (ct/in-future "48h")})]
(let [params {::rpc/profile-id (:id admin)
:team-id (:id team)
:email "victim@example.com"}
result (th/command-raw! (assoc params ::th/type :get-team-invitation-token))
[mdata _] (get-in th/*system* [:app.rpc/methods :get-team-invitation-token])
;; The cfg of a command carries its type, the same way
;; `resolve-methods` does it in production.
event (audit/prepare-rpc-event (assoc th/*system* ::rpc/module "main"
::rpc/type "command")
mdata
(with-meta params {:app.http/request http-request})
result)]
(t/is (= "create-team-invitation-token" (:name event)))
(t/is (= "command" (:type event)))
(t/is (= (:id admin) (:profile-id event)))
(t/is (= (:id team) (get-in event [:props :team-id])))
(t/is (= "victim@example.com" (get-in event [:props :email])))
(t/is (= :editor (get-in event [:props :role])))
(t/is (= (:id invitation) (get-in event [:props :invitation-id])))
(t/is (not (contains? (:props event) :token)))))))
(t/deftest get-team-invitations-requires-admin-permissions
;; The pending invitations carry the email of the invited person and the
;; role offered to them: the same data the rest of the invitation
;; commands already require admin for.
(let [owner (th/create-profile* 1 {:is-active true})
viewer (th/create-profile* 2 {:is-active true})
editor (th/create-profile* 3 {:is-active true})
admin (th/create-profile* 4 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})
pool (:app.db/pool th/*system*)]
(th/create-team-role* {:team-id (:id team)
:profile-id (:id viewer)
:role :viewer})
(th/create-team-role* {:team-id (:id team)
:profile-id (:id editor)
:role :editor})
(th/create-team-role* {:team-id (:id team)
:profile-id (:id admin)
:role :admin})
(db/insert! pool :team-invitation
{:team-id (:id team)
:email-to "victim@example.com"
:role "editor"
:created-by (:id owner)
:valid-until (ct/in-future "48h")})
(let [request (fn [profile-id]
{::th/type :get-team-invitations
::rpc/profile-id (:id profile-id)
:team-id (:id team)})]
(t/testing "a viewer cannot list the pending invitations"
(let [out (th/command! (request viewer))]
(t/is (not (th/success? out)))
(t/is (= :validation (-> out :error ex-data :type)))
(t/is (th/ex-of-code? (:error out) :insufficient-permissions))))
(t/testing "an editor cannot list the pending invitations"
(let [out (th/command! (request editor))]
(t/is (not (th/success? out)))
(t/is (= :validation (-> out :error ex-data :type)))
(t/is (th/ex-of-code? (:error out) :insufficient-permissions))))
(t/testing "an admin can list the pending invitations"
(let [out (th/command! (request admin))]
(t/is (th/success? out))
(t/is (= 1 (count (:result out))))
(t/is (= "victim@example.com" (:email (first (:result out)))))))
(t/testing "the team owner can list the pending invitations"
(let [out (th/command! (request owner))]
(t/is (th/success? out))
(t/is (= 1 (count (:result out)))))))))
(t/deftest invitation-permissions-deny-non-members
;; A profile that is not a member of the team has no permissions row at
;; all, so the check gets nil instead of a map of flags. That is also
;; the case of an organization owner that is not a member of the team:
;; the team permissions lookup is membership only, so they are left out
;; of the invitation commands too.
;;
;; NOTE: the audit wrapper is installed when the system boots, and only
;; when the audit log, webhooks or telemetry flags are on, none of them
;; is on in the test environment. So there is no way to observe here
;; whether a denied call emits an event: that is a property of the
;; wrapper, shared by every command.
(let [owner (th/create-profile* 1 {:is-active true})
outsider (th/create-profile* 2 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})
pool (:app.db/pool th/*system*)]
(db/insert! pool :team-invitation
{:team-id (:id team)
:email-to "victim@example.com"
:role "editor"
:created-by (:id owner)
:valid-until (ct/in-future "48h")})
(t/testing "a non member cannot get the token"
(let [out (th/command! {::th/type :get-team-invitation-token
::rpc/profile-id (:id outsider)
:team-id (:id team)
:email "victim@example.com"})]
(t/is (not (th/success? out)))
(t/is (= :validation (-> out :error ex-data :type)))
(t/is (th/ex-of-code? (:error out) :insufficient-permissions))))
(t/testing "a non member cannot list the pending invitations"
(let [out (th/command! {::th/type :get-team-invitations
::rpc/profile-id (:id outsider)
:team-id (:id team)})]
(t/is (not (th/success? out)))
(t/is (= :validation (-> out :error ex-data :type)))
(t/is (th/ex-of-code? (:error out) :insufficient-permissions))))))
(t/deftest accept-invitation-tokens
(let [profile1 (th/create-profile* 1 {:is-active true})
profile2 (th/create-profile* 2 {:is-active true})
profile3 (th/create-profile* 3 {:is-active true})
team (th/create-team* 1 {:profile-id (:id profile1)})
pool (:app.db/pool th/*system*)]
(let [token (tokens/generate th/*system*
{:iss :team-invitation
:exp (ct/in-future "1h")
:profile-id (:id profile1)
:role :editor
:team-id (:id team)
:member-email (:email profile2)
:member-id (:id profile2)})]
(t/testing "Verify token as anonymous user"
(db/insert! pool :team-invitation
{:team-id (:id team)
:email-to (:email profile2)
:role "editor"
:valid-until (ct/in-future "48h")})
(let [data {::th/type :verify-token :token token}
out (th/command! data)]
;; (th/print-result! out)
(t/is (th/success? out))
(let [result (:result out)]
(t/is (contains? result :invitation-token))
(t/is (contains? result :iss))
(t/is (contains? result :redirect-to))
(t/is (contains? result :state))
(t/is (= :pending (:state result)))
(t/is (= :auth-login (:redirect-to result))))
(let [rows (db/query pool :team-profile-rel {:team-id (:id team)})]
(t/is (= 1 (count rows))))))
;; Clean members
(db/delete! pool :team-profile-rel
{:team-id (:id team)
:profile-id (:id profile2)})
(t/testing "Verify token as logged-in user"
(let [data {::th/type :verify-token
::rpc/profile-id (:id profile2)
:token token}
out (th/command! data)]
;; (th/print-result! out)
(t/is (th/success? out))
(let [result (:result out)]
(t/is (= :created (:state result)))
(t/is (= (:email profile2) (:member-email result)))
(t/is (= (:id profile2) (:member-id result))))
(let [rows (db/query pool :team-profile-rel {:team-id (:id team)})]
(t/is (= 2 (count rows))))))
(t/testing "Verify token as logged-in wrong user"
(db/insert! pool :team-invitation
{:team-id (:id team)
:email-to (:email profile3)
:role "editor"
:valid-until (ct/in-future "48h")})
(let [data {::th/type :verify-token
::rpc/profile-id (:id profile1)
:token token}
out (th/command! data)]
;; (th/print-result! out)
(t/is (not (th/success? out)))
(let [edata (-> out :error ex-data)]
(t/is (= :validation (:type edata)))
(t/is (= :invalid-token (:code edata)))))))))
(t/deftest accept-organization-invitation-audit-event
(with-mocks [audit-mock {:target 'app.loggers.audit/submit :return nil}]
(let [inviter (th/create-profile* 201 {:is-active true})
invitee (th/create-profile* 202 {:is-active true})
team (th/create-team* 201 {:profile-id (:id inviter)})
organization-id (uuid/random)
default-team-id (uuid/random)
direct-token (tokens/generate
th/*system*
{:iss :team-invitation
:exp (ct/in-future "1h")
:profile-id (:id inviter)
:role :editor
:organization-id organization-id
:member-email (:email invitee)
:member-id (:id invitee)})
team-token (tokens/generate
th/*system*
{:iss :team-invitation
:exp (ct/in-future "1h")
:profile-id (:id inviter)
:role :editor
:team-id (:id team)
:member-email (:email invitee)
:member-id (:id invitee)})
verify! (fn [token]
(th/command! {::th/type :verify-token
::rpc/profile-id (:id invitee)
:token token}))
emitted-events
(fn [event-name]
(->> (:call-args-list @audit-mock)
(map second)
(filter #(= event-name (:name %)))))
organization-event
(fn []
(first (emitted-events "accept-organization-invitation")))]
(db/insert! (:app.db/pool th/*system*)
:team-invitation
{:org-id organization-id
:email-to (:email invitee)
:created-by (:id inviter)
:role "editor"
:valid-until (ct/in-future "48h")})
(with-redefs [cf/flags (conj cf/flags :admin-console)
nitrate/call
(fn [_cfg method _params]
(case method
:get-organization-membership {:organization-id organization-id
:is-member false}
:get-organization-members [(:id inviter) (uuid/random) (uuid/random)]
nil))
teams/initialize-user-in-organization
(fn [& _] default-team-id)]
(let [out (verify! direct-token)]
(t/is (th/success? out))
(t/is (not (contains? (:result out) :organization-invitation-audit)))))
(let [event (organization-event)
from-event (first (emitted-events "accept-organization-invitation-from"))]
(t/is (= organization-id (get-in event [:props :organization-id])))
(t/is (= (:id invitee) (get-in event [:props :profile-id])))
(t/is (= (:id inviter) (get-in event [:props :invited-by])))
(t/is (= (:email invitee) (get-in event [:props :profile-email])))
(t/is (= :editor (get-in event [:props :role])))
(t/is (uuid? (get-in event [:props :invitation-id])))
(t/is (= "direct-organization-invitation"
(get-in event [:props :organization-member-add-source])))
(t/is (false? (get-in event [:props :belongs-to-team-on-add])))
(t/is (= 3 (get-in event [:props :organization-member-count-before])))
(t/is (not (contains? (:props event) :invitation-origin)))
(t/is (not (contains? (:context event) :event-origin)))
(t/is (= 1 (count (emitted-events "accept-organization-invitation"))))
(t/is (= 1 (count (emitted-events "accept-organization-invitation-from"))))
(t/is (= (:id inviter) (:profile-id from-event)))
(t/is (= (:props event) (:props from-event)))
(t/is (not-any? #(contains? #{"accept-team-invitation"
"accept-team-invitation-from"}
(:name (second %)))
(:call-args-list @audit-mock))))
;; Legacy org invitation with no :created-by → invitee event only, no -from.
(th/reset-mock! audit-mock)
(db/insert! (:app.db/pool th/*system*)
:team-invitation
{:org-id organization-id
:email-to (:email invitee)
:role "editor"
:valid-until (ct/in-future "48h")})
(with-redefs [cf/flags (conj cf/flags :admin-console)
nitrate/call
(fn [_cfg method _params]
(case method
:get-organization-membership {:organization-id organization-id
:is-member false}
:get-organization-members [(:id inviter) (uuid/random) (uuid/random)]
nil))
teams/initialize-user-in-organization
(fn [& _] default-team-id)]
(let [out (verify! direct-token)]
(t/is (th/success? out))))
(t/is (= 1 (count (emitted-events "accept-organization-invitation"))))
(t/is (empty? (emitted-events "accept-organization-invitation-from")))
(th/reset-mock! audit-mock)
(db/insert! (:app.db/pool th/*system*)
:team-invitation
{:team-id (:id team)
:email-to (:email invitee)
:created-by (:id inviter)
:role "editor"
:valid-until (ct/in-future "48h")})
(with-redefs [cf/flags (conj cf/flags :admin-console)
nitrate/call
(fn [_cfg method _params]
(case method
:get-organization-membership-by-team {:organization-id organization-id
:is-member false}
:get-organization-members (into [(:id inviter)]
(repeatedly 4 uuid/random))
nil))
teams/add-profile-to-team! (fn [& _] nil)]
(let [out (verify! team-token)]
(t/is (th/success? out))
(t/is (not (contains? (:result out) :organization-invitation-audit)))))
(let [events (mapv second (:call-args-list @audit-mock))
event (organization-event)
org-from-event (first (emitted-events "accept-organization-invitation-from"))]
(t/is (some #(= "accept-team-invitation" (:name %)) events))
(t/is (some #(= "accept-team-invitation-from" (:name %)) events))
(t/is (= (:id team) (get-in event [:props :team-id])))
(t/is (= organization-id (get-in event [:props :organization-id])))
(t/is (= (:id invitee) (get-in event [:props :profile-id])))
(t/is (= (:id inviter) (get-in event [:props :invited-by])))
(t/is (= "team-invitation"
(get-in event [:props :organization-member-add-source])))
(t/is (true? (get-in event [:props :belongs-to-team-on-add])))
(t/is (= 5 (get-in event [:props :organization-member-count-before])))
(t/is (not (contains? (:props event) :invitation-origin)))
(t/is (not (contains? (:context event) :event-origin)))
(t/is (= 1 (count (emitted-events "accept-organization-invitation"))))
(t/is (= 1 (count (emitted-events "accept-organization-invitation-from"))))
(t/is (= (:id inviter) (:profile-id org-from-event)))
(t/is (= (:props event) (:props org-from-event))))
(let [from-event (first (emitted-events "accept-team-invitation-from"))]
(t/is (= (:id team) (get-in from-event [:props :team-id])))
(t/is (= :editor (get-in from-event [:props :role])))
(t/is (uuid? (get-in from-event [:props :invitation-id])))
(t/is (= (:id inviter) (get-in from-event [:props :invited-by])))
(t/is (= (:id invitee) (get-in from-event [:props :profile-id])))
(t/is (= (:email invitee) (get-in from-event [:props :profile-email])))
(t/is (not (contains? (get-in from-event [:props]) :email)))
(t/is (not (contains? (get-in from-event [:props]) :user-id)))
(t/is (not (contains? (get-in from-event [:props])
:user-who-send-invitation))))
(th/reset-mock! audit-mock)
(db/insert! (:app.db/pool th/*system*)
:team-invitation
{:team-id (:id team)
:email-to (:email invitee)
:role "editor"
:valid-until (ct/in-future "48h")})
(with-redefs [cf/flags (conj cf/flags :admin-console)
nitrate/call
(fn [_cfg method _params]
(case method
:get-organization-membership-by-team {:organization-id organization-id
:is-member true}
:get-organization-members (throw (ex-info "unexpected member count" {}))
nil))
teams/add-profile-to-team! (fn [& _] nil)]
(let [out (verify! team-token)]
(t/is (th/success? out))
(t/is (not (contains? (:result out) :organization-invitation-audit)))))
(let [events (mapv second (:call-args-list @audit-mock))]
(t/is (some #(= "accept-team-invitation" (:name %)) events))
(t/is (not-any? #(= "accept-organization-invitation" (:name %)) events))))))
(t/deftest create-team-invitations-with-email-verification-disabled
(with-mocks [mock {:target 'app.email/send-template :return nil}]
(let [profile1 (th/create-profile* 1 {:is-active true})
profile2 (th/create-profile* 2 {:is-active true})
profile3 (th/create-profile* 3 {:is-active true :is-muted true})
team (th/create-team* 1 {:profile-id (:id profile1)})
pool (:app.db/pool th/*system*)
data {::th/type :create-team-invitations
::rpc/profile-id (:id profile1)
:team-id (:id team)
:role :editor}]
;; invite internal user without complaints
(with-redefs [app.config/flags #{}]
(th/reset-mock! mock)
(let [data (assoc data :emails [(:email profile2)])
out (th/command! data)]
;; (th/print-result! out)
(t/is (th/success? out))
(t/is (= 0 (:call-count (deref mock)))))
(let [members (db/query pool :team-profile-rel
{:team-id (:id team)
:profile-id (:id profile2)})]
(t/is (= 1 (count members)))
(t/is (true? (-> members first :can-edit))))))))
(t/deftest query-team-invitations
(let [prof (th/create-profile* 1 {:is-active true})
team (th/create-team* 1 {:profile-id (:id prof)})
data {::th/type :get-team-invitations
::rpc/profile-id (:id prof)
:team-id (:id team)}]
;; insert an entry on the database with an enabled invitation
(db/insert! th/*pool* :team-invitation
{:team-id (:team-id data)
:email-to "test1@mail.com"
:role "editor"
:valid-until (ct/in-future "48h")})
;; insert an entry on the database with an expired invitation
(db/insert! th/*pool* :team-invitation
{:team-id (:team-id data)
:email-to "test2@mail.com"
:role "editor"
:valid-until (ct/in-past "48h")})
(let [out (th/command! data)]
(t/is (th/success? out))
(let [result (:result out)
one (first result)
two (second result)]
(t/is (= 2 (count result)))
(t/is (= "test1@mail.com" (:email one)))
(t/is (= "test2@mail.com" (:email two)))
(t/is (false? (:expired one)))
(t/is (true? (:expired two)))))))
(t/deftest update-team-invitation-role
(let [prof (th/create-profile* 1 {:is-active true})
team (th/create-team* 1 {:profile-id (:id prof)})
data {::th/type :update-team-invitation-role
::rpc/profile-id (:id prof)
:team-id (:id team)
:email "TEST1@mail.com"
:role :admin}]
;; insert an entry on the database with an invitation
(db/insert! th/*pool* :team-invitation
{:team-id (:team-id data)
:email-to "test1@mail.com"
:role "editor"
:valid-until (ct/in-future "48h")})
(let [out (th/command! data)
;; retrieve the value from the database and check its content
res (db/get* th/*pool* :team-invitation
{:team-id (:team-id data) :email-to "test1@mail.com"})]
(t/is (th/success? out))
(t/is (nil? (:result out)))
(t/is (= "admin" (:role res))))))
(t/deftest delete-team-invitation
(let [prof (th/create-profile* 1 {:is-active true})
team (th/create-team* 1 {:profile-id (:id prof)})
data {::th/type :delete-team-invitation
::rpc/profile-id (:id prof)
:team-id (:id team)
:email "TEST1@mail.com"}]
;; insert an entry on the database with an invitation
(db/insert! th/*pool* :team-invitation
{:team-id (:team-id data)
:email-to "test1@mail.com"
:role "editor"
:valid-until (ct/in-future "48h")})
(let [out (th/command! data)
;; retrieve the value from the database and check its content
res (db/get* th/*pool* :team-invitation
{:team-id (:team-id data) :email-to "test1@mail.com"})]
(t/is (th/success? out))
(t/is (nil? (:result out)))
(t/is (nil? res)))))
;; The organization `send-invitations` policy covers the pending invitations
;; too: changing their role, deleting them and getting their link manage the
;; same access that sending one grants.
(def ^:private pending-invitation-email "pending@example.com")
(defn- reset-pending-invitation
"Leaves one pending `editor` invitation in the team, whatever the previous
scenario did to it."
[team-id]
(db/delete! th/*pool* :team-invitation
{:team-id team-id :email-to pending-invitation-email})
(db/insert! th/*pool* :team-invitation
{:team-id team-id
:email-to pending-invitation-email
:role "editor"
:valid-until (ct/in-future "48h")}))
(defn- get-pending-invitation-role
[team-id]
(:role (db/get* th/*pool* :team-invitation
{:team-id team-id :email-to pending-invitation-email})))
(defn- organization-with-policy
"A team organization lookup that answers with an organization whose
`send-invitations` value and attribute restriction answer are the given
ones."
[owner-id send-invitations restrictions-met]
(fn [_params]
{:organization {:id (uuid/random)
:name "Acme"
:slug "acme"
:owner-id owner-id
:avatar-bg-url "https://example.com/avatar.svg"
:permissions {:send-invitations send-invitations}
:restrictions-met {:send-invitations restrictions-met}}
:is-your-penpot false}))
(defn- organization-lookup-failure
[status]
(fn [_params]
(throw (ex-info "nitrate error" {:type :nitrate-http-error
:status status}))))
(defn- check-follows-send-invitations-policy
"Runs the invitation `command` (a params map without profile, team and
email) under each organization policy and checks who may go through."
[command]
(let [owner (th/create-profile* 440 {:is-active true})
admin (th/create-profile* 441 {:is-active true})
team (th/create-team* 440 {:profile-id (:id owner)})
_ (th/create-team-role* {:team-id (:id team)
:profile-id (:id admin)
:role :admin})
team-id (:id team)
execute
(fn [profile get-team-organization]
(reset-pending-invitation team-id)
(with-redefs [cf/flags (conj cf/flags :admin-console)
nitrate/call (fn [_cfg method params]
(case method
:get-team-organization (get-team-organization params)
nil))]
(th/command! (assoc command
::rpc/profile-id (:id profile)
:team-id team-id
:email pending-invitation-email))))]
(t/testing "an admin is denied when only team owners may invite"
(let [out (execute admin (organization-with-policy (:id owner) "owners" true))]
(t/is (= :insufficient-permissions (th/ex-code (:error out))))
(t/is (= "editor" (get-pending-invitation-role team-id)))))
(t/testing "an admin is denied when an attribute restriction leaves them out"
(let [out (execute admin (organization-with-policy (:id owner) "ownersAndAdmins" false))]
(t/is (= :insufficient-permissions (th/ex-code (:error out))))
(t/is (= "editor" (get-pending-invitation-role team-id)))))
(t/testing "a failed organization lookup denies the admin"
(let [out (execute admin (organization-lookup-failure 403))]
(t/is (= :nitrate-http-error (th/ex-type (:error out))))
(t/is (= "editor" (get-pending-invitation-role team-id)))))
(t/testing "a team owner is allowed when only team owners may invite"
(t/is (th/success? (execute owner (organization-with-policy (:id owner) "owners" true)))))
(t/testing "an admin is allowed when the policy and the restriction include them"
(t/is (th/success? (execute admin (organization-with-policy (:id owner) "ownersAndAdmins" true)))))
(t/testing "an admin is allowed in a team without organization"
(t/is (th/success? (execute admin (organization-lookup-failure 404)))))))
(t/deftest update-team-invitation-role-follows-send-invitations-policy
(check-follows-send-invitations-policy
{::th/type :update-team-invitation-role
:role :admin}))
(t/deftest delete-team-invitation-follows-send-invitations-policy
(check-follows-send-invitations-policy
{::th/type :delete-team-invitation}))
(t/deftest get-team-invitation-token-follows-send-invitations-policy
(check-follows-send-invitations-policy
{::th/type :get-team-invitation-token}))
(t/deftest get-owned-teams
(let [profile1 (th/create-profile* 1 {:is-active true})
profile2 (th/create-profile* 2 {:is-active true})
team1 (th/create-team* 1 {:profile-id (:id profile1)})
team2 (th/create-team* 2 {:profile-id (:id profile2)})
params {::th/type :get-owned-teams
::rpc/profile-id (:id profile1)}
out (th/command! params)]
;; (th/print-result! out)
(t/is (th/success? out))
(let [[item1 :as result] (:result out)]
(t/is (= 1 (count result)))
(t/is (= (:id team1) (:id item1)))
(t/is (= 1 (:total-members item1)))
(t/is (= 1 (:total-editors item1)))
(t/is (not= (:default-team-id profile1) (:id item1))))))
(t/deftest get-teams-fetches-organizations-in-one-batch
(let [profile (th/create-profile* 1 {:is-active true})
organization-team (th/create-team* 1 {:profile-id (:id profile)})
plain-team (th/create-team* 2 {:profile-id (:id profile)})
expired-team (th/create-team* 3 {:profile-id (:id profile)})
organization-id (uuid/random)
calls (atom [])
organization {:id organization-id
:name "Acme"
:slug "acme"
:owner-id (:id profile)
:avatar-bg-url "https://example.com/avatar.svg"}
nitrate-call (fn [_cfg method params]
(swap! calls conj [method params])
[{:id (:id organization-team)
:is-your-penpot false
:organization organization}
{:id (:id expired-team)
:is-your-penpot false
:organization (assoc organization :expired-license true)}])
params {::th/type :get-teams
::rpc/profile-id (:id profile)}]
(with-redefs [cf/flags (conj cf/flags :admin-console)
nitrate/call nitrate-call]
(let [out (th/command! params)
teams (:result out)]
(t/is (th/success? out))
(t/is (= 1 (count @calls)))
(t/is (= :get-teams-organizations (ffirst @calls)))
(t/is (= #{(:default-team-id profile)
(:id organization-team)
(:id plain-team)
(:id expired-team)}
(-> @calls first second :team-ids set)))
(t/is (= #{(:default-team-id profile)
(:id organization-team)
(:id plain-team)}
(into #{} (map :id) teams)))
(t/is (= organization
(->> teams
(filter #(= (:id organization-team) (:id %)))
first
:organization)))))))
(t/deftest get-teams-propagates-profile-id-and-restrictions-met
(let [profile (th/create-profile* 1 {:is-active true})
organization-team (th/create-team* 1 {:profile-id (:id profile)})
organization-id (uuid/random)
calls (atom [])
restrictions-met {:create-teams true :move-teams false :send-invitations true}
organization {:id organization-id
:name "Acme"
:slug "acme"
:owner-id (:id profile)
:avatar-bg-url "https://example.com/avatar.svg"
:restrictions-met restrictions-met}
nitrate-call (fn [_cfg method params]
(swap! calls conj [method params])
[{:id (:id organization-team)
:is-your-penpot false
:organization organization}])
params {::th/type :get-teams
::rpc/profile-id (:id profile)}]
(with-redefs [cf/flags (conj cf/flags :admin-console)
nitrate/call nitrate-call]
(let [out (th/command! params)
teams (:result out)
team (first (filter #(= (:id organization-team) (:id %)) teams))]
(t/is (th/success? out))
;; The batched lookup carries the caller's profile-id so Nitrate can
;; answer :restrictions-met for this profile.
(t/is (= :get-teams-organizations (ffirst @calls)))
(t/is (= (:id profile) (-> @calls first second ::rpc/profile-id)))
;; :restrictions-met flows through apply-organization into the nested
;; :organization map (organization->team-keys).
(t/is (= restrictions-met
(get-in team [:organization :restrictions-met])))))))
(t/deftest get-teams-rejects-invalid-organization-batch-response
(let [profile (th/create-profile* 1 {:is-active true})
calls (atom [])
params {::th/type :get-teams
::rpc/profile-id (:id profile)}]
(with-redefs [cf/flags (conj cf/flags :admin-console)
nitrate/call (fn [_cfg method call-params]
(swap! calls conj [method call-params])
nil)]
(let [out (th/command! params)]
(t/is (not (th/success? out)))
(t/is (= :nitrate-unavailable (th/ex-type (:error out))))
(t/is (= 1 (count @calls)))
(t/is (= :get-teams-organizations (ffirst @calls)))))))
(defn- find-team
[teams team-id]
(d/seek #(= team-id (:id %)) teams))
(t/deftest get-teams-shows-owner-nitrate-licence-to-every-member
(let [member (th/create-profile* 1 {:is-active true})
owner (th/create-profile* 2 {:is-active true})
org-owner (th/create-profile* 3 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})
organization-team (th/create-team* 2 {:profile-id (:id org-owner)})
_ (th/create-team-role* {:team-id (:id team)
:profile-id (:id member)
:role :editor})
_ (th/create-team-role* {:team-id (:id organization-team)
:profile-id (:id member)
:role :editor})
calls (atom [])
nitrate-call (fn [_cfg method params]
(swap! calls conj [method params])
(case method
:get-teams-organizations
[{:id (:id organization-team)
:is-your-penpot false
:organization {:id (uuid/random)
:name "Acme"
:slug "acme"
:owner-id (:id org-owner)}}]
:get-subscriptions
[{:profile-id (:id owner) :type "nitrate" :status "trialing"}
{:profile-id (:id org-owner) :type "nitrate" :status "active"}]))
params {::th/type :get-teams
::rpc/profile-id (:id member)}]
(with-redefs [cf/flags (conj cf/flags :admin-console :subscriptions)
nitrate/call nitrate-call]
(let [out (th/command! params)
teams (:result out)]
(t/is (th/success? out))
(t/is (= [:get-teams-organizations :get-subscriptions] (mapv first @calls)))
;; One request with the owners of the teams outside an organization,
;; the member included as owner of its default team.
(t/is (= #{(:id owner) (:id member)}
(-> @calls second second :profile-ids set)))
(t/is (= {:type "nitrate" :status "trialing"}
(-> (find-team teams (:id team))
:subscription
(select-keys [:type :status]))))
;; A team in an organization keeps the Stripe plan of its owner.
(t/is (= "professional"
(-> (find-team teams (:id organization-team)) :subscription :type)))
(t/is (= "professional"
(-> (find-team teams (:default-team-id member)) :subscription :type)))))))
(t/deftest get-teams-ignores-owner-nitrate-licence-that-is-not-valid
(let [member (th/create-profile* 1 {:is-active true})
owner (th/create-profile* 2 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})
_ (th/create-team-role* {:team-id (:id team)
:profile-id (:id member)
:role :editor})
nitrate-call (fn [_cfg method _params]
(case method
:get-teams-organizations []
:get-subscriptions [{:profile-id (:id owner)
:type "nitrate"
:status "canceled"}]))
params {::th/type :get-teams
::rpc/profile-id (:id member)}]
(with-redefs [cf/flags (conj cf/flags :admin-console :subscriptions)
nitrate/call nitrate-call]
(let [out (th/command! params)]
(t/is (th/success? out))
(t/is (= "professional"
(-> (find-team (:result out) (:id team)) :subscription :type)))))))
(t/deftest get-teams-keeps-stripe-plan-when-owner-licences-fail
(let [member (th/create-profile* 1 {:is-active true})
owner (th/create-profile* 2 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})
_ (th/create-team-role* {:team-id (:id team)
:profile-id (:id member)
:role :editor})
nitrate-call (fn [_cfg method _params]
(case method
:get-teams-organizations []
:get-subscriptions (throw (ex-info "nitrate is down"
{:type :nitrate-unavailable}))))
params {::th/type :get-teams
::rpc/profile-id (:id member)}]
(with-redefs [cf/flags (conj cf/flags :admin-console :subscriptions)
nitrate/call nitrate-call]
(let [out (th/command! params)]
(t/is (th/success? out))
(t/is (= "professional"
(-> (find-team (:result out) (:id team)) :subscription :type)))))))
(t/deftest get-team-shows-owner-nitrate-licence-to-a-member
(let [member (th/create-profile* 1 {:is-active true})
owner (th/create-profile* 2 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})
_ (th/create-team-role* {:team-id (:id team)
:profile-id (:id member)
:role :editor})
calls (atom [])
nitrate-call (fn [_cfg method params]
(swap! calls conj [method params])
(case method
:get-team-organization nil
:get-subscriptions [{:profile-id (:id owner)
:type "nitrate"
:status "active"}]))
params {::th/type :get-team
::rpc/profile-id (:id member)
:id (:id team)}]
(with-redefs [cf/flags (conj cf/flags :admin-console :subscriptions)
nitrate/call nitrate-call]
(let [out (th/command! params)]
(t/is (th/success? out))
(t/is (= [(:id owner)] (-> (last @calls) second :profile-ids)))
(t/is (= "nitrate" (-> out :result :subscription :type)))))))
(t/deftest team-deletion-1
(let [profile1 (th/create-profile* 1 {:is-active true})
team (th/create-team* 1 {:profile-id (:id profile1)})
pool (:app.db/pool th/*system*)
data {::th/type :delete-team
::rpc/profile-id (:id profile1)
:team-id (:id team)}]
;; team is not deleted because it does not meet all
;; conditions to be deleted.
(let [result (th/run-task! :objects-gc {})]
(t/is (= 0 (:processed result))))
;; query the list of teams
(let [data {::th/type :get-teams
::rpc/profile-id (:id profile1)}
out (th/command! data)]
;; (th/print-result! out)
(t/is (th/success? out))
(let [result (:result out)]
(t/is (= 2 (count result)))
(t/is (= (:id team) (get-in result [1 :id])))
(t/is (= (:default-team-id profile1) (get-in result [0 :id])))))
;; Request team to be deleted
(let [params {::th/type :delete-team
::rpc/profile-id (:id profile1)
:id (:id team)}
out (th/command! params)]
(t/is (th/success? out)))
;; query the list of teams after soft deletion
(let [data {::th/type :get-teams
::rpc/profile-id (:id profile1)}
out (th/command! data)]
;; (th/print-result! out)
(t/is (th/success? out))
(let [result (:result out)]
(t/is (= 1 (count result)))
(t/is (= (:default-team-id profile1) (get-in result [0 :id])))))
(th/run-pending-jobs)
;; run permanent deletion (should be noop)
(let [result (th/run-task! :objects-gc {})]
(t/is (= 0 (:processed result))))
;; query the list of projects after hard deletion
(let [data {::th/type :get-projects
::rpc/profile-id (:id profile1)
:team-id (:id team)}
out (th/command! data)]
;; (th/print-result! out)
(t/is (not (th/success? out)))
(let [edata (-> out :error ex-data)]
(t/is (= :not-found (:type edata)))))
;; run permanent deletion
(binding [ct/*clock* (ct/fixed-clock (ct/in-future {:days 8}))]
(let [result (th/run-task! :objects-gc {})]
(t/is (= 2 (:processed result)))))
;; query the list of projects of a after hard deletion
(let [data {::th/type :get-projects
::rpc/profile-id (:id profile1)
:team-id (:id team)}
out (th/command! data)]
;; (th/print-result! out)
(t/is (not (th/success? out)))
(let [edata (-> out :error ex-data)]
(t/is (= :not-found (:type edata)))))))
(t/deftest team-deletion-2
(let [storage (-> (:app.storage/storage th/*system*)
(assoc ::sto/backend :assets-fs))
prof (th/create-profile* 1)
team (th/create-team* 1 {:profile-id (:id prof)})
proj (th/create-project* 1 {:profile-id (:id prof)
:team-id (:id team)})
file (th/create-file* 1 {:profile-id (:id prof)
:project-id (:default-project-id team)
:is-shared false})
mfile {:filename "sample.jpg"
:path (th/tempfile "backend_tests/test_files/sample.jpg")
:mtype "image/jpeg"
:size 312043}]
(let [params {::th/type :upload-file-media-object
::rpc/profile-id (:id prof)
:file-id (:id file)
:is-local true
:name "testfile"
:content mfile}
out (th/multipart-command! params)]
(t/is (nil? (:error out))))
(let [params {::th/type :delete-team
::rpc/profile-id (:id prof)
:id (:id team)}
out (th/command! params)]
#_(th/print-result! out)
(t/is (nil? (:error out))))
(th/run-pending-jobs)
(let [rows (th/db-exec! ["select * from team where id = ?" (:id team)])]
(t/is (= 1 (count rows)))
(t/is (ct/inst? (:deleted-at (first rows)))))
(binding [ct/*clock* (ct/fixed-clock (ct/in-future {:days 8}))]
(let [result (th/run-task! :objects-gc {})]
(t/is (= 7 (:processed result)))))))
(t/deftest delete-team-audit-includes-organization-id
;; Assert result metadata directly: wrap-audit is inactive in the
;; default test flags, so mocking audit/submit would never see this.
(let [owner (th/create-profile* 1 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})
organization-id (uuid/next)
organization {:id organization-id
:name "Acme"
:slug "acme"
:owner-id (:id owner)
:avatar-bg-url "https://example.com/avatar.svg"
:permissions {:delete-teams "onlyOwners"}}]
(with-redefs [cf/flags (conj cf/flags :admin-console)
nitrate/call
(fn [_cfg method params]
(case method
:get-team-organization
(when (= (:id team) (:team-id params))
{:id (:id team)
:is-your-penpot false
:organization organization})
:delete-team nil
nil))]
(let [result (db/tx-run! th/*system*
(fn [cfg]
(teams/delete-team cfg {:team-id (:id team)
:profile-id (:id owner)})))
props (::audit/props (meta result))]
(t/is (= organization-id (:organization-id props)))
(t/is (= (:id team) (:team-id props)))
(t/is (= (:name team) (:team-name props)))))))
(t/deftest create-team-access-request
(with-mocks [mock {:target 'app.email/send-template :return nil}]
(let [owner (th/create-profile* 1 {:is-active true :email "owner@bar.com"})
requester (th/create-profile* 3 {:is-active true :email "requester@bar.com"})
team (th/create-team* 1 {:profile-id (:id owner)})
proj (th/create-project* 1 {:profile-id (:id owner)
:team-id (:id team)})
file (th/create-file* 1 {:profile-id (:id owner)
:project-id (:id proj)})
;; Cold storage makes the file map carry the internal pointer, so the
;; response assertion below is meaningful.
_ (th/run-task! :offload-file-data {:file-id (:id file)})
data {::th/type :create-team-access-request
::rpc/profile-id (:id requester)
:file-id (:id file)}]
;; request success
(let [out (th/command! data)
;; retrieve the value from the database and check its content
requests (th/db-query :team-access-request
{:team-id (:id team)
:requester-id (:id requester)})]
(t/is (th/success? out))
(t/is (= 1 (:call-count @mock)))
(t/is (= 1 (count requests)))
(t/is (not (contains? (:result out) :storage-object-id))))
;; request again fails
(th/reset-mock! mock)
(let [out (th/command! data)
edata (-> out :error ex-data)]
(t/is (not (th/success? out)))
(t/is (= 0 (:call-count @mock)))
(t/is (= :validation (:type edata)))
(t/is (= :request-already-sent (:code edata))))
;; request again when is expired success
(th/reset-mock! mock)
(th/db-update! :team-access-request
{:valid-until (ct/in-past "1h")}
{:team-id (:id team)
:requester-id (:id requester)})
(t/is (th/success? (th/command! data)))
(t/is (= 1 (:call-count @mock))))))
(t/deftest create-team-access-request-owner-muted
(with-mocks [mock {:target 'app.email/send-template :return nil}]
(let [owner (th/create-profile* 1 {:is-active true :is-muted true :email "owner@bar.com"})
requester (th/create-profile* 2 {:is-active true :email "requester@bar.com"})
team (th/create-team* 1 {:profile-id (:id owner)})
proj (th/create-project* 1 {:profile-id (:id owner)
:team-id (:id team)})
file (th/create-file* 1 {:profile-id (:id owner)
:project-id (:id proj)})
data {::th/type :create-team-access-request
::rpc/profile-id (:id requester)
:file-id (:id file)}]
;; request to team with owner muted should success
(t/is (th/success? (th/command! data)))
(t/is (= 1 (:call-count @mock))))))
(t/deftest create-team-access-request-requester-muted
(with-mocks [mock {:target 'app.email/send-template :return nil}]
(let [owner (th/create-profile* 1 {:is-active true :email "owner@bar.com"})
requester (th/create-profile* 2 {:is-active true :is-muted true :email "requester@bar.com"})
team (th/create-team* 1 {:profile-id (:id owner)})
proj (th/create-project* 1 {:profile-id (:id owner)
:team-id (:id team)})
file (th/create-file* 1 {:profile-id (:id owner)
:project-id (:id proj)})
data {::th/type :create-team-access-request
::rpc/profile-id (:id requester)
:file-id (:id file)}
out (th/command! data)
edata (-> out :error ex-data)]
;; request with requester muted should fail
(t/is (not (th/success? out)))
(t/is (= 0 (:call-count @mock)))
(t/is (= :validation (:type edata)))
(t/is (= :member-is-muted (:code edata)))
(t/is (= (:email requester) (:email edata))))))
(t/deftest create-team-access-request-owner-bounce
(with-mocks [mock {:target 'app.email/send-template :return nil}]
(let [owner (th/create-profile* 1 {:is-active true :email "owner@bar.com"})
requester (th/create-profile* 2 {:is-active true :email "requester@bar.com"})
team (th/create-team* 1 {:profile-id (:id owner)})
proj (th/create-project* 1 {:profile-id (:id owner)
:team-id (:id team)})
file (th/create-file* 1 {:profile-id (:id owner)
:project-id (:id proj)})
pool (:app.db/pool th/*system*)
data {::th/type :create-team-access-request
::rpc/profile-id (:id requester)
:file-id (:id file)}]
(th/create-global-complaint-for pool {:type :bounce :email "owner@bar.com"})
(let [out (th/command! data)
edata (-> out :error ex-data)]
;; request with owner bounce should fail
(t/is (not (th/success? out)))
(t/is (= 0 (:call-count @mock)))
(t/is (= :restriction (:type edata)))
(t/is (= :email-has-permanent-bounces (:code edata)))
(t/is (= "private" (:email edata)))))))
(t/deftest create-team-access-request-requester-bounce
(with-mocks [mock {:target 'app.email/send-template :return nil}]
(let [owner (th/create-profile* 1 {:is-active true :email "owner@bar.com"})
requester (th/create-profile* 2 {:is-active true :email "requester@bar.com"})
team (th/create-team* 1 {:profile-id (:id owner)})
proj (th/create-project* 1 {:profile-id (:id owner)
:team-id (:id team)})
file (th/create-file* 1 {:profile-id (:id owner)
:project-id (:id proj)})
pool (:app.db/pool th/*system*)
data {::th/type :create-team-access-request
::rpc/profile-id (:id requester)
:file-id (:id file)}]
;; request with requester bounce should success
(th/create-global-complaint-for pool {:type :bounce :email "requester@bar.com"})
(t/is (th/success? (th/command! data)))
(t/is (= 1 (:call-count @mock))))))
(t/deftest create-team-with-invalid-name
(let [profile (th/create-profile* 1 {:is-active true})]
;; name with a dot should fail
(let [data {::th/type :create-team
::rpc/profile-id (:id profile)
:name "foo.bar"}
out (th/command! data)]
(t/is (not (th/success? out)))
(t/is (th/ex-of-type? (:error out) :validation))
(t/is (th/ex-of-code? (:error out) :params-validation)))
;; name with a colon should fail
(let [data {::th/type :create-team
::rpc/profile-id (:id profile)
:name "foo:bar"}
out (th/command! data)]
(t/is (not (th/success? out)))
(t/is (th/ex-of-type? (:error out) :validation))
(t/is (th/ex-of-code? (:error out) :params-validation)))
;; name with a slash should fail
(let [data {::th/type :create-team
::rpc/profile-id (:id profile)
:name "foo/bar"}
out (th/command! data)]
(t/is (not (th/success? out)))
(t/is (th/ex-of-type? (:error out) :validation))
(t/is (th/ex-of-code? (:error out) :params-validation)))
;; valid name should succeed
(let [data {::th/type :create-team
::rpc/profile-id (:id profile)
:name "My Valid Team"}
out (th/command! data)]
(t/is (th/success? out)))))
(t/deftest create-team-invitations-email-cooldown
(with-mocks [mock {:target 'app.email/send-template :return nil}]
(let [profile1 (th/create-profile* 1 {:is-active true})
team (th/create-team* 1 {:profile-id (:id profile1)})
data {::th/type :create-team-invitations
::rpc/profile-id (:id profile1)
:team-id (:id team)
:role :editor
:emails ["cooldown-test@example.com"]}]
;; First invitation sends email
(let [out (th/command! data)]
(t/is (th/success? out))
(t/is (= 1 (:call-count @mock))))
;; Resending immediately should NOT send email (cooldown active)
(th/reset-mock! mock)
(let [out (th/command! data)]
(t/is (th/success? out))
(t/is (= 0 (:call-count @mock))))
;; Resending to a different email should send email
(th/reset-mock! mock)
(let [data (assoc data :emails ["different@example.com"])
out (th/command! data)]
(t/is (th/success? out))
(t/is (= 1 (:call-count @mock))))
;; After cooldown expires, resending should send email
(th/reset-mock! mock)
(th/db-update! :team-invitation
{:updated-at (ct/in-past "10m")}
{:team-id (:id team)
:email-to "cooldown-test@example.com"})
(let [data (assoc data :emails ["cooldown-test@example.com"])
out (th/command! data)]
(t/is (th/success? out))
(t/is (= 1 (:call-count @mock)))))))
(t/deftest update-team-with-invalid-name
(let [profile (th/create-profile* 1 {:is-active true})
team (th/create-team* 1 {:profile-id (:id profile)})]
;; name with a dot should fail
(let [data {::th/type :update-team
::rpc/profile-id (:id profile)
:id (:id team)
:name "foo.bar"}
out (th/command! data)]
(t/is (not (th/success? out)))
(t/is (th/ex-of-type? (:error out) :validation))
(t/is (th/ex-of-code? (:error out) :params-validation)))
;; name with a colon should fail
(let [data {::th/type :update-team
::rpc/profile-id (:id profile)
:id (:id team)
:name "foo:bar"}
out (th/command! data)]
(t/is (not (th/success? out)))
(t/is (th/ex-of-type? (:error out) :validation))
(t/is (th/ex-of-code? (:error out) :params-validation)))
;; name with a slash should fail
(let [data {::th/type :update-team
::rpc/profile-id (:id profile)
:id (:id team)
:name "foo/bar"}
out (th/command! data)]
(t/is (not (th/success? out)))
(t/is (th/ex-of-type? (:error out) :validation))
(t/is (th/ex-of-code? (:error out) :params-validation)))
;; valid name should succeed
(let [data {::th/type :update-team
::rpc/profile-id (:id profile)
:id (:id team)
:name "My Valid Team"}
out (th/command! data)]
(t/is (th/success? out)))))
(t/deftest create-team-in-organization-regression
(with-mocks [audit-mock {:target 'app.loggers.audit/submit :return nil}]
(let [owner (th/create-profile* 401 {:is-active true})
non-member (th/create-profile* 402 {:is-active true})
organization-id (uuid/random)
params {::th/type :create-team
::rpc/profile-id (:id owner)
:name "Test Team"
:organization-id organization-id}
nitrate-call-fn
(fn [_cfg method p]
(case method
:get-organization-membership
(if (= (:profile-id p) (:id non-member))
{:organization-id organization-id :is-member false}
{:organization-id organization-id :is-member true})
:get-organization-permissions
{:owner-id (:id owner)
:permissions {:create-teams "any"}}
:set-team-organization
(let [team-id (:team-id p)]
{:id team-id
:name "Test Team"
:organization-id organization-id
:default-project-id (uuid/random)})
nil))]
;; Non-member should be denied with :user-doesnt-belong-organization
(with-redefs [cf/flags (conj cf/flags :admin-console)
nitrate/call nitrate-call-fn]
(let [out (th/command! (assoc params ::rpc/profile-id (:id non-member)))]
(t/is (not (th/success? out)))
(let [edata (-> out :error ex-data)]
(t/is (= :validation (:type edata)))
(t/is (= :user-doesnt-belong-organization (:code edata))))))
;; Authorized member should succeed
(th/reset-mock! audit-mock)
(with-redefs [cf/flags (conj cf/flags :admin-console)
nitrate/call nitrate-call-fn]
(let [out (th/command! params)]
(t/is (th/success? out))
(let [team (:result out)]
(t/is (uuid? (:id team)))
(t/is (= "Test Team" (:name team)))))))))
(t/deftest create-team-in-organization-follows-attribute-restriction
(with-mocks [audit-mock {:target 'app.loggers.audit/submit :return nil}]
(let [org-owner (th/create-profile* 410 {:is-active true})
member (th/create-profile* 411 {:is-active true})
organization-id (uuid/random)
perms-params (atom nil)
make-nitrate-call
(fn [restrictions-met]
(fn [_cfg method p]
(case method
:get-organization-membership
{:organization-id organization-id :is-member true}
:get-organization-permissions
(do
(reset! perms-params p)
{:owner-id (:id org-owner)
:permissions {:create-teams "any"}
:restrictions-met restrictions-met})
:set-team-organization
(let [team-id (:team-id p)]
{:id team-id
:name "Test Team"
:organization-id organization-id
:default-project-id (uuid/random)})
nil)))
create!
(fn [profile-id]
(th/command! {::th/type :create-team
::rpc/profile-id profile-id
:name "Test Team"
:organization-id organization-id}))]
;; A non-owner member is denied when the create-teams restriction is not met
(with-redefs [cf/flags (conj cf/flags :admin-console)
nitrate/call (make-nitrate-call {:create-teams false})]
(let [out (create! (:id member))]
(t/is (not (th/success? out)))
(t/is (= :not-allowed (th/ex-code (:error out))))
;; the caller's profile-id travels to Nitrate so it can evaluate the restriction
(t/is (= (:id member) (::rpc/profile-id @perms-params)))))
;; The same member is allowed when the restriction is met
(th/reset-mock! audit-mock)
(with-redefs [cf/flags (conj cf/flags :admin-console)
nitrate/call (make-nitrate-call {:create-teams true})]
(t/is (th/success? (create! (:id member)))))
;; The organization owner keeps the create-teams bypass even when not met
(th/reset-mock! audit-mock)
(with-redefs [cf/flags (conj cf/flags :admin-console)
nitrate/call (make-nitrate-call {:create-teams false})]
(t/is (th/success? (create! (:id org-owner))))))))
;; --- T7-F-01: Role ceiling in team invitations ---
(t/deftest admin-cannot-create-invitation-with-owner-role
(with-mocks [mock {:target 'app.email/send-template :return nil}]
(let [owner (th/create-profile* 1 {:is-active true})
admin (th/create-profile* 2 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})]
;; Add admin as team member with :admin role
(th/create-team-role* {:team-id (:id team)
:profile-id (:id admin)
:role :admin})
;; Admin tries to create invitation with :owner role (emails+role format)
;; This should FAIL with :cant-promote-to-owner
(let [data {::th/type :create-team-invitations
::rpc/profile-id (:id admin)
:team-id (:id team)
:role :owner
:emails ["invitee@example.com"]}
out (th/command! data)]
(t/is (not (th/success? out)))
(t/is (th/ex-of-type? (:error out) :validation))
(t/is (th/ex-of-code? (:error out) :cant-promote-to-owner))
(t/is (= 0 (:call-count @mock)))))))
(t/deftest admin-cannot-create-invitation-with-owner-role-invitations-format
(with-mocks [mock {:target 'app.email/send-template :return nil}]
(let [owner (th/create-profile* 1 {:is-active true})
admin (th/create-profile* 2 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})]
;; Add admin as team member with :admin role
(th/create-team-role* {:team-id (:id team)
:profile-id (:id admin)
:role :admin})
;; Admin tries to create invitation with :owner role (invitations format)
;; This should FAIL with :cant-promote-to-owner
(let [data {::th/type :create-team-invitations
::rpc/profile-id (:id admin)
:team-id (:id team)
:invitations [{:email "invitee@example.com" :role :owner}]}
out (th/command! data)]
(t/is (not (th/success? out)))
(t/is (th/ex-of-type? (:error out) :validation))
(t/is (th/ex-of-code? (:error out) :cant-promote-to-owner))
(t/is (= 0 (:call-count @mock)))))))
(t/deftest admin-cannot-update-invitation-role-to-owner
(with-mocks [mock {:target 'app.email/send-template :return nil}]
(let [owner (th/create-profile* 1 {:is-active true})
admin (th/create-profile* 2 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})]
;; Add admin as team member with :admin role
(th/create-team-role* {:team-id (:id team)
:profile-id (:id admin)
:role :admin})
;; Owner creates an invitation with :editor role
(let [data {::th/type :create-team-invitations
::rpc/profile-id (:id owner)
:team-id (:id team)
:role :editor
:emails ["invitee@example.com"]}
out (th/command! data)]
(t/is (th/success? out)))
(th/reset-mock! mock)
;; Admin tries to update invitation role to :owner
;; This should FAIL with :cant-promote-to-owner
(let [data {::th/type :update-team-invitation-role
::rpc/profile-id (:id admin)
:team-id (:id team)
:email "invitee@example.com"
:role :owner}
out (th/command! data)]
(t/is (not (th/success? out)))
(t/is (th/ex-of-type? (:error out) :validation))
(t/is (th/ex-of-code? (:error out) :cant-promote-to-owner))))))
(t/deftest owner-can-create-invitation-with-owner-role
(with-mocks [mock {:target 'app.email/send-template :return nil}]
(let [owner (th/create-profile* 1 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})]
;; Owner creates invitation with :owner role
;; This should SUCCEED (owner has full privileges)
(let [data {::th/type :create-team-invitations
::rpc/profile-id (:id owner)
:team-id (:id team)
:role :owner
:emails ["invitee@example.com"]}
out (th/command! data)]
(t/is (th/success? out))
(t/is (= 1 (:call-count @mock)))))))
(t/deftest admin-cannot-remove-team-owner
(let [owner (th/create-profile* 1 {:is-active true})
admin (th/create-profile* 2 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})]
(th/create-team-role* {:team-id (:id team)
:profile-id (:id admin)
:role :admin})
(let [out (th/command! {::th/type :delete-team-member
::rpc/profile-id (:id admin)
:team-id (:id team)
:member-id (:id owner)})]
(t/is (not (th/success? out)))
(t/is (th/ex-of-type? (:error out) :validation))
(t/is (th/ex-of-code? (:error out) :cant-remove-owner)))))
(t/deftest owner-can-remove-another-owner
(let [owner1 (th/create-profile* 1 {:is-active true})
owner2 (th/create-profile* 2 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner1)})]
(th/create-team-role* {:team-id (:id team)
:profile-id (:id owner2)
:role :owner})
(let [out (th/command! {::th/type :delete-team-member
::rpc/profile-id (:id owner1)
:team-id (:id team)
:member-id (:id owner2)})]
(t/is (th/success? out)))))
(t/deftest owner-can-remove-admin
(let [owner (th/create-profile* 1 {:is-active true})
admin (th/create-profile* 2 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})]
(th/create-team-role* {:team-id (:id team)
:profile-id (:id admin)
:role :admin})
(let [out (th/command! {::th/type :delete-team-member
::rpc/profile-id (:id owner)
:team-id (:id team)
:member-id (:id admin)})]
(t/is (th/success? out)))))
(t/deftest admin-can-remove-admin
(let [owner (th/create-profile* 1 {:is-active true})
admin1 (th/create-profile* 2 {:is-active true})
admin2 (th/create-profile* 3 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})]
(th/create-team-role* {:team-id (:id team)
:profile-id (:id admin1)
:role :admin})
(th/create-team-role* {:team-id (:id team)
:profile-id (:id admin2)
:role :admin})
(let [out (th/command! {::th/type :delete-team-member
::rpc/profile-id (:id admin1)
:team-id (:id team)
:member-id (:id admin2)})]
(t/is (th/success? out)))))
(t/deftest delete-nonexistent-member-returns-not-found
(let [owner (th/create-profile* 1 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})
fake-id (uuid/next)]
(let [out (th/command! {::th/type :delete-team-member
::rpc/profile-id (:id owner)
:team-id (:id team)
:member-id fake-id})]
(t/is (not (th/success? out)))
(t/is (th/ex-of-type? (:error out) :not-found))
(t/is (th/ex-of-code? (:error out) :member-does-not-exist)))))
(t/deftest create-team-rejects-client-id
(let [profile (th/create-profile* 1 {:is-active true})
sent-id (uuid/next)
out (th/command! {::th/type :create-team
::rpc/profile-id (:id profile)
:name "team with client id"
:id sent-id})]
(t/is (th/ex-info? (:error out)))
(t/is (th/ex-of-type? (:error out) :validation))
(t/is (th/ex-of-code? (:error out) :params-validation))))
(t/deftest create-team-with-invitations-rejects-client-id
(let [profile (th/create-profile* 1 {:is-active true})
sent-id (uuid/next)
out (th/command! {::th/type :create-team-with-invitations
::rpc/profile-id (:id profile)
:name "team with client id"
:emails ["invitee@example.com"]
:role :editor
:id sent-id})]
(t/is (th/ex-info? (:error out)))
(t/is (th/ex-of-type? (:error out) :validation))
(t/is (th/ex-of-code? (:error out) :params-validation))))
(t/deftest update-team-photo-rejects-forged-file
;; Same transit-forgery vector as profile photos: without a multipart
;; request the wrapper must reject it at params validation.
(let [prof (th/create-profile* 1)
team (th/create-team* 1 {:profile-id (:id prof)})
out (th/command! {::th/type :update-team-photo
::rpc/profile-id (:id prof)
:team-id (:id team)
:file {:filename "evil.jpg"
:size 123
:path (fs/path "/etc/passwd")
:mtype "image/jpeg"}})]
(t/is (th/ex-of-code? (:error out) :params-validation))))
(t/deftest leaver-loses-file-access
;; GHSA-v9r9-h77c-55m2: leaving the team must revoke the file and
;; project roles granted in that team.
(let [owner (th/create-profile* 1 {:is-active true})
editor (th/create-profile* 2 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})]
(th/create-team-role* {:team-id (:id team)
:profile-id (:id editor)
:role :editor})
(let [project (th/create-project* 1 {:profile-id (:id editor)
:team-id (:id team)})
file (th/create-file* 1 {:profile-id (:id editor)
:project-id (:id project)})]
(t/testing "editor can rename and read summary before leaving"
(let [out (th/command! {::th/type :rename-file
::rpc/profile-id (:id editor)
:id (:id file)
:name "renamed"})]
(t/is (th/success? out)))
(let [out (th/command! {::th/type :get-file-summary
::rpc/profile-id (:id editor)
:id (:id file)})]
(t/is (th/success? out))))
(t/testing "leave team"
(let [out (th/command! {::th/type :leave-team
::rpc/profile-id (:id editor)
:id (:id team)})]
(t/is (th/success? out))))
(t/testing "renaming after leaving fails with not-found"
(let [out (th/command! {::th/type :rename-file
::rpc/profile-id (:id editor)
:id (:id file)
:name "renamed-again"})
error (:error out)]
(t/is (th/ex-info? error))
(t/is (th/ex-of-type? error :not-found))))
(t/testing "file summary after leaving fails with not-found"
(let [out (th/command! {::th/type :get-file-summary
::rpc/profile-id (:id editor)
:id (:id file)})
error (:error out)]
(t/is (th/ex-info? error))
(t/is (th/ex-of-type? error :not-found))))
(t/testing "get-file after leaving fails with not-found"
(let [out (th/command! {::th/type :get-file
::rpc/profile-id (:id editor)
:id (:id file)
:components-v2 true})
error (:error out)]
(t/is (th/ex-info? error))
(t/is (th/ex-of-type? error :not-found)))))))
(t/deftest removed-member-loses-file-access
;; Same as above through the expulsion path.
(let [owner (th/create-profile* 1 {:is-active true})
editor (th/create-profile* 2 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})]
(th/create-team-role* {:team-id (:id team)
:profile-id (:id editor)
:role :editor})
(let [project (th/create-project* 1 {:profile-id (:id editor)
:team-id (:id team)})
file (th/create-file* 1 {:profile-id (:id editor)
:project-id (:id project)})]
(t/testing "remove member"
(let [out (th/command! {::th/type :delete-team-member
::rpc/profile-id (:id owner)
:team-id (:id team)
:member-id (:id editor)})]
(t/is (th/success? out))))
(t/testing "renaming after removal fails with not-found"
(let [out (th/command! {::th/type :rename-file
::rpc/profile-id (:id editor)
:id (:id file)
:name "renamed-again"})
error (:error out)]
(t/is (th/ex-info? error))
(t/is (th/ex-of-type? error :not-found))))
(t/testing "file summary after removal fails with not-found"
(let [out (th/command! {::th/type :get-file-summary
::rpc/profile-id (:id editor)
:id (:id file)})
error (:error out)]
(t/is (th/ex-info? error))
(t/is (th/ex-of-type? error :not-found)))))))
(t/deftest orphan-file-role-without-team-membership-grants-nothing
;; Defense in depth: a file role row without a live team membership
;; (leftover from before the leave-team cleanup) grants no access.
(let [owner (th/create-profile* 1 {:is-active true})
stranger (th/create-profile* 2 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})
project (th/create-project* 1 {:profile-id (:id owner)
:team-id (:id team)})
file (th/create-file* 1 {:profile-id (:id owner)
:project-id (:id project)})]
;; Plant an orphan file role for a profile that was never a member.
(th/create-file-role* {:file-id (:id file)
:profile-id (:id stranger)
:role :editor})
(t/testing "orphan file role cannot rename"
(let [out (th/command! {::th/type :rename-file
::rpc/profile-id (:id stranger)
:id (:id file)
:name "renamed"})
error (:error out)]
(t/is (th/ex-info? error))
(t/is (th/ex-of-type? error :not-found))))
(t/testing "orphan file role cannot read summary"
(let [out (th/command! {::th/type :get-file-summary
::rpc/profile-id (:id stranger)
:id (:id file)})
error (:error out)]
(t/is (th/ex-info? error))
(t/is (th/ex-of-type? error :not-found))))))
(t/deftest leaver-loses-project-access
;; Same revocation at project level (covers sql:project-permissions).
(let [owner (th/create-profile* 1 {:is-active true})
editor (th/create-profile* 2 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})]
(th/create-team-role* {:team-id (:id team)
:profile-id (:id editor)
:role :editor})
(let [project (th/create-project* 1 {:profile-id (:id editor)
:team-id (:id team)})]
(t/testing "leave team"
(let [out (th/command! {::th/type :leave-team
::rpc/profile-id (:id editor)
:id (:id team)})]
(t/is (th/success? out))))
(t/testing "get-project after leaving fails with not-found"
(let [out (th/command! {::th/type :get-project
::rpc/profile-id (:id editor)
:id (:id project)})
error (:error out)]
(t/is (th/ex-info? error))
(t/is (th/ex-of-type? error :not-found))))
(t/testing "rename-project after leaving fails with not-found"
(let [out (th/command! {::th/type :rename-project
::rpc/profile-id (:id editor)
:id (:id project)
:name "renamed"})
error (:error out)]
(t/is (th/ex-info? error))
(t/is (th/ex-of-type? error :not-found)))))))
(t/deftest leaver-vanishes-from-search
;; The old team's files are no longer searchable by the leaver.
(let [owner (th/create-profile* 1 {:is-active true})
editor (th/create-profile* 2 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})]
(th/create-team-role* {:team-id (:id team)
:profile-id (:id editor)
:role :editor})
(let [project (th/create-project* 1 {:profile-id (:id editor)
:team-id (:id team)})
file (th/create-file* 1 {:profile-id (:id editor)
:project-id (:id project)
:name "searchable-file"})]
(t/testing "member finds the file before leaving"
(let [out (th/command! {::th/type :search-files
::rpc/profile-id (:id editor)
:team-id (:id team)
:search-term "searchable"})]
(t/is (th/success? out))
(t/is (= (:id file) (-> out :result first :id)))))
(t/testing "leave team"
(let [out (th/command! {::th/type :leave-team
::rpc/profile-id (:id editor)
:id (:id team)})]
(t/is (th/success? out))))
(t/testing "search on the old team fails with not-found"
(let [out (th/command! {::th/type :search-files
::rpc/profile-id (:id editor)
:team-id (:id team)
:search-term "searchable"})
error (:error out)]
(t/is (th/ex-info? error))
(t/is (th/ex-of-type? error :not-found)))))))
(t/deftest pins-are-cleaned-on-leave
;; The project pin (team-project-profile-rel) goes with the leaver.
(let [owner (th/create-profile* 1 {:is-active true})
editor (th/create-profile* 2 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})]
(th/create-team-role* {:team-id (:id team)
:profile-id (:id editor)
:role :editor})
(let [project (th/create-project* 1 {:profile-id (:id editor)
:team-id (:id team)})]
(t/testing "pin the project"
(let [out (th/command! {::th/type :update-project-pin
::rpc/profile-id (:id editor)
:id (:id project)
:team-id (:id team)
:is-pinned true})]
(t/is (th/success? out)))
(t/is (= 1 (count (th/db-query :team-project-profile-rel
{:team-id (:id team)
:profile-id (:id editor)})))))
(t/testing "leave team removes the pin row"
(let [out (th/command! {::th/type :leave-team
::rpc/profile-id (:id editor)
:id (:id team)})]
(t/is (th/success? out)))
(t/is (= 0 (count (th/db-query :team-project-profile-rel
{:team-id (:id team)
:profile-id (:id editor)}))))))))
(t/deftest leaving-one-team-keeps-other-team-access
;; Deletes are scoped by team: leaving A keeps B's files usable.
(let [owner (th/create-profile* 1 {:is-active true})
editor (th/create-profile* 2 {:is-active true})
team-a (th/create-team* 1 {:profile-id (:id owner)})
team-b (th/create-team* 2 {:profile-id (:id owner)})]
(th/create-team-role* {:team-id (:id team-a)
:profile-id (:id editor)
:role :editor})
(th/create-team-role* {:team-id (:id team-b)
:profile-id (:id editor)
:role :editor})
(let [project-a (th/create-project* 1 {:profile-id (:id editor)
:team-id (:id team-a)})
file-a (th/create-file* 1 {:profile-id (:id editor)
:project-id (:id project-a)})
project-b (th/create-project* 2 {:profile-id (:id editor)
:team-id (:id team-b)})
file-b (th/create-file* 2 {:profile-id (:id editor)
:project-id (:id project-b)})]
(t/testing "leave team A"
(let [out (th/command! {::th/type :leave-team
::rpc/profile-id (:id editor)
:id (:id team-a)})]
(t/is (th/success? out))))
(t/testing "team A file is gone"
(let [out (th/command! {::th/type :rename-file
::rpc/profile-id (:id editor)
:id (:id file-a)
:name "renamed"})
error (:error out)]
(t/is (th/ex-info? error))
(t/is (th/ex-of-type? error :not-found))))
(t/testing "team B file still works"
(let [out (th/command! {::th/type :rename-file
::rpc/profile-id (:id editor)
:id (:id file-b)
:name "renamed"})]
(t/is (th/success? out)))))))
(t/deftest orphan-project-role-grants-nothing-and-lists-nothing
;; Mirror of the file orphan test at project level, including the
;; get-all-projects listing (covers the F1 guard).
(let [owner (th/create-profile* 1 {:is-active true})
stranger (th/create-profile* 2 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})
project (th/create-project* 1 {:profile-id (:id owner)
:team-id (:id team)})]
;; Plant an orphan project role for a profile that was never a member.
(th/create-project-role* {:project-id (:id project)
:profile-id (:id stranger)
:role :editor})
(t/testing "orphan project role cannot read the project"
(let [out (th/command! {::th/type :get-project
::rpc/profile-id (:id stranger)
:id (:id project)})
error (:error out)]
(t/is (th/ex-info? error))
(t/is (th/ex-of-type? error :not-found))))
(t/testing "orphan project is not listed"
(let [out (th/command! {::th/type :get-all-projects
::rpc/profile-id (:id stranger)})]
(t/is (th/success? out))
(t/is (nil? (some #(= (:id project) (:id %)) (:result out))))))))
(t/deftest create-team-emits-add-team-member-audit-event
;; Team creation adds the owner through add-profile-to-team!, which used to
;; leave no membership event.
(with-mocks [audit-mock {:target 'app.loggers.audit/submit :return nil}]
(th/with-audit-config (conj cf/flags :nexus)
(let [owner (th/create-profile* 1 {:is-active true})
team (th/create-team* 21 {:profile-id (:id owner)})
events (->> (:call-args-list @audit-mock)
(map second)
(filter #(= "add-team-member" (:name %)))
(filter #(= (:id team) (get-in % [:props :team-id]))))]
(t/is (= 1 (count events)))
(t/is (= (:id owner) (get-in (first events) [:props :member-id])))
(t/is (true? (get-in (first events) [:props :is-owner])))))))
(t/deftest add-team-member-event-skipped-when-membership-exists
;; Adding an existing member with on-conflict-do-nothing inserts nothing and
;; must not report a membership that already exists.
(with-mocks [audit-mock {:target 'app.loggers.audit/submit :return nil}]
(th/with-audit-config (conj cf/flags :nexus)
(let [owner (th/create-profile* 1 {:is-active true})
team (th/create-team* 22 {:profile-id (:id owner)})]
(th/reset-mock! audit-mock)
(db/tx-run! th/*system*
(fn [cfg]
(teams/add-profile-to-team! cfg
{:team-id (:id team)
:profile-id (:id owner)}
{::db/on-conflict-do-nothing? true})))
(t/is (empty? (:call-args-list @audit-mock)))))))
(t/deftest no-add-team-member-event-without-a-consumer
(with-mocks [audit-mock {:target 'app.loggers.audit/submit :return nil}]
(let [owner (th/create-profile* 1 {:is-active true})
_team (th/create-team* 23 {:profile-id (:id owner)})
events (->> (:call-args-list @audit-mock)
(map second)
(filter #(= "add-team-member" (:name %))))]
(t/is (empty? events)))))
(t/deftest add-team-member-event-carries-derived-role
;; `team_profile_rel` has no role column, so no caller can pass it: the
;; emitter derives it from the flags, inverse of permissions-for-role.
(with-mocks [audit-mock {:target 'app.loggers.audit/submit :return nil}]
(th/with-audit-config (conj cf/flags :nexus)
(let [owner (th/create-profile* 1 {:is-active true})
team (th/create-team* 26 {:profile-id (:id owner)})
editor (th/create-profile* 27 {:is-active true})
admin (th/create-profile* 28 {:is-active true})
viewer (th/create-profile* 29 {:is-active true})]
(db/tx-run! th/*system*
(fn [cfg]
(doseq [[member role] [[editor :editor]
[admin :admin]
[viewer :viewer]]]
(teams/add-profile-to-team!
cfg
(merge {:team-id (:id team)
:profile-id (:id member)}
(get types.team/permissions-for-role role))))))
(let [by-member (->> (:call-args-list @audit-mock)
(map second)
(filter #(= "add-team-member" (:name %)))
(d/index-by #(get-in % [:props :member-id])))]
(t/is (= :owner (get-in by-member [(:id owner) :props :role])))
(t/is (= :editor (get-in by-member [(:id editor) :props :role])))
(t/is (= :admin (get-in by-member [(:id admin) :props :role])))
(t/is (= :viewer (get-in by-member [(:id viewer) :props :role]))))))))
(t/deftest add-team-member-event-carries-request-context
;; Sibling events built by `prepare-rpc-event` carry the request context;
;; the hand-built membership event must not lose it when rpc params travel.
(with-mocks [audit-mock {:target 'app.loggers.audit/submit :return nil}]
(th/with-audit-config (conj cf/flags :nexus)
(let [owner (th/create-profile* 1 {:is-active true})
member (th/create-profile* 2 {:is-active true})
team (th/create-team* 30 {:profile-id (:id owner)})]
(th/reset-mock! audit-mock)
(db/tx-run! th/*system*
(fn [cfg]
(teams/add-profile-to-team!
cfg
(merge {:team-id (:id team)
:profile-id (:id member)}
(get types.team/permissions-for-role :editor))
nil
{:rpc-params {::rpc/profile-id (:id owner)
::rpc/request-id "req-1"}})))
(let [events (->> (:call-args-list @audit-mock)
(map second)
(filter #(= "add-team-member" (:name %))))]
(t/is (= 1 (count events)))
(t/is (= "req-1" (get-in (first events) [:context :request-id]))))))))
(t/deftest invitation-direct-add-attributes-event-to-inviter
;; With email verification off, inviting an existing profile adds the
;; member directly; the event belongs to the inviter, not the member.
(with-mocks [audit-mock {:target 'app.loggers.audit/submit :return nil}]
(th/with-audit-config #{:nexus}
(let [owner (th/create-profile* 1 {:is-active true})
member (th/create-profile* 2 {:is-active true})
team (th/create-team* 31 {:profile-id (:id owner)})]
(th/reset-mock! audit-mock)
(let [out (th/command! {::th/type :create-team-invitations
::rpc/profile-id (:id owner)
:team-id (:id team)
:role :editor
:emails [(:email member)]})]
(t/is (th/success? out))
(let [events (->> (:call-args-list @audit-mock)
(map second)
(filter #(= "add-team-member" (:name %)))
(filter #(= (:id member) (get-in % [:props :member-id]))))]
(t/is (= 1 (count events)))
(t/is (= (:id owner) (:profile-id (first events))))
(t/is (= :editor (get-in (first events) [:props :role])))))))))
(t/deftest create-team-audit-event-carries-created-at
;; The projection keys the new team on the event id and needs its creation
;; time from the same event.
(let [profile (th/create-profile* 1 {:is-active true})
out (th/command-raw! {::th/type :create-team
::rpc/profile-id (:id profile)
:name "team"})
props (:app.loggers.audit/props (meta out))]
(t/is (= (:id out) (:id props)))
(t/is (= (:created-at out) (:created-at props)))
(t/is (ct/inst? (:created-at props)))
(t/is (= (:modified-at out) (:modified-at props)))
(t/is (ct/inst? (:modified-at props)))))
(t/deftest delete-team-audit-event-carries-entity-data
;; The projection marks the team deleted from this single event, so it needs
;; the identity, the name and the timestamps.
(let [profile (th/create-profile* 1 {:is-active true})
team (th/create-team* 31 {:profile-id (:id profile)})
out (th/command-raw! {::th/type :delete-team
::rpc/profile-id (:id profile)
:id (:id team)})
props (:app.loggers.audit/props (meta out))]
;; The API response is unchanged: the command still returns nil.
(t/is (nil? (rph/unwrap out)))
(t/is (= (:id team) (:id props)))
(t/is (= (:name team) (:name props)))
(t/is (= (:created-at team) (:created-at props)))
(t/is (ct/inst? (:modified-at props)))
(t/is (ct/inst? (:deleted-at props)))))
(t/deftest create-team-with-invitations-audit-event-carries-team-id
;; The team is created by an internal call, so both the command event and
;; the manual event must carry its id for a projection to key it.
(with-mocks [audit-mock {:target 'app.loggers.audit/submit :return nil}
email-mock {:target 'app.email/send-template :return nil}]
(th/with-audit-config (conj cf/flags :nexus)
(let [profile (th/create-profile* 1 {:is-active true})
out (th/command-raw! {::th/type :create-team-with-invitations
::rpc/profile-id (:id profile)
:name "team"
:emails #{"invitee@example.com"}
:role :editor})
props (:app.loggers.audit/props (meta out))
events (->> (:call-args-list @audit-mock)
(map second)
(filter #(= "create-team" (:name %))))]
(t/is (= (:id out) (:id props)))
(t/is (= 1 (count events)))
(t/is (= (:id out) (get-in (first events) [:props :id])))))))
(t/deftest update-team-member-role-to-owner-names-demoted-member
;; Promoting a member to owner demotes the actor; the event names them so a
;; projection can keep a single owner.
(let [owner (th/create-profile* 1 {:is-active true})
editor (th/create-profile* 2 {:is-active true})
team (th/create-team* 82 {:profile-id (:id owner)})]
(th/create-team-role* {:team-id (:id team)
:profile-id (:id editor)
:role :editor})
(let [out (th/command-raw! {::th/type :update-team-member-role
::rpc/profile-id (:id owner)
:team-id (:id team)
:member-id (:id editor)
:role :editor})
props (:app.loggers.audit/props (meta out))]
(t/is (nil? (:prev-owner-id props))))
(let [out (th/command-raw! {::th/type :update-team-member-role
::rpc/profile-id (:id owner)
:team-id (:id team)
:member-id (:id editor)
:role :owner})
props (:app.loggers.audit/props (meta out))]
(t/is (= (:id owner) (:prev-owner-id props))))))
(t/deftest create-team-invitations-link-base-follows-trusted-origins
(let [owner (th/create-profile* 1 {:is-active true})
team (th/create-team* 1 {:profile-id (:id owner)})
cases [{:origin "https://alt.example.com"
:email "trusted@example.com"
:expected "https://alt.example.com"}
{:origin "https://evil.example.com"
:email "untrusted@example.com"
:expected "http://localhost:3449"}]]
(doseq [{:keys [origin email expected]} cases]
(with-mocks [mock {:target 'app.email/send-template :return nil}]
(with-redefs [cf/config (assoc cf/config
:public-uri "http://localhost:3449"
:trusted-origins #{"https://alt.example.com"})]
(let [data {::th/type :create-team-invitations
::rpc/profile-id (:id owner)
:team-id (:id team)
:role :editor
:emails [email]}
out (th/command-through-middleware data {:headers {"origin" origin}})
sent (-> @mock :call-args-list first second)]
(t/is (th/success? out) origin)
(t/is (= expected (str (:public-uri sent))) origin)))))))