github.com/quickfeed/quickfeed@v0.0.0-20240507093252-ed8ca812a09c/database/db_user_test.go (about) 1 package database_test 2 3 import ( 4 "testing" 5 6 "github.com/google/go-cmp/cmp" 7 "github.com/quickfeed/quickfeed/internal/qtest" 8 "github.com/quickfeed/quickfeed/qf" 9 "google.golang.org/protobuf/testing/protocmp" 10 ) 11 12 func TestDBUpdateUserAccessToken(t *testing.T) { 13 const ( 14 accessToken = "123" 15 newAccessToken = "4567890" 16 remoteID = 10 17 ) 18 wantUser := &qf.User{ 19 ID: 1, 20 IsAdmin: true, // first user is always admin 21 } 22 23 db, cleanup := qtest.TestDB(t) 24 defer cleanup() 25 26 var user qf.User 27 if err := db.CreateUser(&user); err != nil { 28 t.Error(err) 29 } 30 gotUser, err := db.GetUser(user.ID) 31 if err != nil { 32 t.Error(err) 33 } 34 if diff := cmp.Diff(wantUser, gotUser, protocmp.Transform()); diff != "" { 35 t.Errorf("GetUser() mismatch (-wantUser, +gotUser):\n%s", diff) 36 } 37 38 user.RefreshToken = accessToken 39 user.ScmRemoteID = remoteID 40 if err := db.UpdateUser(&user); err != nil { 41 t.Error(err) 42 } 43 gotUser, err = db.GetUser(user.ID) 44 if err != nil { 45 t.Error(err) 46 } 47 wantUser.RefreshToken = accessToken 48 wantUser.ScmRemoteID = remoteID 49 if diff := cmp.Diff(wantUser, gotUser, protocmp.Transform()); diff != "" { 50 t.Errorf("GetUser() mismatch (-wantUser, +gotUser):\n%s", diff) 51 } 52 53 // do another update 54 user.RefreshToken = newAccessToken 55 if err := db.UpdateUser(&user); err != nil { 56 t.Error(err) 57 } 58 gotUser, err = db.GetUser(user.ID) 59 if err != nil { 60 t.Error(err) 61 } 62 wantUser.RefreshToken = newAccessToken 63 if diff := cmp.Diff(wantUser, gotUser, protocmp.Transform()); diff != "" { 64 t.Errorf("GetUser() mismatch (-wantUser, +gotUser):\n%s", diff) 65 } 66 } 67 68 func TestDBUpdateAccessTokenUserGetAccessToken(t *testing.T) { 69 const ( 70 newAccessToken = "123" 71 anotherToken = "456" 72 ) 73 db, cleanup := qtest.TestDB(t) 74 defer cleanup() 75 wantUser := qtest.CreateFakeUser(t, db) 76 77 cachedAccessToken := wantUser.GetRefreshToken() 78 79 // Update the access token for the user. 80 wantUser.RefreshToken = newAccessToken 81 if err := db.UpdateUser(wantUser); err != nil { 82 t.Error(err) 83 } 84 gotUser, err := db.GetUser(wantUser.ID) 85 if err != nil { 86 t.Error(err) 87 } 88 89 // Assign the access token we expect to the wantUser object. 90 wantUser.RefreshToken = newAccessToken 91 if diff := cmp.Diff(wantUser, gotUser, protocmp.Transform()); diff != "" { 92 t.Errorf("GetUser() mismatch (-wantUser +gotUser):\n%s", diff) 93 } 94 cachedAccessToken2 := gotUser.GetRefreshToken() 95 96 if cachedAccessToken == cachedAccessToken2 { 97 t.Errorf("cached access token before and after are the same: %s == %s", cachedAccessToken, cachedAccessToken2) 98 } 99 100 // Update the access token again for the user. 101 wantUser.RefreshToken = anotherToken 102 if err := db.UpdateUser(wantUser); err != nil { 103 t.Error(err) 104 } 105 gotUser, err = db.GetUser(wantUser.ID) 106 if err != nil { 107 t.Error(err) 108 } 109 if diff := cmp.Diff(wantUser, gotUser, protocmp.Transform()); diff != "" { 110 t.Errorf("GetUser() mismatch (-wantUser +gotUser):\n%s", diff) 111 } 112 cachedAccessToken3 := gotUser.GetRefreshToken() 113 114 if cachedAccessToken2 == cachedAccessToken3 { 115 t.Errorf("cached access token before and after are the same: %s == %s", cachedAccessToken2, cachedAccessToken3) 116 } 117 }