<commit_before>// Copyright 2020 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package secrets

import (
	"context"
	"fmt"
	"testing"

	"github.com/googleapis/gax-go"
	secretmanagerpb "google.golang.org/genproto/googleapis/cloud/secretmanager/v1beta1"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"

	. "github.com/smartystreets/goconvey/convey"
	. "go.chromium.org/luci/common/testing/assertions"
)

func TestSecretManagerSource(t *testing.T) {
	t.Parallel()

	ctx := context.Background()

	Convey("Constructor", t, func() {
		Convey("Works", func() {
			s, err := NewSecretManagerSource(ctx, "sm://project/secret", nil)
			So(err, ShouldBeNil)
			So(s.project, ShouldEqual, "project")
			So(s.secret, ShouldEqual, "secret")
		})

		Convey("Not sm://", func() {
			_, err := NewSecretManagerSource(ctx, "zz://project/secret", nil)
			So(err, ShouldErrLike, "not a sm://... URL")
		})

		Convey("Wrong sm:// format", func() {
			_, err := NewSecretManagerSource(ctx, "sm://project", nil)
			So(err, ShouldErrLike, "should have form")
			_, err = NewSecretManagerSource(ctx, "sm://project/secret/zzz", nil)
			So(err, ShouldErrLike, "should have form")
		})
	})

	Convey("ReadSecret", t, func() {
		client := mockedSMClient{
			project:  "project",
			secret:   "secret",
			versions: map[string]*mockedVersion{},
		}

		src := &SecretManagerSource{
			client:    &client,
			secretURL: "sm://project/secret",
			project:   "project",
			secret:    "secret",
		}

		// Reading the latest version and one previous one.
		client.setVersion(1, []byte{1, 2, 3})
		client.setVersion(2, []byte{4, 5, 6})
		secret, err := src.ReadSecret(ctx)
		So(err, ShouldBeNil)
		So(secret, ShouldResemble, &Secret{
			Current: []byte{4, 5, 6},
			Previous: [][]byte{
				{1, 2, 3},
			},<commit_msg>[secrets] Pass fake TokenSource in tests instead of nil.<commit_after>// Copyright 2020 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package secrets

import (
	"context"
	"fmt"
	"testing"

	"github.com/googleapis/gax-go"
	"golang.org/x/oauth2"
	secretmanagerpb "google.golang.org/genproto/googleapis/cloud/secretmanager/v1beta1"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"

	. "github.com/smartystreets/goconvey/convey"
	. "go.chromium.org/luci/common/testing/assertions"
)

func TestSecretManagerSource(t *testing.T) {
	t.Parallel()

	ctx := context.Background()
	fakeTS := oauth2.StaticTokenSource(&oauth2.Token{})

	Convey("Constructor", t, func() {
		Convey("Works", func() {
			s, err := NewSecretManagerSource(ctx, "sm://project/secret", fakeTS)
			So(err, ShouldBeNil)
			So(s.project, ShouldEqual, "project")
			So(s.secret, ShouldEqual, "secret")
		})

		Convey("Not sm://", func() {
			_, err := NewSecretManagerSource(ctx, "zz://project/secret", fakeTS)
			So(err, ShouldErrLike, "not a sm://... URL")
		})

		Convey("Wrong sm:// format", func() {
			_, err := NewSecretManagerSource(ctx, "sm://project", fakeTS)
			So(err, ShouldErrLike, "should have form")
			_, err = NewSecretManagerSource(ctx, "sm://project/secret/zzz", fakeTS)
			So(err, ShouldErrLike, "should have form")
		})
	})

	Convey("ReadSecret", t, func() {
		client := mockedSMClient{
			project:  "project",
			secret:   "secret",
			versions: map[string]*mockedVersion{},
		}

		src := &SecretManagerSource{
			client:    &client,
			secretURL: "sm://project/secret",
			project:   "project",
			secret:    "secret",
		}

		// Reading the latest version and one previous one.
		client.setVersion(1, []byte{1, 2, 3})
		client.setVersion(2, []byte{4, 5, 6})
		secret, err := src.ReadSecret(ctx)
		So(err, ShouldBeNil)
		So(secret, ShouldResemble, &Secret{
			Current: []byte{4, 5, 6},
			Previous: [][]byte{
				{1, 2, 3},
			},
{'fraction_non_alphanumeric': 0.1547295886406348, 'fraction_numerical': 0.010649404886197537, 'mean_word_length': 3.756703078450844, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4302493', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1573, 'n_words': 431}