func TestCreditCardAuthRetryFail(t *testing.T) { Convey("When a non subscribed user request for Auth", t, func() { withTestServer(t, func(endpoint string) { withTestCreditCardToken(func(token string) { testUsername := "******" testGroupName := "guests" ses, err := modelhelper.CreateSessionForAccount(testUsername, testGroupName) tests.ResultedWithNoErrorCheck(ses, err) cp := &stripe.ChargeParams{ Source: &stripe.SourceParams{ Token: token, }, Email: "*****@*****.**", } req, err := json.Marshal(cp) tests.ResultedWithNoErrorCheck(req, err) res, err := rest.DoRequestWithAuth("POST", endpoint+EndpointCreditCardAuth, req, ses.ClientId) So(err, ShouldBeNil) res, err = rest.DoRequestWithAuth("POST", endpoint+EndpointCreditCardAuth, req, ses.ClientId) So(err, ShouldNotBeNil) So(res, ShouldBeNil) }) }) }) }
func TestCreditCardDeleteNonAdmin(t *testing.T) { Convey("When a non admin user request comes", t, func() { withTestServer(t, func(endpoint string) { acc, _, groupName := models.CreateRandomGroupDataWithChecks() ses, err := modelhelper.CreateSessionForAccount(acc.Nick, groupName) tests.ResultedWithNoErrorCheck(ses, err) Convey("Endpoint should return error", func() { ccdeleteURL := endpoint + EndpointCreditCardDelete _, err := rest.DoRequestWithAuth("DELETE", ccdeleteURL, nil, ses.ClientId) So(err, ShouldNotBeNil) }) }) }) }
func TestSessionUpdateData(t *testing.T) { db := modeltesthelper.NewMongoDB(t) defer db.Close() testUsername := "******" testGroupName := "testgroupname" ses, err := modelhelper.CreateSessionForAccount(testUsername, testGroupName) if err != nil { t.Error(err) } nonExistingKey := "nonExistingKey" val, err := ses.Data.GetString(nonExistingKey) if err != models.ErrDataKeyNotExists { t.Error("expected ErrDataKeyNotExists, got", err) } if val != "" { t.Error("expected empty string, got", val) } key := "chargeID" value := "chargeVal" data := map[string]interface{}{ key: value, } if err := modelhelper.UpdateSessionData(ses.ClientId, data); err != nil { t.Error(err) } ses, err = modelhelper.GetSessionById(ses.Id.Hex()) if err != nil { t.Error(err) } val, err = ses.Data.GetString(key) if err != nil { t.Error("expected nil, got", err) } if val != value { t.Error("expected", value, "got", val) } }
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) } }) } }
func TestChannelUpdatedCalculateUnreadItemCount(t *testing.T) { r := runner.New("test") if err := r.Init(); err != nil { t.Fatalf("couldnt start bongo %s", err.Error()) } defer r.Close() appConfig := config.MustRead(r.Conf.Path) modelhelper.Initialize(appConfig.Mongo) defer modelhelper.Close() groupName := models.RandomGroupName() Convey("while testing get account", t, func() { Convey("if cookie is not set, should return nil", func() { a := getAccount(&http.Request{}, models.Channel_KODING_NAME) So(a, ShouldNotBeNil) So(a.Id, ShouldBeZeroValue) }) Convey("if cookie value is not set, should return nil", func() { req, _ := http.NewRequest("GET", "/", nil) expire := time.Now().AddDate(0, 0, 1) cookie := http.Cookie{ Name: "clientId", Value: "", Path: "/", Domain: "localhost", Expires: expire, } req.AddCookie(&cookie) a := getAccount(req, models.Channel_KODING_NAME) So(a, ShouldNotBeNil) So(a.Id, ShouldBeZeroValue) }) Convey("if session doesnt have username, should return nil", func() { ses, err := modelhelper.CreateSessionForAccount("", groupName) So(err, ShouldBeNil) So(ses, ShouldNotBeNil) req, _ := http.NewRequest("GET", "/", nil) expire := time.Now().AddDate(0, 0, 1) cookie := http.Cookie{ Name: "clientId", Value: ses.ClientId, Path: "/", Domain: "localhost", Expires: expire, } req.AddCookie(&cookie) a := getAccount(req, models.Channel_KODING_NAME) So(a, ShouldNotBeNil) So(a.Id, ShouldBeZeroValue) }) Convey("if session is valid, should return account", func() { acc, err := models.CreateAccountInBothDbs() So(err, ShouldBeNil) So(acc, ShouldNotBeNil) ses, err := modelhelper.CreateSessionForAccount(acc.Nick, groupName) So(err, ShouldBeNil) So(ses, ShouldNotBeNil) req, _ := http.NewRequest("GET", "/", nil) expire := time.Now().AddDate(0, 0, 1) cookie := http.Cookie{ Name: "clientId", Value: ses.ClientId, Path: "/", Domain: "localhost", Expires: expire, } req.AddCookie(&cookie) res := getAccount(req, models.Channel_KODING_NAME) So(res, ShouldNotBeNil) So(acc.Id, ShouldEqual, res.Id) }) }) Convey("while making sure account", t, func() { Convey("if account is not in postgres", func() { nick := models.RandomName() oldAcc := &kodingmodels.Account{ Id: bson.NewObjectId(), Profile: kodingmodels.AccountProfile{ Nickname: nick, }, } err := modelhelper.CreateAccount(oldAcc) So(err, ShouldBeNil) oldUser := &kodingmodels.User{ ObjectId: bson.NewObjectId(), Password: nick, Salt: nick, Name: nick, Email: nick + "@koding.com", EmailFrequency: &kodingmodels.EmailFrequency{}, } err = modelhelper.CreateUser(oldUser) So(err, ShouldBeNil) groupName := models.RandomGroupName() _, err = makeSureAccount(groupName, nick) So(err, ShouldBeNil) Convey("should create it in postgres", func() { a := models.NewAccount() err = a.ByNick(nick) So(err, ShouldBeNil) So(a.OldId, ShouldEqual, oldAcc.Id.Hex()) Convey("should set socialAPI id in mongo", func() { oldAccFromDB, err := modelhelper.GetAccount(nick) So(err, ShouldBeNil) So(oldAccFromDB.SocialApiId, ShouldEqual, strconv.FormatInt(a.Id, 10)) }) }) }) Convey("if account is in postgres", func() { acc, err := models.CreateAccountInBothDbs() So(err, ShouldBeNil) So(acc, ShouldNotBeNil) groupName := models.RandomGroupName() _, err = makeSureAccount(groupName, acc.Nick) So(err, ShouldBeNil) Convey("should be in postgres", func() { a := models.NewAccount() err = a.ByNick(acc.Nick) So(err, ShouldBeNil) So(a.OldId, ShouldEqual, acc.OldId) Convey("should have socialAPI set", func() { oldAccFromDB, err := modelhelper.GetAccount(acc.Nick) So(err, ShouldBeNil) So(oldAccFromDB.SocialApiId, ShouldEqual, strconv.FormatInt(a.Id, 10)) }) }) }) }) Convey("while making sure group membership", t, func() { Convey("if account is not not a member", func() { account := models.CreateAccountWithTest() requester := models.CreateAccountWithTest() groupChannel := models.CreateTypedPublicChannelWithTest(account.Id, models.Channel_TYPE_GROUP) err := makeSureMembership(groupChannel, requester.Id) So(err, ShouldBeNil) Convey("should add as participant", func() { status, err := groupChannel.IsParticipant(requester.Id) So(err, ShouldBeNil) So(status, ShouldBeTrue) Convey("if account is a member", func() { err := makeSureMembership(groupChannel, requester.Id) So(err, ShouldBeNil) Convey("should be a participant", func() { status, err := groupChannel.IsParticipant(requester.Id) So(err, ShouldBeNil) So(status, ShouldBeTrue) }) }) }) }) }) }