go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/appengine/gaetesting/middleware.go (about)

     1  // Copyright 2015 The LUCI Authors.
     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 gaetesting is DEPRECATED.
    16  //
    17  // Deprecated: mock only specific dependencies your test depends on instead of
    18  // trying to mock everything at once.
    19  package gaetesting
    20  
    21  import (
    22  	"context"
    23  	"flag"
    24  
    25  	"go.chromium.org/luci/common/logging/gologger"
    26  	"go.chromium.org/luci/gae/impl/memory"
    27  	"go.chromium.org/luci/server/caching"
    28  	"go.chromium.org/luci/server/secrets"
    29  	"go.chromium.org/luci/server/secrets/testsecrets"
    30  )
    31  
    32  var goLogger = flag.Bool("test.gologger", false, "Enable console logging during test.")
    33  
    34  // TestingContext returns context with base services installed:
    35  //   - go.chromium.org/luci/gae/impl/memory (in-memory appengine services)
    36  //   - go.chromium.org/luci/server/caching (access to process cache)
    37  //   - go.chromium.org/luci/server/secrets/testsecrets (access to fake secret keys)
    38  func TestingContext() context.Context {
    39  	ctx := context.Background()
    40  	ctx = memory.Use(ctx)
    41  	return commonTestingContext(ctx)
    42  }
    43  
    44  // TestingContextWithAppID returns context with the specified App ID and base
    45  // services installed:
    46  //   - go.chromium.org/luci/gae/impl/memory (in-memory appengine services)
    47  //   - go.chromium.org/luci/server/caching (access to process cache)
    48  //   - go.chromium.org/luci/server/secrets/testsecrets (access to fake secret keys)
    49  func TestingContextWithAppID(appID string) context.Context {
    50  	ctx := context.Background()
    51  	ctx = memory.UseWithAppID(ctx, appID)
    52  	return commonTestingContext(ctx)
    53  }
    54  
    55  func commonTestingContext(ctx context.Context) context.Context {
    56  	ctx = secrets.Use(ctx, &testsecrets.Store{})
    57  	ctx = caching.WithEmptyProcessCache(ctx)
    58  
    59  	if *goLogger {
    60  		ctx = gologger.StdConfig.Use(ctx)
    61  	}
    62  
    63  	return ctx
    64  }