github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/configs/api/helpers_test.go (about)

     1  package api
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io"
     8  	"net/http"
     9  	"net/http/httptest"
    10  	"testing"
    11  
    12  	"github.com/cortexproject/cortex/pkg/configs/userconfig"
    13  
    14  	"github.com/stretchr/testify/require"
    15  	"github.com/weaveworks/common/user"
    16  
    17  	"github.com/cortexproject/cortex/pkg/configs/db"
    18  	"github.com/cortexproject/cortex/pkg/configs/db/dbtest"
    19  )
    20  
    21  var (
    22  	app      *API
    23  	database db.DB
    24  	counter  int
    25  )
    26  
    27  // setup sets up the environment for the tests.
    28  func setup(t *testing.T) {
    29  	database = dbtest.Setup(t)
    30  	app = New(database, Config{
    31  		Notifications: NotificationsConfig{
    32  			DisableEmail: true,
    33  		},
    34  	})
    35  	counter = 0
    36  }
    37  
    38  // setup sets up the environment for the tests with email enabled.
    39  func setupWithEmailEnabled(t *testing.T) {
    40  	database = dbtest.Setup(t)
    41  	app = New(database, Config{
    42  		Notifications: NotificationsConfig{
    43  			DisableEmail: false,
    44  		},
    45  	})
    46  	counter = 0
    47  }
    48  
    49  // cleanup cleans up the environment after a test.
    50  func cleanup(t *testing.T) {
    51  	dbtest.Cleanup(t, database)
    52  }
    53  
    54  // request makes a request to the configs API.
    55  func request(t *testing.T, method, urlStr string, body io.Reader) *httptest.ResponseRecorder {
    56  	w := httptest.NewRecorder()
    57  	r, err := http.NewRequest(method, urlStr, body)
    58  	require.NoError(t, err)
    59  	app.ServeHTTP(w, r)
    60  	return w
    61  }
    62  
    63  // requestAsUser makes a request to the configs API as the given user.
    64  func requestAsUser(t *testing.T, userID string, method, urlStr string, contentType string, body io.Reader) *httptest.ResponseRecorder {
    65  	w := httptest.NewRecorder()
    66  	r, err := http.NewRequest(method, urlStr, body)
    67  	require.NoError(t, err)
    68  	r = r.WithContext(user.InjectOrgID(r.Context(), userID))
    69  	err = user.InjectOrgIDIntoHTTPRequest(r.Context(), r)
    70  	require.NoError(t, err)
    71  	if contentType != "" {
    72  		r.Header.Set("Content-Type", contentType)
    73  	}
    74  	app.ServeHTTP(w, r)
    75  	return w
    76  }
    77  
    78  // makeString makes a string, guaranteed to be unique within a test.
    79  func makeString(pattern string) string {
    80  	counter++
    81  	return fmt.Sprintf(pattern, counter)
    82  }
    83  
    84  // makeUserID makes an arbitrary user ID. Guaranteed to be unique within a test.
    85  func makeUserID() string {
    86  	return makeString("user%d")
    87  }
    88  
    89  // makeConfig makes some arbitrary configuration.
    90  func makeConfig() userconfig.Config {
    91  	return userconfig.Config{
    92  		AlertmanagerConfig: makeString(`
    93              # Config no. %d.
    94              route:
    95                receiver: noop
    96  
    97              receivers:
    98              - name: noop`),
    99  		RulesConfig: userconfig.RulesConfig{},
   100  	}
   101  }
   102  
   103  func readerFromConfig(t *testing.T, config userconfig.Config) io.Reader {
   104  	b, err := json.Marshal(config)
   105  	require.NoError(t, err)
   106  	return bytes.NewReader(b)
   107  }
   108  
   109  // parseView parses a userconfig.View from JSON.
   110  func parseView(t *testing.T, b []byte) userconfig.View {
   111  	var x userconfig.View
   112  	err := json.Unmarshal(b, &x)
   113  	require.NoError(t, err, "Could not unmarshal JSON: %v", string(b))
   114  	return x
   115  }