github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/test/integration/auth_int_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/ActiveState/cli/internal/testhelpers/suite"
    10  	"github.com/ActiveState/termtest"
    11  	"github.com/google/uuid"
    12  
    13  	"github.com/ActiveState/cli/internal/testhelpers/e2e"
    14  	"github.com/ActiveState/cli/internal/testhelpers/tagsuite"
    15  )
    16  
    17  var uid uuid.UUID
    18  
    19  func init() {
    20  	// Because of our parallel test running tweak normal suite operations are limited and we have to set up shared resources
    21  	// at a higher level. We can better address this in the long run but for now this'll have to do.
    22  	var err error
    23  	uid, err = uuid.NewRandom()
    24  	if err != nil {
    25  		panic(fmt.Sprintf("Could not generate uuid, error: %v", err))
    26  	}
    27  }
    28  
    29  type AuthIntegrationTestSuite struct {
    30  	tagsuite.Suite
    31  }
    32  
    33  func (suite *AuthIntegrationTestSuite) TestAuth() {
    34  	suite.OnlyRunForTags(tagsuite.Auth, tagsuite.Critical)
    35  	ts := e2e.New(suite.T(), false)
    36  	defer ts.Close()
    37  	user := ts.CreateNewUser()
    38  	ts.LogoutUser()
    39  	suite.interactiveLogin(ts, user.Username, user.Password)
    40  	ts.LogoutUser()
    41  	suite.loginFlags(ts, user.Username)
    42  	suite.ensureLogout(ts)
    43  }
    44  
    45  func (suite *AuthIntegrationTestSuite) TestAuthToken() {
    46  	suite.OnlyRunForTags(tagsuite.Auth, tagsuite.Critical)
    47  	ts := e2e.New(suite.T(), false)
    48  	defer ts.Close()
    49  
    50  	cp := ts.Spawn(tagsuite.Auth, "--token", e2e.PersistentToken, "-n")
    51  	cp.Expect("logged in", termtest.OptExpectTimeout(40*time.Second))
    52  	cp.ExpectExitCode(0)
    53  
    54  	cp = ts.Spawn(tagsuite.Auth, "--non-interactive")
    55  	cp.Expect("logged in", termtest.OptExpectTimeout(40*time.Second))
    56  	cp.ExpectExitCode(0)
    57  
    58  	ts.LogoutUser()
    59  	suite.ensureLogout(ts)
    60  }
    61  
    62  func (suite *AuthIntegrationTestSuite) interactiveLogin(ts *e2e.Session, username, password string) {
    63  	cp := ts.Spawn(tagsuite.Auth, "--prompt")
    64  	cp.Expect("username:")
    65  	cp.SendLine(username)
    66  	cp.Expect("password:")
    67  	cp.SendLine(password)
    68  	cp.Expect("logged in")
    69  	cp.ExpectExitCode(0)
    70  
    71  	// still logged in?
    72  	c2 := ts.Spawn(tagsuite.Auth)
    73  	c2.Expect("You are logged in")
    74  	c2.ExpectExitCode(0)
    75  }
    76  
    77  func (suite *AuthIntegrationTestSuite) loginFlags(ts *e2e.Session, username string) {
    78  	cp := ts.Spawn(tagsuite.Auth, "--username", username, "--password", "bad-password")
    79  	cp.Expect("You are not authorized, did you provide valid login credentials?")
    80  	cp.ExpectExitCode(1)
    81  	ts.IgnoreLogErrors()
    82  }
    83  
    84  func (suite *AuthIntegrationTestSuite) ensureLogout(ts *e2e.Session) {
    85  	cp := ts.Spawn(tagsuite.Auth, "--prompt")
    86  	cp.Expect("username:")
    87  	cp.SendCtrlC()
    88  }
    89  
    90  type userJSON struct {
    91  	Username string `json:"username,omitempty"`
    92  }
    93  
    94  func (suite *AuthIntegrationTestSuite) authOutput(method string) {
    95  	data, err := json.Marshal(userJSON{
    96  		Username: e2e.PersistentUsername,
    97  	})
    98  	suite.Require().NoError(err)
    99  
   100  	ts := e2e.New(suite.T(), false)
   101  	defer ts.Close()
   102  
   103  	expected := string(data)
   104  	ts.LoginAsPersistentUser()
   105  	cp := ts.Spawn(tagsuite.Auth, "--output", method)
   106  	cp.Expect(`"}`)
   107  	cp.ExpectExitCode(0)
   108  	suite.Contains(cp.Output(), string(expected))
   109  }
   110  
   111  func (suite *AuthIntegrationTestSuite) TestAuth_JsonOutput() {
   112  	suite.OnlyRunForTags(tagsuite.Auth, tagsuite.JSON)
   113  	suite.authOutput("json")
   114  }
   115  
   116  func TestAuthIntegrationTestSuite(t *testing.T) {
   117  	suite.Run(t, new(AuthIntegrationTestSuite))
   118  }