// NewApiTester creates and returns a new ApiTester,
// logging in to the Traffic Ops instance specified in `test/environment` to get an auth token.
// Does not return an error - calls t.Fatal on error
func NewApiTester(t *testing.T) apitest.ApiTester {
	env, err := environment.Get(environment.DefaultPath)
	if err != nil {
		t.Fatalf("Failed to get environment: %v\n", err)
	}

	// getTrafficOpsCookie logs in to Traffic Ops and returns an auth cookie
	getTrafficOpsCookie := func(cdnUri, user, pass string) (string, error) {
		uri := cdnUri + TrafficOpsAPIPath + "user/login"
		postdata := `{"u":"` + user + `", "p":"` + pass + `"}`
		req, err := http.NewRequest("POST", uri, strings.NewReader(postdata))
		if err != nil {
			return "", err
		}
		req.Header.Add("Accept", "application/json")

		client := apitest.GetClient()
		resp, err := client.Do(req)
		if err != nil {
			return "", err
		}
		defer resp.Body.Close()

		for _, cookie := range resp.Cookies() {
			if cookie.Name == `mojolicious` {
				return cookie.Value, nil
			}
		}

		data, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			return "", err
		}
		return "", errors.New("No login cookie received: " + string(data))
	}

	token, err := getTrafficOpsCookie(env.TrafficOps.URI, env.TrafficOps.User, env.TrafficOps.Password)
	if err != nil {
		t.Fatal(err)
	}
	return &trafficOpsApiTesterData{fqdn: env.TrafficOps.URI, cookies: []*http.Cookie{&http.Cookie{Name: "mojolicious", Value: token}}}
}
Ejemplo n.º 2
0
func TestTrafficOpsLogin(t *testing.T) {
	env, err := environment.Get(environment.DefaultPath)
	if err != nil {
		t.Fatalf("Failed to get environment: %v\n", err)
	}

	caps := selenium.Capabilities{"browserName": "firefox"}
	wd, err := selenium.NewRemote(caps, "")
	if err != nil {
		t.Fatalf("Error creating selenium Remote: %v\n", err)
	}
	defer wd.Quit()

	if err := wd.Get(env.TrafficOps.URI); err != nil {
		t.Fatalf("Error getting URI: %v\n", err)
	}

	elem, err := wd.FindElement(selenium.ByID, "u")
	if err != nil {
		t.Fatalf("Error finding element: %v\n", err)
	}
	elem.Clear()
	elem.SendKeys(env.TrafficOps.User)

	elem, err = wd.FindElement(selenium.ByID, "p")
	if err != nil {
		t.Fatalf("Error Finding element: %v\n", err)
	}

	elem.Clear()
	elem.SendKeys(env.TrafficOps.Password)

	btn, _ := wd.FindElement(selenium.ByID, "login_button")
	if err != nil {
		t.Fatalf("Error Finding element: %v\n", err)
	}
	btn.Click()

	loadingDiv, err := wd.FindElement(selenium.ByID, "utcclock")
	if err != nil {
		t.Fatalf("Error finding Element: %v\n", err)
	}
	for {
		if output, err := loadingDiv.Text(); err != nil {
			t.Fatalf("Error getting output: %v\n", err)
		} else if output != "loading..." {
			break
		}
		time.Sleep(time.Millisecond * 100)
	}

	div, err := wd.FindElement(selenium.ByClassName, "dataTables_scroll")
	if err != nil {
		t.Fatalf("Error finding Element: %v\n", err)
	}

	//	txt, err := div.Text()
	_, err = div.Text()
	if err != nil {
		t.Fatalf("Error getting Text: %v\n", err)
	}
	//	fmt.Printf("Got: %s\n", txt)
}