|
| 1 | +// Copyright (c) Abstract Machines |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package google_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/absmach/supermq/pkg/oauth2" |
| 13 | + "github.com/absmach/supermq/pkg/oauth2/google" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + testClientID = "test-client-id" |
| 20 | + testClientSecret = "test-client-secret" |
| 21 | + testState = "test-state" |
| 22 | + testRedirectURL = "http://localhost/callback" |
| 23 | + testCode = "test-code" |
| 24 | +) |
| 25 | + |
| 26 | +func TestGetAuthURL(t *testing.T) { |
| 27 | + cfg := oauth2.Config{ |
| 28 | + ClientID: testClientID, |
| 29 | + ClientSecret: testClientSecret, |
| 30 | + State: testState, |
| 31 | + RedirectURL: testRedirectURL, |
| 32 | + } |
| 33 | + |
| 34 | + provider := google.NewProvider(cfg, "http://localhost/ui", "http://localhost/error") |
| 35 | + |
| 36 | + authURL := provider.GetAuthURL() |
| 37 | + |
| 38 | + assert.NotEmpty(t, authURL) |
| 39 | + assert.Contains(t, authURL, "accounts.google.com/o/oauth2/auth") |
| 40 | + assert.Contains(t, authURL, "client_id="+testClientID) |
| 41 | + assert.Contains(t, authURL, "state="+testState) |
| 42 | + // redirect_uri is URL-encoded in the query string |
| 43 | + assert.Contains(t, authURL, "redirect_uri=") |
| 44 | +} |
| 45 | + |
| 46 | +func TestGetAuthURLWithRedirect(t *testing.T) { |
| 47 | + cfg := oauth2.Config{ |
| 48 | + ClientID: testClientID, |
| 49 | + ClientSecret: testClientSecret, |
| 50 | + State: testState, |
| 51 | + RedirectURL: testRedirectURL, |
| 52 | + } |
| 53 | + |
| 54 | + provider := google.NewProvider(cfg, "http://localhost/ui", "http://localhost/error") |
| 55 | + |
| 56 | + customRedirect := "http://localhost:9090/callback" |
| 57 | + authURL := provider.GetAuthURLWithRedirect(customRedirect) |
| 58 | + |
| 59 | + assert.NotEmpty(t, authURL) |
| 60 | + assert.Contains(t, authURL, "accounts.google.com/o/oauth2/auth") |
| 61 | + assert.Contains(t, authURL, "client_id="+testClientID) |
| 62 | + assert.Contains(t, authURL, "state="+testState) |
| 63 | + // redirect_uri is URL-encoded in the query string, just verify it exists |
| 64 | + assert.Contains(t, authURL, "redirect_uri=") |
| 65 | + // Verify the custom redirect is in the URL (URL-encoded) |
| 66 | + assert.Contains(t, authURL, "9090") |
| 67 | +} |
| 68 | + |
| 69 | +func TestExchangeWithRedirect(t *testing.T) { |
| 70 | + // Create a mock OAuth2 server |
| 71 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 72 | + if r.URL.Path != "/token" { |
| 73 | + http.NotFound(w, r) |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + err := r.ParseForm() |
| 78 | + require.NoError(t, err) |
| 79 | + |
| 80 | + // Verify the code |
| 81 | + code := r.FormValue("code") |
| 82 | + |
| 83 | + if code != testCode { |
| 84 | + w.WriteHeader(http.StatusBadRequest) |
| 85 | + w.Write([]byte(`{"error": "invalid_grant"}`)) |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + // Return a mock token |
| 90 | + w.Header().Set("Content-Type", "application/json") |
| 91 | + w.Write([]byte(`{ |
| 92 | + "access_token": "test-access-token", |
| 93 | + "token_type": "Bearer", |
| 94 | + "expires_in": 3600, |
| 95 | + "refresh_token": "test-refresh-token" |
| 96 | + }`)) |
| 97 | + })) |
| 98 | + defer server.Close() |
| 99 | + |
| 100 | + cfg := oauth2.Config{ |
| 101 | + ClientID: testClientID, |
| 102 | + ClientSecret: testClientSecret, |
| 103 | + State: testState, |
| 104 | + RedirectURL: testRedirectURL, |
| 105 | + } |
| 106 | + |
| 107 | + // We can't easily test the actual Google provider without modifying the endpoint |
| 108 | + // This test verifies the method exists and has the correct signature |
| 109 | + provider := google.NewProvider(cfg, "http://localhost/ui", "http://localhost/error") |
| 110 | + |
| 111 | + // Test with invalid code (will fail but ensures method works) |
| 112 | + _, err := provider.ExchangeWithRedirect(context.Background(), "invalid-code", "http://localhost:9090/callback") |
| 113 | + assert.Error(t, err) // Expected to fail with actual Google OAuth |
| 114 | +} |
| 115 | + |
| 116 | +func TestIsEnabled(t *testing.T) { |
| 117 | + cases := []struct { |
| 118 | + name string |
| 119 | + config oauth2.Config |
| 120 | + expected bool |
| 121 | + }{ |
| 122 | + { |
| 123 | + name: "enabled with all credentials", |
| 124 | + config: oauth2.Config{ |
| 125 | + ClientID: testClientID, |
| 126 | + ClientSecret: testClientSecret, |
| 127 | + State: testState, |
| 128 | + RedirectURL: testRedirectURL, |
| 129 | + }, |
| 130 | + expected: true, |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "disabled without client ID", |
| 134 | + config: oauth2.Config{ |
| 135 | + ClientID: "", |
| 136 | + ClientSecret: testClientSecret, |
| 137 | + State: testState, |
| 138 | + RedirectURL: testRedirectURL, |
| 139 | + }, |
| 140 | + expected: false, |
| 141 | + }, |
| 142 | + { |
| 143 | + name: "disabled without client secret", |
| 144 | + config: oauth2.Config{ |
| 145 | + ClientID: testClientID, |
| 146 | + ClientSecret: "", |
| 147 | + State: testState, |
| 148 | + RedirectURL: testRedirectURL, |
| 149 | + }, |
| 150 | + expected: false, |
| 151 | + }, |
| 152 | + { |
| 153 | + name: "disabled without credentials", |
| 154 | + config: oauth2.Config{ |
| 155 | + ClientID: "", |
| 156 | + ClientSecret: "", |
| 157 | + State: testState, |
| 158 | + RedirectURL: testRedirectURL, |
| 159 | + }, |
| 160 | + expected: false, |
| 161 | + }, |
| 162 | + } |
| 163 | + |
| 164 | + for _, tc := range cases { |
| 165 | + t.Run(tc.name, func(t *testing.T) { |
| 166 | + provider := google.NewProvider(tc.config, "http://localhost/ui", "http://localhost/error") |
| 167 | + assert.Equal(t, tc.expected, provider.IsEnabled()) |
| 168 | + }) |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +func TestProviderMethods(t *testing.T) { |
| 173 | + cfg := oauth2.Config{ |
| 174 | + ClientID: testClientID, |
| 175 | + ClientSecret: testClientSecret, |
| 176 | + State: testState, |
| 177 | + RedirectURL: testRedirectURL, |
| 178 | + } |
| 179 | + |
| 180 | + uiRedirectURL := "http://localhost:9095/ui/tokens/secure" |
| 181 | + errorURL := "http://localhost:9095/ui/error" |
| 182 | + |
| 183 | + provider := google.NewProvider(cfg, uiRedirectURL, errorURL) |
| 184 | + |
| 185 | + t.Run("Name", func(t *testing.T) { |
| 186 | + assert.Equal(t, "google", provider.Name()) |
| 187 | + }) |
| 188 | + |
| 189 | + t.Run("State", func(t *testing.T) { |
| 190 | + assert.Equal(t, testState, provider.State()) |
| 191 | + }) |
| 192 | + |
| 193 | + t.Run("RedirectURL", func(t *testing.T) { |
| 194 | + assert.Equal(t, uiRedirectURL, provider.RedirectURL()) |
| 195 | + }) |
| 196 | + |
| 197 | + t.Run("ErrorURL", func(t *testing.T) { |
| 198 | + assert.Equal(t, errorURL, provider.ErrorURL()) |
| 199 | + }) |
| 200 | +} |
0 commit comments