示例#1
0
文件: todo_test.go 项目: elos/elos
func newTestUser(t *testing.T, db data.DB) *oldmodels.User {
	u := oldmodels.NewUser()
	u.SetID(db.NewID())
	u.CreatedAt = time.Now()
	u.UpdatedAt = time.Now()
	if err := db.Save(u); err != nil {
		t.Fatalf("Error newTestUser: %s", err)
	}
	return u
}
示例#2
0
文件: init.go 项目: elos/elos
func (c *InitCommand) Run(args []string) int {
	if c.Config.DB == "" {
		c.Ui.Error("No database listed")
		return 1
	}
	c.Ui.Info("Connecting to db...")
	db, err := models.MongoDB(c.Config.DB)
	if err != nil {
		c.Ui.Error(err.Error())
		return 1
	}
	c.Ui.Info("Connected")
	c.Ui.Output("Welcome to Elos!")
	c.Ui.Output("We first need to create you an elos user account")

	pass, err := c.Ui.Ask("What would you like your master password to be?")
	if err != nil {
		return 1
	}

	newUser := models.NewUser()
	newUser.SetID(db.NewID())
	newUser.CreatedAt = time.Now()
	newUser.Password = pass
	newUser.UpdatedAt = time.Now()
	if err = db.Save(newUser); err != nil {
		c.Ui.Error(fmt.Sprintf("Failure to save user: %s", err))
		return 1
	}

	c.Config.UserID = newUser.ID().String()
	err = WriteConfigFile(c.Config)
	if err != nil {
		c.Ui.Error("Failed to update user id info")
		return 1
	}

	c.Ui.Info(fmt.Sprintf("User account created, your id is: %s", newUser.ID()))

	return 0
}
示例#3
0
func TestRegisterPOST(t *testing.T) {
	db, _, s := testInstance(t, context.Background())
	defer s.Close()

	username, password := "******", "private"

	params := url.Values{}
	params.Set("username", username)
	params.Set("password", password)
	url := s.URL + "/register/?" + params.Encode()
	t.Logf("Constructed URL: %s", url)

	req, err := http.NewRequest("POST", url, nil)
	if err != nil {
		t.Fatal(err)
	}

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		t.Fatal(err)
	}

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Fatal(err)
	}

	t.Logf("Code: %d", resp.StatusCode)
	t.Logf("Body:\n%s", body)

	if resp.StatusCode != http.StatusCreated {
		t.Fatalf("Expected status code of %d", http.StatusCreated)
	}

	if !strings.Contains(string(body), "id") {
		t.Fatal("Response body should have contained user id")
	}

	t.Log("Unmarshalling response body into user struct")
	u := models.NewUser()
	if err := json.Unmarshal(body, u); err != nil {
		t.Fatal(err)
	}
	t.Log("unmarshalled")

	iter, err := db.Query(models.CredentialKind).Select(data.AttrMap{"owner_id": u.Id}).Execute()
	if err != nil {
		t.Fatal(err)
	}

	c := models.NewCredential()
	iter.Next(c)
	if err := iter.Close(); err != nil {
		t.Fatal(err)
	}

	if c.Public != username {
		t.Fatal("Credential's public should be the username")
	}

	if c.Private != password {
		t.Fatal("Credential's private should be the password")
	}
}