summaryrefslogtreecommitdiff
path: root/internal/db/bundb/headerfilter_test.go
blob: d7e2b26ee241cfa921df61fab9688e6c8d341d54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// 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 <http://www.gnu.org/licenses/>.

package bundb_test

import (
	"context"
	"testing"

	"github.com/stretchr/testify/suite"
	"github.com/superseriousbusiness/gotosocial/internal/db"
	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
)

type HeaderFilterTestSuite struct {
	BunDBStandardTestSuite
}

func (suite *HeaderFilterTestSuite) TestAllowHeaderFilterGetPutUpdateDelete() {
	suite.testHeaderFilterGetPutUpdateDelete(
		suite.db.GetAllowHeaderFilter,
		suite.db.GetAllowHeaderFilters,
		suite.db.PutAllowHeaderFilter,
		suite.db.UpdateAllowHeaderFilter,
		suite.db.DeleteAllowHeaderFilter,
	)
}

func (suite *HeaderFilterTestSuite) TestBlockHeaderFilterGetPutUpdateDelete() {
	suite.testHeaderFilterGetPutUpdateDelete(
		suite.db.GetBlockHeaderFilter,
		suite.db.GetBlockHeaderFilters,
		suite.db.PutBlockHeaderFilter,
		suite.db.UpdateBlockHeaderFilter,
		suite.db.DeleteBlockHeaderFilter,
	)
}

func (suite *HeaderFilterTestSuite) testHeaderFilterGetPutUpdateDelete(
	get func(context.Context, string) (*gtsmodel.HeaderFilter, error),
	getAll func(context.Context) ([]*gtsmodel.HeaderFilter, error),
	put func(context.Context, *gtsmodel.HeaderFilter) error,
	update func(context.Context, *gtsmodel.HeaderFilter, ...string) error,
	delete func(context.Context, string) error,
) {
	t := suite.T()

	// Create new example header filter.
	filter := gtsmodel.HeaderFilter{
		ID:       "some unique id",
		Header:   "Http-Header-Key",
		Regex:    ".*",
		AuthorID: "some unique author id",
	}

	// Create new cancellable test context.
	ctx := context.Background()
	ctx, cncl := context.WithCancel(ctx)
	defer cncl()

	// Insert the example header filter into db.
	if err := put(ctx, &filter); err != nil {
		t.Fatalf("error inserting header filter: %v", err)
	}

	// Now fetch newly created filter.
	check, err := get(ctx, filter.ID)
	if err != nil {
		t.Fatalf("error fetching header filter: %v", err)
	}

	// Check all expected fields match.
	suite.Equal(filter.ID, check.ID)
	suite.Equal(filter.Header, check.Header)
	suite.Equal(filter.Regex, check.Regex)
	suite.Equal(filter.AuthorID, check.AuthorID)

	// Fetch all header filters.
	all, err := getAll(ctx)
	if err != nil {
		t.Fatalf("error fetching header filters: %v", err)
	}

	// Ensure contains example.
	suite.Equal(len(all), 1)
	suite.Equal(all[0].ID, filter.ID)

	// Update the header filter regex value.
	check.Regex = "new regex value"
	if err := update(ctx, check); err != nil {
		t.Fatalf("error updating header filter: %v", err)
	}

	// Ensure 'updated_at' was updated on check model.
	suite.True(check.UpdatedAt.After(filter.UpdatedAt))

	// Now delete the header filter from db.
	if err := delete(ctx, filter.ID); err != nil {
		t.Fatalf("error deleting header filter: %v", err)
	}

	// Ensure we can't refetch it.
	_, err = get(ctx, filter.ID)
	if err != db.ErrNoEntries {
		t.Fatalf("deleted header filter returned unexpected error: %v", err)
	}
}

func TestHeaderFilterTestSuite(t *testing.T) {
	suite.Run(t, new(HeaderFilterTestSuite))
}