Skip to content

armgit / posta

Public Imported from GitHub
0

Self-hosted email delivery platform that enables applications to send emails via HTTP APIs while handling SMTP delivery, templates, storage, security, and analytics. Fully self-hostable alternative to services like SendGrid or Mailgun.

3.6 KB
Raw Blame
// Copyright (c) 2026 Jonas Kaninda.
//
// This file is part of Posta.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package repositories

import (
	"errors"
	"os"
	"testing"

	"github.com/goposta/posta/internal/models"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
)

func testDB(t *testing.T) *gorm.DB {
	t.Helper()
	dsn := os.Getenv("TEST_DATABASE_DSN")
	if dsn == "" {
		dsn = "host=localhost user=posta password=posta dbname=posta port=5432 sslmode=disable"
	}
	db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)})
	if err != nil {
		t.Skipf("skipping: no test database available: %v", err)
	}
	if err := db.AutoMigrate(&models.User{}, &models.StyleSheet{}); err != nil {
		t.Skipf("skipping: cannot migrate test schema: %v", err)
	}
	return db
}

func uintPtr(v uint) *uint { return &v }

func createUser(t *testing.T, tx *gorm.DB, email string) uint {
	t.Helper()
	u := &models.User{Name: "test", Email: email, PasswordHash: "x"}
	if err := tx.Create(u).Error; err != nil {
		t.Fatalf("create user %s: %v", email, err)
	}
	return u.ID
}

func TestFindByIDInScope_RejectsForeignWorkspace(t *testing.T) {
	db := testDB(t)

	tx := db.Begin()
	t.Cleanup(func() { tx.Rollback() })

	repo := NewStyleSheetRepository(tx)

	userA := createUser(t, tx, "[email protected]")
	userB := createUser(t, tx, "[email protected]")

	const wsA, wsB uint = 90001, 90002
	owned := &models.StyleSheet{UserID: userA, WorkspaceID: uintPtr(wsA), Name: "owned", CSS: "body{color:red}"}
	foreign := &models.StyleSheet{UserID: userB, WorkspaceID: uintPtr(wsB), Name: "foreign", CSS: "body{color:blue}"}
	if err := repo.Create(owned); err != nil {
		t.Fatalf("create owned: %v", err)
	}
	if err := repo.Create(foreign); err != nil {
		t.Fatalf("create foreign: %v", err)
	}

	scopeA := ResourceScope{UserID: userA, WorkspaceID: uintPtr(wsA)}

	got, err := repo.FindByIDInScope(scopeA, owned.ID)
	if err != nil {
		t.Fatalf("expected in-scope stylesheet to resolve, got error: %v", err)
	}
	if got.ID != owned.ID {
		t.Fatalf("expected stylesheet %d, got %d", owned.ID, got.ID)
	}

	if _, err := repo.FindByIDInScope(scopeA, foreign.ID); !errors.Is(err, gorm.ErrRecordNotFound) {
		t.Fatalf("expected ErrRecordNotFound for foreign stylesheet, got: %v", err)
	}
}

func TestFindByIDInScope_PersonalScopeIsolated(t *testing.T) {
	db := testDB(t)

	tx := db.Begin()
	t.Cleanup(func() { tx.Rollback() })

	repo := NewStyleSheetRepository(tx)

	userID := createUser(t, tx, "[email protected]")
	wsOwned := &models.StyleSheet{UserID: userID, WorkspaceID: uintPtr(90003), Name: "ws", CSS: "a{}"}
	if err := repo.Create(wsOwned); err != nil {
		t.Fatalf("create: %v", err)
	}

	personal := ResourceScope{UserID: userID}
	if _, err := repo.FindByIDInScope(personal, wsOwned.ID); !errors.Is(err, gorm.ErrRecordNotFound) {
		t.Fatalf("expected personal scope to not resolve workspace stylesheet, got: %v", err)
	}
}