package searchaccessser import ( "testing" "time" ) func TestSearchAccessToken(t *testing.T) { const ( secret = "unit-test-secret" uid = uint64(32239213) videoID = "64b100000000000000000001" ) now := time.Now().Truncate(time.Second) token, err := issue(secret, uid, videoID, now) if err != nil { t.Fatalf("issue() error = %v", err) } tests := []struct { name string secret string uid uint64 video string now time.Time wantOK bool }{ {name: "valid", secret: secret, uid: uid, video: videoID, now: now.Add(time.Minute), wantOK: true}, {name: "wrong uid", secret: secret, uid: uid + 1, video: videoID, now: now.Add(time.Minute)}, {name: "wrong video", secret: secret, uid: uid, video: "64b100000000000000000002", now: now.Add(time.Minute)}, {name: "wrong secret", secret: "another-secret", uid: uid, video: videoID, now: now.Add(time.Minute)}, {name: "expired", secret: secret, uid: uid, video: videoID, now: now.Add(tokenTTL + time.Second)}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { if got := validate(test.secret, token, test.uid, test.video, test.now); got != test.wantOK { t.Fatalf("validate() = %v, want %v", got, test.wantOK) } }) } } func TestIssueRejectsMissingInputs(t *testing.T) { if _, err := issue("", 1, "64b100000000000000000001", time.Now()); err == nil { t.Fatal("issue() with empty secret should fail") } if _, err := issue("secret", 1, "", time.Now()); err == nil { t.Fatal("issue() with empty video id should fail") } }