示例#1
0
func TestLogout(t *testing.T) {
	netrcFile := bytes.NewBufferString(`
	machine github.com
	  login [email protected]
	  password secretpw
	machine 127.0.0.1
	  login [email protected]
	  password abcxyz123
	`)

	config.APIEndpoint = "http://127.0.0.1/api"
	loadNetrc = func() *netrc.Netrc {
		nrc, _ := netrc.Parse(netrcFile)
		return nrc
	}
	writeNetrcFile = func(body []byte) error {
		_, err := netrcFile.Write(body)
		return err
	}
	err := Logout()
	if err != nil {
		t.Error(err)
	}
	expectedNetrcFile := `
	machine github.com
	  login [email protected]
	  password secretpw
	`
	if netrcFile.String() != expectedNetrcFile {
		t.Error("Expected netrcFile to contain github login")
	}
}
示例#2
0
func (l *login) updatedNetrcContent(
	e *env,
	content io.Reader,
	credentials *credentials,
) ([]byte, error) {
	tokens, err := netrc.Parse(content)
	if err != nil {
		return nil, stackerr.Wrap(err)
	}

	server, err := getHostFromURL(e.Server)
	if err != nil {
		return nil, err
	}
	machine := tokens.FindMachine(server)
	if machine == nil {
		machine = tokens.NewMachine(server, "default", credentials.token, "")
	} else {
		machine.UpdatePassword(credentials.token)
	}

	updatedContent, err := tokens.MarshalText()
	if err != nil {
		return nil, stackerr.Wrap(err)
	}
	return updatedContent, nil
}
示例#3
0
func (l *Login) GetTokenCredentials(e *Env, email string) (bool, *Credentials, error) {
	reader, err := l.getTokensReader()
	if err != nil {
		return false, nil, stackerr.Wrap(err)
	}
	tokens, err := netrc.Parse(reader)
	if err != nil {
		return false, nil, stackerr.Wrap(err)
	}
	server, err := getHostFromURL(e.Server, email)
	if err != nil {
		return false, nil, err
	}
	machine := tokens.FindMachine(server)
	if machine != nil {
		return true,
			&Credentials{
				Token: machine.Password,
			}, nil
	}

	if email == "" {
		return false, nil, stackerr.Newf("Could not find account key for %q", server)
	}

	// check for system default account key for the given server
	// since we could not find account key for the given account (email)
	server, err = getHostFromURL(e.Server, "")
	if err != nil {
		return false, nil, err
	}
	machine = tokens.FindMachine(server)
	if machine != nil {
		return false,
			&Credentials{
				Token: machine.Password,
			}, nil
	}
	return false,
		nil,
		stackerr.Newf(
			`Could not find account key for email: %q,
and default access key not configured for %q
`,
			email,
			e.Server,
		)
}
示例#4
0
func (l *login) getTokenCredentials(e *env) (*credentials, error) {
	reader, err := l.getTokensReader()
	if err != nil {
		return nil, stackerr.Wrap(err)
	}
	tokens, err := netrc.Parse(reader)
	if err != nil {
		return nil, stackerr.Wrap(err)
	}
	server, err := getHostFromURL(e.Server)
	if err != nil {
		return nil, err
	}
	machine := tokens.FindMachine(server)
	if machine == nil {
		return nil, stackerr.Newf("could not find token for %s", server)
	}
	return &credentials{
		token: machine.Password,
	}, nil
}
示例#5
0
func TestLogin(t *testing.T) {
	// Fake gemnasium api
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

		// Check URI
		if r.RequestURI != LOGIN_PATH {
			t.Errorf("Expected RequestURI to be %s, got: %s", LOGIN_PATH, r.RequestURI)
		}

		// Check request is a POST
		if r.Method != "POST" {
			t.Errorf("Expected a POST, got a %s", r.Method)
		}

		var credentials struct {
			Email, Password string
		}
		err := json.NewDecoder(r.Body).Decode(&credentials)
		if err != nil {
			t.Error(err)
		}

		w.Header().Set("Content-Type", "application/json")
		if credentials.Email != "" && credentials.Password != "" {
			w.Write([]byte(`{"api_token": "abcxyz123"}`))
		} else {
			w.WriteHeader(http.StatusBadRequest)
		}
	}))
	defer ts.Close()

	config.APIEndpoint = ts.URL
	// don't try to use stdin
	getCredentials = func() (email, password string, err error) {
		return "*****@*****.**", "secret123", nil
	}

	netrcFile := bytes.NewBufferString("")

	loadNetrc = func() *netrc.Netrc {
		nrc, _ := netrc.Parse(netrcFile)
		return nrc

	}
	// don't actually write to the file
	writeNetrcFile = func(body []byte) error {
		_, err := netrcFile.Write(body)
		return err
	}
	err := Login()
	if err != nil {
		t.Error(err)
	}

	expectedNetrcFile :=
		`machine 127.0.0.1
	login [email protected]
	password abcxyz123`
	if netrcFile.String() != expectedNetrcFile {
		t.Errorf("Expected netrcFile to be:\n%#v\ngot:\n%#v\n", expectedNetrcFile, netrcFile.String())
	}
}