summaryrefslogtreecommitdiff
path: root/player-server/internal/service/note_test.go
blob: ae985b7309be47b3bdc35588ce6f05707a58b6dd (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
package service

import (
	"context"
	"errors"
	"testing"

	"codeberg.org/snonux/player/internal/clock"
	"codeberg.org/snonux/player/internal/model"
	"codeberg.org/snonux/player/internal/repository"
)

func TestNoteService_GetNote_AccessCheck(t *testing.T) {
	ctx := context.Background()
	store := &repository.MockStore{
		MediaRepo: repository.MockMediaRepo{
			GetMediaByIDFunc: func(ctx context.Context, id int64) (*model.Media, error) {
				return &model.Media{ID: 1, SetID: 1}, nil
			},
		},
		UserRepo: repository.MockUserRepo{
			GetUserByIDFunc: func(ctx context.Context, id int64) (*model.User, error) {
				return &model.User{ID: id, IsAdmin: false}, nil
			},
		},
		SetRepo: repository.MockSetRepo{
			GetSetByIDFunc: func(ctx context.Context, id int64) (*model.Set, error) {
				return &model.Set{ID: id}, nil
			},
		},
		SetPermissionRepo: repository.MockSetPermissionRepo{
			GetPermissionFunc: func(ctx context.Context, setID, userID int64) (*model.SetPermission, error) {
				return nil, nil
			},
		},
	}
	svc := NewNoteService(store, clock.RealClock{}, &accessHelper{store: store})
	_, err := svc.GetNote(ctx, 1, 2)
	if !errors.Is(err, ErrForbidden) {
		t.Fatalf("expected ErrForbidden, got %v", err)
	}
}

func TestNoteService_SetNote(t *testing.T) {
	ctx := context.Background()

	tests := []struct {
		name      string
		method    string
		wantErr   bool
		noteErr   error
		upsertErr error
		deleteErr error
	}{
		{name: "get note", method: "get"},
		{name: "get note error", method: "get", wantErr: true, noteErr: errors.New("boom")},
		{name: "upsert note", method: "upsert"},
		{name: "upsert error", method: "upsert", wantErr: true, upsertErr: errors.New("boom")},
		{name: "delete note", method: "delete"},
		{name: "delete error", method: "delete", wantErr: true, deleteErr: errors.New("boom")},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			store := &repository.MockStore{
				MediaRepo: repository.MockMediaRepo{
					GetMediaByIDFunc: func(ctx context.Context, id int64) (*model.Media, error) {
						return &model.Media{ID: 1, SetID: 1}, nil
					},
				},
				UserRepo: repository.MockUserRepo{
					GetUserByIDFunc: func(ctx context.Context, id int64) (*model.User, error) {
						return &model.User{ID: id, IsAdmin: true}, nil
					},
				},
				SetRepo: repository.MockSetRepo{
					GetSetByIDFunc: func(ctx context.Context, id int64) (*model.Set, error) {
						return &model.Set{ID: id}, nil
					},
				},
				NoteRepo: repository.MockNoteRepo{
					GetNoteFunc: func(ctx context.Context, mediaID, userID int64) (*model.Note, error) {
						return &model.Note{MediaID: mediaID, UserID: userID, Content: "hello"}, tt.noteErr
					},
					UpsertNoteFunc: func(ctx context.Context, note *model.Note) error {
						return tt.upsertErr
					},
					DeleteNoteFunc: func(ctx context.Context, mediaID, userID int64) error {
						return tt.deleteErr
					},
				},
			}
			svc := NewNoteService(store, clock.RealClock{}, &accessHelper{store: store})
			var err error
			switch tt.method {
			case "get":
				_, err = svc.GetNote(ctx, 1, 1)
			case "upsert":
				err = svc.UpsertNote(ctx, &model.Note{MediaID: 1, UserID: 1, Content: "hi"})
			case "delete":
				err = svc.DeleteNote(ctx, 1, 1)
			}
			if tt.wantErr {
				if err == nil {
					t.Fatal("expected error")
				}
				return
			}
			if err != nil {
				t.Fatalf("unexpected error: %v", err)
			}
		})
	}
}