github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/topgun/core/atc_login_session_test.go (about)

     1  package topgun_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  
     7  	"github.com/pf-qiu/concourse/v6/atc"
     8  	"golang.org/x/oauth2"
     9  
    10  	_ "github.com/lib/pq"
    11  
    12  	. "github.com/pf-qiu/concourse/v6/topgun"
    13  	. "github.com/pf-qiu/concourse/v6/topgun/common"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("Multiple ATCs Login Session Test", func() {
    19  	Context("with two atcs available", func() {
    20  		var atcs []BoshInstance
    21  		var atc0URL string
    22  		var atc1URL string
    23  
    24  		BeforeEach(func() {
    25  			Deploy(
    26  				"deployments/concourse.yml",
    27  				"-o", "operations/web-instances.yml",
    28  				"-v", "web_instances=2",
    29  			)
    30  
    31  			atcs = JobInstances("web")
    32  			atc0URL = "http://" + atcs[0].IP + ":8080"
    33  			atc1URL = "http://" + atcs[1].IP + ":8080"
    34  		})
    35  
    36  		Context("make api request to a different atc by a token from a stopped atc", func() {
    37  			var token *oauth2.Token
    38  
    39  			BeforeEach(func() {
    40  				var err error
    41  				token, err = FetchToken(atc0URL, AtcUsername, AtcPassword)
    42  				Expect(err).ToNot(HaveOccurred())
    43  
    44  				By("stopping the first atc")
    45  				Bosh("stop", atcs[0].Name)
    46  			})
    47  
    48  			AfterEach(func() {
    49  				Bosh("start", atcs[0].Name)
    50  			})
    51  
    52  			It("request successfully", func() {
    53  				client := &http.Client{}
    54  				reqHeader := http.Header{}
    55  				reqHeader.Set("Authorization", "Bearer "+token.AccessToken)
    56  
    57  				By("make request with the token to second atc")
    58  				request, err := http.NewRequest("GET", atc1URL+"/api/v1/workers", nil)
    59  				request.Header = reqHeader
    60  				Expect(err).NotTo(HaveOccurred())
    61  
    62  				response, err := client.Do(request)
    63  				Expect(err).NotTo(HaveOccurred())
    64  
    65  				var workers []atc.Worker
    66  				err = json.NewDecoder(response.Body).Decode(&workers)
    67  				Expect(err).NotTo(HaveOccurred())
    68  			})
    69  		})
    70  
    71  		Context("when two atcs have the same external url (dex redirect uri is the same)", func() {
    72  			deploy := func() {
    73  				Deploy(
    74  					"deployments/concourse.yml",
    75  					"-o", "operations/web-instances.yml",
    76  					"-v", "web_instances=2",
    77  					"-o", "operations/external-url.yml",
    78  					"-v", "external_url="+atc0URL,
    79  				)
    80  			}
    81  
    82  			BeforeEach(deploy)
    83  
    84  			It("should be able to login to both ATCs", func() {
    85  				Fly.Login(AtcUsername, AtcPassword, atc1URL)
    86  				Fly.Login(AtcUsername, AtcPassword, atc0URL)
    87  
    88  				By("deploying a second time (with a different token signing key)")
    89  				deploy()
    90  
    91  				Fly.Login(AtcUsername, AtcPassword, atc0URL)
    92  				Fly.Login(AtcUsername, AtcPassword, atc1URL)
    93  			})
    94  		})
    95  	})
    96  })