github.com/spinnaker/spin@v1.30.0/cmd/account/whoami_test.go (about)

     1  // Copyright 2021, Han Byul Ko.
     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 account
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"net/http"
    21  	"net/http/httptest"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/spinnaker/spin/cmd"
    26  	"github.com/spinnaker/spin/util"
    27  )
    28  
    29  func TestAccountWhoami_basic(t *testing.T) {
    30  	ts := testAccountWhoamiSuccess()
    31  	defer ts.Close()
    32  
    33  	rootCmd, options := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard)
    34  	rootCmd.AddCommand(NewAccountCmd(options))
    35  
    36  	args := []string{"account", "whoami", "--gate-endpoint=" + ts.URL}
    37  	rootCmd.SetArgs(args)
    38  	err := rootCmd.Execute()
    39  	if err != nil {
    40  		t.Fatalf("Command failed with: %s", err)
    41  	}
    42  }
    43  
    44  func TestAccountWhoami_fail(t *testing.T) {
    45  	ts := testGateFail()
    46  	defer ts.Close()
    47  
    48  	rootCmd, options := cmd.NewCmdRoot(ioutil.Discard, ioutil.Discard)
    49  	rootCmd.AddCommand(NewAccountCmd(options))
    50  
    51  	args := []string{"account", "whoami", "--gate-endpoint=" + ts.URL}
    52  	rootCmd.SetArgs(args)
    53  	err := rootCmd.Execute()
    54  	if err == nil {
    55  		t.Fatalf("Command failed with: %s", err)
    56  	}
    57  }
    58  
    59  func testAccountWhoamiSuccess() *httptest.Server {
    60  	mux := util.TestGateMuxWithVersionHandler()
    61  	mux.Handle("/auth/user", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    62  		w.Header().Add("content-type", "application/json")
    63  		fmt.Fprintln(w, strings.TrimSpace(whoamiJson))
    64  	}))
    65  	return httptest.NewServer(mux)
    66  }
    67  
    68  const whoamiJson = `[
    69  {
    70  	"email": "pcmusic@example.com",
    71  	"username": "pcmusic@example.com",
    72  	"firstName": "A. G.",
    73  	"lastName": "Cook",
    74  	"roles": [
    75  		"public",
    76  		"admin"
    77  	],
    78  	"allowedAccounts": [
    79  		"ecr",
    80  		"spinnaker"
    81  	],
    82  	"enabled": true,
    83  	"authorities": [
    84  		{
    85  			"authority": "public"
    86  		},
    87  		{
    88  			"authority": "admin"
    89  		}
    90  	],
    91  	"accountNonExpired": true,
    92  	"credentialsNonExpired": true,
    93  	"accountNonLocked": true
    94  }
    95  ]
    96  `