Use userId in player, other fixes (#3182)

* [bugfix] player: use userId, other fixes

This PR primarily resolves #1928 by switching the foreign key of `player` from `user.user_name` to `user.id`.
There are also a few other fixes/changes:

- For some bizarre reason, `ip_address` is never returned from `read`/`get`. Change the field to `ip`, which works. Somehow
- Update `players_test.go` mock to also check for user agent, replicating the actual code
- Update `player_repository.go` `isPermitted` to check user id. I don't know how this worked before...
- tests!
- a few places referred to `typ`, when it is really `userAgent`. Change the field names

* baseRequest -> selectPlayer

* remove comment

* update migration, make all of persistence foreign key enabled

* maybe don't forget to save the file first
This commit is contained in:
Kendall Garner 2024-08-03 17:37:21 +00:00 committed by GitHub
parent 5360283bb0
commit fa85e2a781
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 373 additions and 32 deletions

View file

@ -21,7 +21,7 @@ func TestPersistence(t *testing.T) {
//os.Remove("./test-123.db")
//conf.Server.DbPath = "./test-123.db"
conf.Server.DbPath = "file::memory:?cache=shared"
conf.Server.DbPath = "file::memory:?cache=shared&_foreign_keys=on"
defer db.Init()()
log.SetLevel(log.LevelError)
RegisterFailHandler(Fail)
@ -83,6 +83,12 @@ var (
testPlaylists []*model.Playlist
)
var (
adminUser = model.User{ID: "userid", UserName: "userid", Name: "admin", Email: "admin@email.com", IsAdmin: true}
regularUser = model.User{ID: "2222", UserName: "regular-user", Name: "Regular User", Email: "regular@example.com"}
testUsers = model.Users{adminUser, regularUser}
)
func P(path string) string {
return filepath.FromSlash(path)
}
@ -92,13 +98,14 @@ func P(path string) string {
var _ = BeforeSuite(func() {
conn := NewDBXBuilder(db.Db())
ctx := log.NewContext(context.TODO())
user := model.User{ID: "userid", UserName: "userid", IsAdmin: true}
ctx = request.WithUser(ctx, user)
ctx = request.WithUser(ctx, adminUser)
ur := NewUserRepository(ctx, conn)
err := ur.Put(&user)
if err != nil {
panic(err)
for i := range testUsers {
err := ur.Put(&testUsers[i])
if err != nil {
panic(err)
}
}
gr := NewGenreRepository(ctx, conn)