mirror of
https://github.com/phishingclub/phishingclub.git
synced 2026-12-06 10:03:46 +01:00
54 lines
1.6 KiB
Go
54 lines
1.6 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/phishingclub/phishingclub/database"
|
|
"github.com/phishingclub/phishingclub/testdb"
|
|
)
|
|
|
|
// TestScriptGetByNameScoped checks the company script wins over a global one
|
|
func TestScriptGetByNameScoped(t *testing.T) {
|
|
db, captured := newBuilderDB(t, &database.Script{})
|
|
r := &Script{DB: db}
|
|
a := uuid.New()
|
|
insertScript(t, db, "shared", "global body", nil)
|
|
insertScript(t, db, "shared", "company body", &a)
|
|
insertScript(t, db, "only global", "only global body", nil)
|
|
|
|
got, err := r.GetByNameScoped(context.Background(), "shared", &a)
|
|
if err != nil {
|
|
t.Fatalf("company lookup: %v", err)
|
|
}
|
|
if got.CompanyID.IsNull() {
|
|
t.Fatal("company lookup: want the company script, got the global one")
|
|
}
|
|
q := lastQuery(t, captured)
|
|
checkPlaceholders(t, q)
|
|
// postgres binds the limit, sqlite writes it
|
|
want := "ORDER BY `scripts`.`company_id` IS NULL, `scripts`.`id` LIMIT 1"
|
|
if testdb.IsPostgres() {
|
|
want = "ORDER BY `scripts`.`company_id` IS NULL, `scripts`.`id` LIMIT ?"
|
|
}
|
|
if !containsSQL(q.sql, want) {
|
|
t.Fatalf("missing company order in %q", q.sql)
|
|
}
|
|
|
|
got, err = r.GetByNameScoped(context.Background(), "shared", nil)
|
|
if err != nil {
|
|
t.Fatalf("global lookup: %v", err)
|
|
}
|
|
if !got.CompanyID.IsNull() {
|
|
t.Fatal("global lookup: want the global script")
|
|
}
|
|
if _, err := r.GetByNameScoped(context.Background(), "only global", &a); err != nil {
|
|
t.Fatalf("global script must be visible to a company: %v", err)
|
|
}
|
|
b := uuid.New()
|
|
if _, err := r.GetByNameScoped(context.Background(), "shared", &b); err != nil {
|
|
t.Fatalf("other company sees the global script: %v", err)
|
|
}
|
|
}
|