|
| 1 | +package auth |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + _ "embed" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | +) |
| 16 | + |
| 17 | +func Test_getTokenByCookie(t *testing.T) { |
| 18 | + oldTimeFunc := timeFunc |
| 19 | + timeFunc = func() time.Time { |
| 20 | + return time.Date(2025, 6, 1, 0, 0, 0, 0, time.UTC) |
| 21 | + } |
| 22 | + t.Cleanup(func() { |
| 23 | + timeFunc = oldTimeFunc |
| 24 | + }) |
| 25 | + |
| 26 | + type args struct { |
| 27 | + ctx context.Context |
| 28 | + workspaceName string |
| 29 | + dCookie string |
| 30 | + } |
| 31 | + tests := []struct { |
| 32 | + name string |
| 33 | + args args |
| 34 | + testBody []byte |
| 35 | + want string |
| 36 | + want1 []*http.Cookie |
| 37 | + wantErr bool |
| 38 | + }{ |
| 39 | + { |
| 40 | + name: "finds the token and cookies", |
| 41 | + args: args{ |
| 42 | + ctx: context.Background(), |
| 43 | + workspaceName: "test", |
| 44 | + dCookie: "dcookie", |
| 45 | + }, |
| 46 | + testBody: testBody, |
| 47 | + want: "xoxc-000000000300-604451271345-8802919159412-ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", |
| 48 | + want1: []*http.Cookie{ |
| 49 | + {Name: "unit", Value: "test", Raw: "unit=test"}, |
| 50 | + makeCookie("d", "dcookie"), |
| 51 | + }, |
| 52 | + wantErr: false, |
| 53 | + }, |
| 54 | + } |
| 55 | + for _, tt := range tests { |
| 56 | + t.Run(tt.name, func(t *testing.T) { |
| 57 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 58 | + http.SetCookie(w, &http.Cookie{Name: "unit", Value: "test"}) |
| 59 | + io.Copy(w, bytes.NewReader(tt.testBody)) |
| 60 | + })) |
| 61 | + ssbURI = func(string) string { |
| 62 | + return srv.URL |
| 63 | + } |
| 64 | + got, got1, err := getTokenByCookie(tt.args.ctx, tt.args.workspaceName, tt.args.dCookie) |
| 65 | + if (err != nil) != tt.wantErr { |
| 66 | + t.Errorf("getTokenByCookie() error = %v, wantErr %v", err, tt.wantErr) |
| 67 | + return |
| 68 | + } |
| 69 | + if got != tt.want { |
| 70 | + t.Errorf("getTokenByCookie() got = %v, want %v", got, tt.want) |
| 71 | + } |
| 72 | + assert.EqualExportedValues(t, tt.want1, got1) |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +//go:embed testdata/redirect.html |
| 78 | +var testBody []byte |
| 79 | + |
| 80 | +func Test_extractToken(t *testing.T) { |
| 81 | + type args struct { |
| 82 | + r io.Reader |
| 83 | + } |
| 84 | + tests := []struct { |
| 85 | + name string |
| 86 | + args args |
| 87 | + want string |
| 88 | + wantErr bool |
| 89 | + }{ |
| 90 | + { |
| 91 | + name: "extracts token from the HTML body", |
| 92 | + args: args{r: bytes.NewReader(testBody)}, |
| 93 | + want: "xoxc-000000000300-604451271345-8802919159412-ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", |
| 94 | + wantErr: false, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "no token is an error", |
| 98 | + args: args{strings.NewReader("first line\nsecond line\n")}, |
| 99 | + want: "", |
| 100 | + wantErr: true, |
| 101 | + }, |
| 102 | + } |
| 103 | + for _, tt := range tests { |
| 104 | + t.Run(tt.name, func(t *testing.T) { |
| 105 | + got, err := extractToken(tt.args.r) |
| 106 | + if (err != nil) != tt.wantErr { |
| 107 | + t.Errorf("extractToken() error = %v, wantErr %v", err, tt.wantErr) |
| 108 | + return |
| 109 | + } |
| 110 | + if got != tt.want { |
| 111 | + t.Errorf("extractToken() = %v, want %v", got, tt.want) |
| 112 | + } |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
0 commit comments