Skip to content

Commit 689a0a7

Browse files
committed
feat(playlist): add methods for RWD access checking
1 parent 19c37d5 commit 689a0a7

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

playlist/playlist.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ type Playlist struct {
3636
IsPublic bool
3737
SharedWith []int
3838
}
39+
40+
func (p Playlist) CanRead(uid int) bool {
41+
return p.IsPublic || p.CanWrite(uid)
42+
}
43+
44+
func (p Playlist) CanWrite(uid int) bool {
45+
return p.UserID == uid || slices.Contains(p.SharedWith, uid)
46+
}
47+
48+
func (p Playlist) CanDelete(uid int) bool {
49+
return p.UserID == uid
3950
}
4051

4152
type Store struct {

playlist/playlist_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,70 @@ It has multiple lines 👍
5757
require.NoError(t, err)
5858
require.True(t, len(playlistIDs) == 1)
5959
}
60+
61+
func TestAccess(t *testing.T) {
62+
t.Parallel()
63+
64+
for _, tc := range []struct {
65+
name string
66+
playlist playlist.Playlist
67+
userID int
68+
expectRead bool
69+
expectWrite bool
70+
expectDelete bool
71+
}{
72+
{
73+
name: "owner can do anything",
74+
playlist: playlist.Playlist{
75+
UserID: 7,
76+
},
77+
userID: 7,
78+
expectRead: true,
79+
expectWrite: true,
80+
expectDelete: true,
81+
},
82+
{
83+
name: "third party cannot do anything",
84+
playlist: playlist.Playlist{
85+
UserID: 7,
86+
},
87+
userID: 99,
88+
expectRead: false,
89+
expectWrite: false,
90+
expectDelete: false,
91+
},
92+
{
93+
name: "third party can read if public",
94+
playlist: playlist.Playlist{
95+
IsPublic: true,
96+
UserID: 7,
97+
},
98+
userID: 99,
99+
expectRead: true,
100+
expectWrite: false,
101+
expectDelete: false,
102+
},
103+
{
104+
name: "shared user can read and write",
105+
playlist: playlist.Playlist{
106+
IsPublic: true,
107+
UserID: 7,
108+
SharedWith: []int{99},
109+
},
110+
userID: 99,
111+
expectRead: true,
112+
expectWrite: true,
113+
expectDelete: false,
114+
},
115+
} {
116+
tc := tc
117+
118+
t.Run(tc.name, func(t *testing.T) {
119+
t.Parallel()
120+
121+
require.Equal(t, tc.expectRead, tc.playlist.CanRead(tc.userID))
122+
require.Equal(t, tc.expectWrite, tc.playlist.CanWrite(tc.userID))
123+
require.Equal(t, tc.expectDelete, tc.playlist.CanDelete(tc.userID))
124+
})
125+
}
126+
}

0 commit comments

Comments
 (0)