Exemple #1
0
func TestUserLoginSessionCreation(t *testing.T) {
	db := modeltesthelper.NewMongoDB(t)
	defer db.Close()

	acc2 := createTestAccount(t)
	defer modelhelper.RemoveAccount(acc2.Id)

	group, err := createGroup()
	if err != nil {
		t.Error(err)
	}

	if err := modelhelper.AddRelationship(&models.Relationship{
		Id:         bson.NewObjectId(),
		TargetId:   acc2.Id,
		TargetName: "JAccount",
		SourceId:   group.Id,
		SourceName: "JGroup",
		As:         "member",
	}); err != nil {
		t.Error(err)
	}

	ses, err := modelhelper.UserLogin(acc2.Profile.Nickname, group.Slug)
	if err != nil {
		t.Errorf("expected nil error, but got %q!", err)
	}

	if ses == nil || ses.ClientId == "" {
		t.Error("expected ses.ClientId to be set, but got empty!")
	}
}
Exemple #2
0
func (k *Kloud) authLogin(username string, req *LoginRequest) (*LoginResponse, error) {
	ses, err := modelhelper.UserLogin(username, req.GroupName)
	switch err {
	case nil:
	case mgo.ErrNotFound:
		return nil, NewError(ErrBadRequest)
	case modelhelper.ErrNotParticipant:
		return nil, NewError(ErrNotAuthorized)
	default:
		k.Log.Debug("Got generic error for UserLogin, username: %q, err: %q", username, err)
		return nil, NewError(ErrInternalServer)
	}

	if err := k.PresenceClient.Ping(username, req.GroupName); err != nil {
		// we dont need to block user login if there is something wrong with socialapi.
		k.Log.Error("ping for user %q failed: %s", username, err)
	}

	resp := &LoginResponse{
		ClientID:  ses.ClientId,
		GroupName: req.GroupName,
		Username:  username,
	}

	if req.Metadata {
		resp.Metadata = &Metadata{
			Endpoints: k.Endpoints,
		}
	}

	return resp, nil
}
Exemple #3
0
func TestUserLogin(t *testing.T) {
	db := modeltesthelper.NewMongoDB(t)
	defer db.Close()

	acc1 := createTestAccount(t)
	defer modelhelper.RemoveAccount(acc1.Id)

	acc2 := createTestAccount(t)
	defer modelhelper.RemoveAccount(acc2.Id)

	group, err := createGroup()
	if err != nil {
		t.Error(err)
	}

	if err := modelhelper.AddRelationship(&models.Relationship{
		Id:         bson.NewObjectId(),
		TargetId:   acc1.Id,
		TargetName: "JAccount",
		SourceId:   group.Id,
		SourceName: "JGroup",
		As:         "member",
	}); err != nil {
		t.Error(err)
	}

	ses, err := modelhelper.CreateSessionForAccount(acc1.Profile.Nickname, group.Slug)
	if err != nil {
		t.Error(err)
	}

	tests := []struct {
		Title    string
		Nick     string
		Slug     string
		ClientID string
		Err      error
	}{
		{
			Title:    "Member account",
			Nick:     acc1.Profile.Nickname,
			Slug:     group.Slug,
			ClientID: ses.ClientId,
			Err:      nil,
		},
		{
			Title:    "Re-testing with Member account",
			Nick:     acc1.Profile.Nickname,
			Slug:     group.Slug,
			ClientID: ses.ClientId,
			Err:      nil,
		},
		{
			Title:    "Non-member account",
			Nick:     acc2.Profile.Nickname,
			Slug:     group.Slug,
			ClientID: "",
			Err:      modelhelper.ErrNotParticipant,
		},
	}

	for _, test := range tests {
		t.Run(test.Title, func(t *testing.T) {
			ses, err := modelhelper.UserLogin(test.Nick, test.Slug)
			if err != test.Err {
				t.Errorf("expected Err equal to %q, but got %q!", test.Err, err)
			}

			if ses != nil && ses.ClientId != test.ClientID {
				t.Errorf("expected ClientID equal to %q, but got %q!", test.ClientID, ses.ClientId)
			}
		})
	}
}