// Copyright 2017 Google LLC.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package internal

import (
	"context"
	"os"
	"testing"

	"github.com/google/go-cmp/cmp"
	"golang.org/x/oauth2"
	"golang.org/x/oauth2/google"
)

type dummyTokenSource struct {
	oauth2.TokenSource
}

func TestTokenSource(t *testing.T) {
	ctx := context.Background()

	// Pass in a TokenSource, get it back.
	ts := &dummyTokenSource{}
	ds := &DialSettings{TokenSource: ts}
	got, err := Creds(ctx, ds)
	if err != nil {
		t.Fatal(err)
	}
	want := &google.DefaultCredentials{TokenSource: ts}
	if !cmp.Equal(got, want) {
		t.Error("did not get the same TokenSource back")
	}

	// If both a file and TokenSource are passed, the file takes precedence
	// (existing behavior).
	// TODO(jba): make this an error?
	ds = &DialSettings{
		TokenSource:     ts,
		CredentialsFile: "testdata/service-account.json",
		DefaultScopes:   []string{"foo"},
	}
	got, err = Creds(ctx, ds)
	if err != nil {
		t.Fatal(err)
	}
	if cmp.Equal(got, want) {
		t.Error("got the same TokenSource back, wanted one from the JSON file")
	}
	// TODO(jba): find a way to test the call to google.DefaultTokenSource.
}

func TestDefaultServiceAccount(t *testing.T) {
	ctx := context.Background()

	// Load a valid JSON file. No way to really test the contents; we just
	// verify that there is no error.
	ds := &DialSettings{
		CredentialsFile: "testdata/service-account.json",
		DefaultScopes:   []string{"foo"},
	}
	if _, err := Creds(ctx, ds); err != nil {
		t.Errorf("got %v, wanted no error", err)
	}

	// Load valid JSON. No way to really test the contents; we just
	// verify that there is no error.
	ds = &DialSettings{
		CredentialsJSON: []byte(validServiceAccountJSON),
		DefaultScopes:   []string{"foo"},
	}
	if _, err := Creds(ctx, ds); err != nil {
		t.Errorf("got %v, wanted no error", err)
	}
}

func TestJWTWithAudience(t *testing.T) {
	ctx := context.Background()

	// Load a valid JSON file. No way to really test the contents; we just
	// verify that there is no error.
	ds := &DialSettings{CredentialsFile: "testdata/service-account.json", Audiences: []string{"foo"}}
	if _, err := Creds(ctx, ds); err != nil {
		t.Errorf("got %v, wanted no error", err)
	}

	// Load valid JSON. No way to really test the contents; we just
	// verify that there is no error.
	ds = &DialSettings{CredentialsJSON: []byte(validServiceAccountJSON), Audiences: []string{"foo"}}
	if _, err := Creds(ctx, ds); err != nil {
		t.Errorf("got %v, wanted no error", err)
	}
}

func TestJWTWithScope(t *testing.T) {
	ctx := context.Background()

	// Load a valid JSON file. No way to really test the contents; we just
	// verify that there is no error.
	ds := &DialSettings{
		CredentialsFile:    "testdata/service-account.json",
		Scopes:             []string{"foo"},
		EnableJwtWithScope: true,
	}
	if _, err := Creds(ctx, ds); err != nil {
		t.Errorf("got %v, wanted no error", err)
	}

	// Load valid JSON. No way to really test the contents; we just
	// verify that there is no error.
	ds = &DialSettings{
		CredentialsJSON:    []byte(validServiceAccountJSON),
		Scopes:             []string{"foo"},
		EnableJwtWithScope: true,
	}
	if _, err := Creds(ctx, ds); err != nil {
		t.Errorf("got %v, wanted no error", err)
	}
}

func TestJWTWithDefaultScopes(t *testing.T) {
	ctx := context.Background()

	// Load a valid JSON file. No way to really test the contents; we just
	// verify that there is no error.
	ds := &DialSettings{
		CredentialsFile:    "testdata/service-account.json",
		DefaultScopes:      []string{"foo"},
		EnableJwtWithScope: true,
	}
	if _, err := Creds(ctx, ds); err != nil {
		t.Errorf("got %v, wanted no error", err)
	}

	// Load valid JSON. No way to really test the contents; we just
	// verify that there is no error.
	ds = &DialSettings{
		CredentialsJSON:    []byte(validServiceAccountJSON),
		DefaultScopes:      []string{"foo"},
		EnableJwtWithScope: true,
	}
	if _, err := Creds(ctx, ds); err != nil {
		t.Errorf("got %v, wanted no error", err)
	}
}

func TestJWTWithDefaultAudience(t *testing.T) {
	ctx := context.Background()

	// Load a valid JSON file. No way to really test the contents; we just
	// verify that there is no error.
	ds := &DialSettings{
		CredentialsFile: "testdata/service-account.json",
		DefaultAudience: "foo",
	}
	if _, err := Creds(ctx, ds); err != nil {
		t.Errorf("got %v, wanted no error", err)
	}

	// Load valid JSON. No way to really test the contents; we just
	// verify that there is no error.
	ds = &DialSettings{
		CredentialsJSON: []byte(validServiceAccountJSON),
		DefaultAudience: "foo",
	}
	if _, err := Creds(ctx, ds); err != nil {
		t.Errorf("got %v, wanted no error", err)
	}
}

func TestOAuth(t *testing.T) {
	ctx := context.Background()

	// Load a valid JSON file. No way to really test the contents; we just
	// verify that there is no error.
	ds := &DialSettings{CredentialsFile: "testdata/service-account.json", Scopes: []string{"foo"}}
	if _, err := Creds(ctx, ds); err != nil {
		t.Errorf("got %v, wanted no error", err)
	}

	// Load valid JSON. No way to really test the contents; we just
	// verify that there is no error.
	ds = &DialSettings{CredentialsJSON: []byte(validServiceAccountJSON), Scopes: []string{"foo"}}
	if _, err := Creds(ctx, ds); err != nil {
		t.Errorf("got %v, wanted no error", err)
	}
}

const validServiceAccountJSON = `{
  "type": "service_account",
  "project_id": "dumba-504",
  "private_key_id": "adsfsdd",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDczcmZh9zhcDjd\nc+a1bcNu06QA+PGwjAZygTy9ays8qTLi4J8zWSjxgb18ZgYnv1gwVenmSBRuj+kg\nG03NPOxGmPrC/fTnZVBZpQRwBccBrPnRMvEM94egVrHKvPgqbifkyn2VR+ouWZvv\nwigR2PbjcvrSNkPE0QpLnRV0stilgCIYXR17lFrOPoiMra3N//1J0IPIFl3qZHxB\nsiejdi7zUiqLkqjYrNRHKulAGcJjqdCsNjAdjxgbRHgTjXSyuJh8bdKBgAMhetKj\nvU2OM431a9MQq77q/kvuJuCDRe6WqHs7JEFICUymTSSauANXowTUz63jfoSMMwmL\nBNcoePAZAgMBAAECggEAFxpkJe+YjbERjiBOqzybJok3/48MsOGR2iDKI3KncT8t\n7x28IqIJXe2qjy3YpoeHmXUf1mPD2YauyJh6xUcC3OcsU8NBQQXxiJOy2WrNVhBM\nilZXcPkkZIM1sqYfnEbu3ypNxhUifGuNXKKW0Tk/qfBRrLWXVSx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j3\nL54zu6Y9GbjLgn6BtfhLHG4v\n-----END PRIVATE KEY-----\n",
  "client_email": "dumba-504@appspot.gserviceaccount.com",
  "client_id": "111",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/dumba-504%40appspot.gserviceaccount.com"
}`

func TestGetQuotaProject(t *testing.T) {
	ctx := context.Background()
	emptyCred, err := credentialsFromJSON(
		ctx,
		[]byte(validServiceAccountJSON),
		&DialSettings{
			Endpoint:      "foo.googleapis.com",
			DefaultScopes: []string{"foo"},
		})
	if err != nil {
		t.Fatalf("got %v, wanted no error", err)
	}
	quotaProjectJSON := []byte(`
{
	"type": "authorized_user",
	"quota_project_id": "foobar"
}`)

	quotaCred, err := credentialsFromJSON(
		ctx,
		[]byte(quotaProjectJSON),
		&DialSettings{
			Endpoint:      "foo.googleapis.com",
			DefaultScopes: []string{"foo"},
		})
	if err != nil {
		t.Fatalf("got %v, wanted no error", err)
	}

	tests := []struct {
		name      string
		cred      *google.Credentials
		clientOpt string
		env       string
		want      string
	}{
		{
			name: "empty all",
			cred: nil,
			want: "",
		},
		{
			name: "empty cred",
			cred: emptyCred,
			want: "",
		},
		{
			name: "from cred",
			cred: quotaCred,
			want: "foobar",
		},
		{
			name:      "from opt",
			cred:      quotaCred,
			clientOpt: "clientopt",
			want:      "clientopt",
		},
		{
			name: "from env",
			cred: quotaCred,
			env:  "envProject",
			want: "envProject",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			oldEnv := os.Getenv(quotaProjectEnvVar)
			if tt.env != "" {
				os.Setenv(quotaProjectEnvVar, tt.env)
			}
			if want, got := tt.want, GetQuotaProject(tt.cred, tt.clientOpt); want != got {
				t.Errorf("GetQuotaProject(%v, %q): want %q, got %q", tt.cred, tt.clientOpt, want, got)
			}
			os.Setenv(quotaProjectEnvVar, oldEnv)
		})
	}
}

func TestCredsWithCredentials(t *testing.T) {
	tests := []struct {
		name string
		ds   *DialSettings
		want string
	}{
		{
			name: "only normal opt",
			ds: &DialSettings{
				Credentials: &google.Credentials{
					TokenSource: oauth2.StaticTokenSource(&oauth2.Token{
						AccessToken: "normal",
					}),
				},
			},
			want: "normal",
		},
		{
			name: "normal and internal creds opt",
			ds: &DialSettings{
				Credentials: &google.Credentials{
					TokenSource: oauth2.StaticTokenSource(&oauth2.Token{
						AccessToken: "normal",
					}),
				},
				InternalCredentials: &google.Credentials{
					TokenSource: oauth2.StaticTokenSource(&oauth2.Token{
						AccessToken: "internal",
					}),
				},
			},
			want: "internal",
		},
		{
			name: "only internal creds opt",
			ds: &DialSettings{
				InternalCredentials: &google.Credentials{
					TokenSource: oauth2.StaticTokenSource(&oauth2.Token{
						AccessToken: "internal",
					}),
				},
			},
			want: "internal",
		},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			creds, err := Creds(context.Background(), tc.ds)
			if err != nil {
				t.Fatalf("got %v, want nil error", err)
			}
			if tok, _ := creds.TokenSource.Token(); tok.AccessToken != tc.want {
				t.Fatalf("tok.AccessToken = %q, want %q", tok.AccessToken, tc.want)
			}
		})
	}
}
