github.com/deis/workflow-e2e@v2.12.2-0.20180227201524-4105be7001fe+incompatible/tests/cmd/auth/commands.go (about)

     1  package auth
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/deis/workflow-e2e/tests/cmd"
     7  	"github.com/deis/workflow-e2e/tests/model"
     8  	"github.com/deis/workflow-e2e/tests/settings"
     9  
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/gbytes"
    12  	. "github.com/onsi/gomega/gexec"
    13  )
    14  
    15  // The functions in this file implement SUCCESS CASES for commonly used `deis auth` subcommands.
    16  // This allows each of these to be re-used easily in multiple contexts.
    17  
    18  // RegisterAdmin executes `deis auth:register`, using hard-coded username, password, and email
    19  // address. When this is executed, it is executed in hopes of registering Workflow's FIRST user,
    20  // which will automatically have admin permissions. If this should fail, the function proceeds
    21  // with logging in using those same hard-coded credentials, in the hopes that the reason for the
    22  // failure is that such an account already exists, having been created by a previous execution of
    23  // the tests.
    24  func RegisterAdmin() {
    25  	admin := model.Admin
    26  	sess, err := cmd.Start("deis auth:register %s --username=%s --password=%s --email=%s", &admin, settings.DeisControllerURL, admin.Username, admin.Password, admin.Email)
    27  	Expect(err).To(BeNil())
    28  	Eventually(sess).Should(Exit())
    29  	Expect(err).NotTo(HaveOccurred())
    30  
    31  	// We cannot entirely count on the registration having succeeded. It may have failed if a user
    32  	// with the username "admin" already exists. However, if that user IS indeed an admin and their
    33  	// password is also "admin" (e.g. the admin was created by a previous run of these tests), then
    34  	// we can proceed... so attempt to login...
    35  	Login(admin)
    36  
    37  	// Now verify this user is an admin by running a privileged command.
    38  	sess, err = cmd.Start("deis users:list", &admin)
    39  	Expect(err).To(BeNil())
    40  	Eventually(sess).Should(Exit(0))
    41  	Expect(err).NotTo(HaveOccurred())
    42  }
    43  
    44  // Register executes `deis auth:register --login=false` using a randomized username and returns a model.User.
    45  // The 'login' flag is set to false during registration because otherwise the newly registered user's configuration
    46  // will be automatically written to the file used to register -- which in this case is the admin user's.
    47  // Since we don't want to overwrite the admin user's configuration, we log the new user in separately.
    48  func Register() model.User {
    49  	// Default registration mode is 'admin_only' as of v2.12.0, so only admin may register new users
    50  	admin := model.Admin
    51  	Login(admin)
    52  
    53  	user := model.NewUser()
    54  	sess, err := cmd.Start("deis auth:register %s --username=%s --password=%s --email=%s --login=false", &admin, settings.DeisControllerURL, user.Username, user.Password, user.Email)
    55  	Expect(err).To(BeNil())
    56  	Eventually(sess).Should(Exit(0))
    57  	Expect(err).NotTo(HaveOccurred())
    58  	Eventually(sess).Should(Say(fmt.Sprintf("Registered %s\n", user.Username)))
    59  
    60  	return user
    61  }
    62  
    63  // Login executes `deis auth:login` as the specified user. In the process, it creates the a
    64  // corresponding profile that contains the user's authentication token. Re-use of this profile is
    65  // for most other actions is what permits multiple test users to act in parallel without impacting
    66  // one another.
    67  func Login(user model.User) {
    68  	sess, err := cmd.Start("deis auth:login %s --username=%s --password=%s", &user, settings.DeisControllerURL, user.Username, user.Password)
    69  	Expect(err).To(BeNil())
    70  	Eventually(sess).Should(Exit(0))
    71  	Expect(err).NotTo(HaveOccurred())
    72  	Eventually(sess).Should(Say(fmt.Sprintf("Logged in as %s\n", user.Username)))
    73  }
    74  
    75  // RegisterAndLogin registers a user using a randomized username and then logs in as the registered user.
    76  func RegisterAndLogin() model.User {
    77  	user := Register()
    78  	Login(user)
    79  	return user
    80  }
    81  
    82  // Whoami executes `deis auth:whoami` as the specified user.
    83  func Whoami(user model.User) {
    84  	sess, err := cmd.Start("deis auth:whoami", &user)
    85  	Eventually(sess).Should(Say("You are %s", user.Username))
    86  	Eventually(sess).Should(Exit(0))
    87  	Expect(err).NotTo(HaveOccurred())
    88  }
    89  
    90  func WhoamiAll(user model.User) {
    91  	sess, err := cmd.Start("deis auth:whoami --all", &user)
    92  	Eventually(sess).Should(Say("ID: 0\nUsername: %s\nEmail: %s", user.Username, user.Email))
    93  	Eventually(sess).Should(Say(`First Name: `))
    94  	Eventually(sess).Should(Say(`Last Name: `))
    95  	Eventually(sess).Should(Say(`Last Login: `))
    96  	Eventually(sess).Should(Say("Is Superuser: %t\nIs Staff: %t\nIs Active: true\n", user.IsSuperuser, user.IsSuperuser))
    97  	Eventually(sess).Should(Say(`Date Joined: `))
    98  	Eventually(sess).Should(Exit(0))
    99  	Expect(err).NotTo(HaveOccurred())
   100  }
   101  
   102  // Regenerate executes `deis auth:regenerate` as the specified user.
   103  func Regenerate(user model.User) {
   104  	sess, err := cmd.Start("deis auth:regenerate", &user)
   105  	Eventually(sess).Should(Say("Token Regenerated"))
   106  	Eventually(sess).Should(Exit(0))
   107  	Expect(err).NotTo(HaveOccurred())
   108  }
   109  
   110  // Logout executes `deis auth:logout` as the specified user.
   111  func Logout(user model.User) {
   112  	sess, err := cmd.Start("deis auth:logout", &user)
   113  	Expect(err).To(BeNil())
   114  	Eventually(sess).Should(Exit(0))
   115  	Expect(err).NotTo(HaveOccurred())
   116  	Eventually(sess).Should(Say("Logged out\n"))
   117  }
   118  
   119  // Cancel executes `deis auth:cancel` as the specified user.
   120  func Cancel(user model.User) {
   121  	sess, err := cmd.Start("deis auth:cancel --username=%s --password=%s --yes", &user, user.Username, user.Password)
   122  	Expect(err).To(BeNil())
   123  	Eventually(sess).Should(Exit(0))
   124  	Expect(err).NotTo(HaveOccurred())
   125  	Eventually(sess).Should(Say("Account cancelled\n"))
   126  }
   127  
   128  // CancelAdmin deletes the admin user that was created to facilitate the tests.
   129  func CancelAdmin() {
   130  	admin := model.Admin
   131  	sess, err := cmd.Start("deis auth:cancel --username=%s --password=%s --yes", &admin, admin.Username, admin.Password)
   132  	Expect(err).To(BeNil())
   133  	Eventually(sess).Should(Exit(0))
   134  	Expect(err).NotTo(HaveOccurred())
   135  	Eventually(sess).Should(Say("Account cancelled\n"))
   136  }