diff --git a/go/src/socialapi/models/helpers.go b/go/src/socialapi/models/helpers.go index 3c6912a2624..86eaeaace63 100644 --- a/go/src/socialapi/models/helpers.go +++ b/go/src/socialapi/models/helpers.go @@ -322,3 +322,21 @@ func UpdateUsernameInBothDbs(currentUsername, newUsername string) error { return acc.Update() } + +// WithStubData creates barebones for operating with koding services. +func WithStubData(f func(username string, groupName string, sessionID string)) { + acc, _, groupName := CreateRandomGroupDataWithChecks() + + group, err := modelhelper.GetGroup(groupName) + So(err, ShouldBeNil) + So(group, ShouldNotBeNil) + + err = modelhelper.MakeAdmin(bson.ObjectIdHex(acc.OldId), group.Id) + So(err, ShouldBeNil) + + ses, err := modelhelper.FetchOrCreateSession(acc.Nick, groupName) + So(err, ShouldBeNil) + So(ses, ShouldNotBeNil) + + f(acc.Nick, groupName, ses.ClientId) +} diff --git a/go/src/socialapi/workers/payment/api/server_test.go b/go/src/socialapi/workers/payment/api/server_test.go index f0d572bf7ff..a77a867fc0f 100644 --- a/go/src/socialapi/workers/payment/api/server_test.go +++ b/go/src/socialapi/workers/payment/api/server_test.go @@ -2,7 +2,6 @@ package api import ( "fmt" - "koding/db/mongodb/modelhelper" "socialapi/config" "socialapi/workers/common/mux" "socialapi/workers/common/tests" @@ -15,41 +14,29 @@ import ( // TODO(cihangir): make generalized "withTestServer" func withTestServer(t *testing.T, f func(url string)) { - const workerName = "paymentwebhook" + tests.WithRunner(t, func(r *runner.Runner) { + if r.Conf.Debug { + stripe.LogLevel = 3 + } + payment.Initialize(config.MustGet()) + port := tests.GetFreePort() + mc := mux.NewConfig(r.Name, "localhost", port) + mc.Debug = r.Conf.Debug + m := mux.New(mc, r.Log, r.Metrics) - r := runner.New(workerName) - if err := r.Init(); err != nil { - t.Fatal(err) - } + AddHandlers(m) - if r.Conf.Debug { - stripe.LogLevel = 3 - } + m.Listen() - c := config.MustRead(r.Conf.Path) - // init mongo connection - modelhelper.Initialize(c.Mongo) - defer modelhelper.Close() + go r.Listen() - payment.Initialize(c) + f(fmt.Sprintf("http://localhost:%s", port)) - port := tests.GetFreePort() - mc := mux.NewConfig(workerName, "localhost", port) - mc.Debug = r.Conf.Debug - m := mux.New(mc, r.Log, r.Metrics) + if err := r.Close(); err != nil { + t.Fatalf("server close errored: %s", err.Error()) + } - AddHandlers(m) - - m.Listen() - - go r.Listen() - - f(fmt.Sprintf("http://localhost:%s", port)) - - if err := r.Close(); err != nil { - t.Fatalf("server close errored: %s", err.Error()) - } - - // shutdown server - m.Close() + // shutdown server + m.Close() + }) } diff --git a/go/src/socialapi/workers/payment/api/util_test.go b/go/src/socialapi/workers/payment/api/util_test.go index 20571e6d4de..88addcaa891 100644 --- a/go/src/socialapi/workers/payment/api/util_test.go +++ b/go/src/socialapi/workers/payment/api/util_test.go @@ -19,27 +19,18 @@ import ( ) func withStubData(endpoint string, f func(username string, groupName string, sessionID string)) { - createURL := endpoint + EndpointCustomerCreate - acc, _, groupName := models.CreateRandomGroupDataWithChecks() + models.WithStubData(func(username string, groupName string, sessionID string) { + req, err := json.Marshal(&stripe.CustomerParams{}) + tests.ResultedWithNoErrorCheck(req, err) - group, err := modelhelper.GetGroup(groupName) - tests.ResultedWithNoErrorCheck(group, err) - - err = modelhelper.MakeAdmin(bson.ObjectIdHex(acc.OldId), group.Id) - So(err, ShouldBeNil) - - ses, err := modelhelper.FetchOrCreateSession(acc.Nick, groupName) - tests.ResultedWithNoErrorCheck(ses, err) + createURL := endpoint + EndpointCustomerCreate + res, err := rest.DoRequestWithAuth("POST", createURL, req, sessionID) + tests.ResultedWithNoErrorCheck(res, err) - req, err := json.Marshal(&stripe.CustomerParams{}) - tests.ResultedWithNoErrorCheck(req, err) - - res, err := rest.DoRequestWithAuth("POST", createURL, req, ses.ClientId) - tests.ResultedWithNoErrorCheck(res, err) + f(username, groupName, sessionID) - f(acc.Nick, groupName, ses.ClientId) - - So(payment.DeleteCustomerForGroup(groupName), ShouldBeNil) + So(payment.DeleteCustomerForGroup(groupName), ShouldBeNil) + }) } func withTestPlan(f func(planID string)) { diff --git a/go/src/socialapi/workers/presence/api/handlers_test.go b/go/src/socialapi/workers/presence/api/handlers_test.go index 61d8083be48..041c151c32a 100644 --- a/go/src/socialapi/workers/presence/api/handlers_test.go +++ b/go/src/socialapi/workers/presence/api/handlers_test.go @@ -15,13 +15,12 @@ import ( "github.com/koding/runner" . "github.com/smartystreets/goconvey/convey" - "gopkg.in/mgo.v2/bson" ) func TestPing(t *testing.T) { Convey("Given testing user & group", t, func() { withTestServer(t, func(endpoint string) { - withStubData(endpoint, func(username, groupName, sessionID string) { + models.WithStubData(func(username, groupName, sessionID string) { Convey("We should be able to send the ping request to", func() { externalURL := endpoint + presence.EndpointPresencePing privateURL := endpoint + presence.EndpointPresencePingPrivate @@ -54,7 +53,7 @@ func TestPing(t *testing.T) { func TestPingWithClient(t *testing.T) { Convey("Given testing user & group", t, func() { withTestServer(t, func(endpoint string) { - withStubData(endpoint, func(username, groupName, sessionID string) { + models.WithStubData(func(username, groupName, sessionID string) { Convey("We should be able to send the ping request", func() { Convey("with public client", func() { c := client.NewPublic(endpoint) @@ -76,21 +75,6 @@ func TestPingWithClient(t *testing.T) { //TODO(cihangir): below shamelessly copied from payment api tests with small //modifications, unify them. -func withStubData(endpoint string, f func(username string, groupName string, sessionID string)) { - acc, _, groupName := models.CreateRandomGroupDataWithChecks() - - group, err := modelhelper.GetGroup(groupName) - tests.ResultedWithNoErrorCheck(group, err) - - err = modelhelper.MakeAdmin(bson.ObjectIdHex(acc.OldId), group.Id) - So(err, ShouldBeNil) - - ses, err := modelhelper.FetchOrCreateSession(acc.Nick, groupName) - tests.ResultedWithNoErrorCheck(ses, err) - - f(acc.Nick, groupName, ses.ClientId) -} - func withTestServer(t *testing.T, f func(url string)) { const workerName = "pingtest" diff --git a/scripts/gotests.sh b/scripts/gotests.sh index 38e519eee10..3dd23156289 100755 --- a/scripts/gotests.sh +++ b/scripts/gotests.sh @@ -106,6 +106,11 @@ elif [ "$1" == "socialapi" ]; then export RUN_FLAGS=${RUN_FLAGS:-"-c=$KONFIG_SOCIALAPI_CONFIGFILEPATH"} runAll $@ +# useful after a refactoring, just to see if there is any error +elif [ "$1" == "compile" ]; then + shift + compile $@ + clean $@ else runAll $@ fi