From e4376b033cf0a842ce15ad84d9be65cb332bde67 Mon Sep 17 00:00:00 2001 From: yoyinzyc Date: Mon, 9 Dec 2019 16:53:20 -0800 Subject: [PATCH] e2e: add TestV3CurlAuthWithoutOption for issue #11414 --- tests/e2e/v3_curl_test.go | 62 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/tests/e2e/v3_curl_test.go b/tests/e2e/v3_curl_test.go index 74b5559f3f57..ab2b05b2efbf 100644 --- a/tests/e2e/v3_curl_test.go +++ b/tests/e2e/v3_curl_test.go @@ -74,6 +74,11 @@ func TestV3CurlAuth(t *testing.T) { testCtl(t, testV3CurlAuth, withApiPrefix(p)) } } +func TestV3CurlAuthWithoutOption(t *testing.T) { + for _, p := range apiPrefix { + testCtl(t, testV3CurlAuthWithoutOption, withApiPrefix(p)) + } +} func TestV3CurlAuthClientTLSCertAuth(t *testing.T) { for _, p := range apiPrefix { testCtl(t, testV3CurlAuth, withApiPrefix(p), withCfg(configClientTLSCertAuthWithNoCN)) @@ -274,6 +279,63 @@ func testV3CurlAuth(cx ctlCtx) { } } +func testV3CurlAuthWithoutOption(cx ctlCtx) { + p := cx.apiPrefix + + // create root user + if err := cURLPost(cx.epc, cURLReq{endpoint: path.Join(p, "/auth/user/add"), value: "{\"name\":\"root\", \"password\":\"toor\"}", expected: "revision"}); err != nil { + cx.t.Fatalf("failed testV3CurlAuth add user with curl (%v)", err) + } + + // create root role + if err := cURLPost(cx.epc, cURLReq{endpoint: path.Join(p, "/auth/role/add"), value: "{\"name\":\"root\"}", expected: "revision"}); err != nil { + cx.t.Fatalf("failed testV3CurlAuth create role with curl using prefix (%s) (%v)", p, err) + } + + // grant root role + if err := cURLPost(cx.epc, cURLReq{endpoint: path.Join(p, "/auth/user/grant"), value: "{\"user\":\"root\", \"role\":\"root\"}", expected: "revision"}); err != nil { + cx.t.Fatalf("failed testV3CurlAuth grant role with curl using prefix (%s) (%v)", p, err) + } + + // enable auth + if err := cURLPost(cx.epc, cURLReq{endpoint: path.Join(p, "/auth/enable"), value: string("{}"), expected: "revision"}); err != nil { + cx.t.Fatalf("failed testV3CurlAuth enable auth with curl using prefix (%s) (%v)", p, err) + } + + // auth request + var ( + authHeader string + cmdArgs []string + lineFunc = func(txt string) bool { return true } + ) + + cmdArgs = cURLPrefixArgs(cx.epc, "POST", cURLReq{endpoint: path.Join(p, "/auth/authenticate"), value: "{\"name\":\"root\", \"password\":\"toor\"}"}) + proc, err := spawnCmd(cmdArgs) + testutil.AssertNil(cx.t, err) + + cURLRes, err := proc.ExpectFunc(lineFunc) + testutil.AssertNil(cx.t, err) + + authRes := make(map[string]interface{}) + testutil.AssertNil(cx.t, json.Unmarshal([]byte(cURLRes), &authRes)) + + token, ok := authRes[rpctypes.TokenFieldNameGRPC].(string) + if !ok { + cx.t.Fatalf("failed invalid token in authenticate response with curl") + } + + authHeader = "Authorization: " + token + + // put "bar" into "foo" + putreq, err := json.Marshal(&pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")}) + testutil.AssertNil(cx.t, err) + + // put with auth + if err = cURLPost(cx.epc, cURLReq{endpoint: path.Join(p, "/kv/put"), value: string(putreq), header: authHeader, expected: "revision"}); err != nil { + cx.t.Fatalf("failed testV3CurlAuth auth put with curl using prefix (%s) (%v)", p, err) + } +} + func TestV3CurlCampaignNoTLS(t *testing.T) { for _, p := range apiPrefix { testCtl(t, testV3CurlCampaign, withApiPrefix(p), withCfg(configNoTLS))