|
| 1 | +// Copyright 2023 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package oauth2adapt |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "net/http" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "cloud.google.com/go/auth" |
| 24 | + "github.com/google/go-cmp/cmp" |
| 25 | + "golang.org/x/oauth2" |
| 26 | +) |
| 27 | + |
| 28 | +func TestTokenProviderFromTokenSource(t *testing.T) { |
| 29 | + tests := []struct { |
| 30 | + name string |
| 31 | + token string |
| 32 | + err error |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "working token", |
| 36 | + token: "fakeToken", |
| 37 | + err: nil, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "coverts err", |
| 41 | + err: &oauth2.RetrieveError{ |
| 42 | + Body: []byte("some bytes"), |
| 43 | + ErrorCode: "412", |
| 44 | + Response: &http.Response{ |
| 45 | + StatusCode: http.StatusTeapot, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + } |
| 50 | + for _, tt := range tests { |
| 51 | + t.Run(tt.name, func(t *testing.T) { |
| 52 | + tp := TokenProviderFromTokenSource(tokenSource{ |
| 53 | + token: tt.token, |
| 54 | + err: tt.err, |
| 55 | + }) |
| 56 | + tok, err := tp.Token(context.Background()) |
| 57 | + if tt.err != nil { |
| 58 | + aErr := &auth.Error{} |
| 59 | + if !errors.As(err, &aErr) { |
| 60 | + t.Fatalf("error not of correct type: %T", err) |
| 61 | + } |
| 62 | + err := tt.err.(*oauth2.RetrieveError) |
| 63 | + if !cmp.Equal(aErr.Body, err.Body) { |
| 64 | + t.Errorf("got %s, want %s", aErr.Body, err.Body) |
| 65 | + } |
| 66 | + if !cmp.Equal(aErr.Err, err) { |
| 67 | + t.Errorf("got %s, want %s", aErr.Err, err) |
| 68 | + } |
| 69 | + if !cmp.Equal(aErr.Response, err.Response) { |
| 70 | + t.Errorf("got %s, want %s", aErr.Err, err) |
| 71 | + } |
| 72 | + return |
| 73 | + } |
| 74 | + if tok.Value != tt.token { |
| 75 | + t.Errorf("got %q, want %q", tok.Value, tt.token) |
| 76 | + } |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestTokenSourceFromTokenProvider(t *testing.T) { |
| 82 | + tests := []struct { |
| 83 | + name string |
| 84 | + token string |
| 85 | + err error |
| 86 | + }{ |
| 87 | + { |
| 88 | + name: "working token", |
| 89 | + token: "fakeToken", |
| 90 | + err: nil, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "coverts err", |
| 94 | + err: &auth.Error{ |
| 95 | + Body: []byte("some bytes"), |
| 96 | + Response: &http.Response{ |
| 97 | + StatusCode: http.StatusTeapot, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + } |
| 102 | + for _, tt := range tests { |
| 103 | + t.Run(tt.name, func(t *testing.T) { |
| 104 | + ts := TokenSourceFromTokenProvider(tokenProvider{ |
| 105 | + token: tt.token, |
| 106 | + err: tt.err, |
| 107 | + }) |
| 108 | + tok, err := ts.Token() |
| 109 | + if tt.err != nil { |
| 110 | + // Should be able to be an auth.Error |
| 111 | + aErr := &auth.Error{} |
| 112 | + if !errors.As(err, &aErr) { |
| 113 | + t.Fatalf("error not of correct type: %T", err) |
| 114 | + } |
| 115 | + err := tt.err.(*auth.Error) |
| 116 | + if !cmp.Equal(aErr.Body, err.Body) { |
| 117 | + t.Errorf("got %s, want %s", aErr.Body, err.Body) |
| 118 | + } |
| 119 | + if !cmp.Equal(aErr.Response, err.Response) { |
| 120 | + t.Errorf("got %s, want %s", aErr.Err, err) |
| 121 | + } |
| 122 | + |
| 123 | + // Should be able to be an oauth2.RetrieveError |
| 124 | + rErr := &oauth2.RetrieveError{} |
| 125 | + if !errors.As(err, &rErr) { |
| 126 | + t.Fatalf("error not of correct type: %T", err) |
| 127 | + } |
| 128 | + if !cmp.Equal(rErr.Body, err.Body) { |
| 129 | + t.Errorf("got %s, want %s", aErr.Body, err.Body) |
| 130 | + } |
| 131 | + if !cmp.Equal(rErr.Response, err.Response) { |
| 132 | + t.Errorf("got %s, want %s", aErr.Err, err) |
| 133 | + } |
| 134 | + return |
| 135 | + } |
| 136 | + if tok.AccessToken != tt.token { |
| 137 | + t.Errorf("got %q, want %q", tok.AccessToken, tt.token) |
| 138 | + } |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +type tokenSource struct { |
| 144 | + token string |
| 145 | + err error |
| 146 | +} |
| 147 | + |
| 148 | +func (ts tokenSource) Token() (*oauth2.Token, error) { |
| 149 | + if ts.err != nil { |
| 150 | + return nil, ts.err |
| 151 | + } |
| 152 | + return &oauth2.Token{ |
| 153 | + AccessToken: ts.token, |
| 154 | + }, nil |
| 155 | +} |
| 156 | + |
| 157 | +type tokenProvider struct { |
| 158 | + token string |
| 159 | + err error |
| 160 | +} |
| 161 | + |
| 162 | +func (tp tokenProvider) Token(context.Context) (*auth.Token, error) { |
| 163 | + if tp.err != nil { |
| 164 | + return nil, tp.err |
| 165 | + } |
| 166 | + return &auth.Token{ |
| 167 | + Value: tp.token, |
| 168 | + }, nil |
| 169 | +} |
0 commit comments