Skip to content
This repository has been archived by the owner on Aug 15, 2022. It is now read-only.

Unify WithStubData function #10774

Merged
merged 3 commits into from Mar 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions go/src/socialapi/models/helpers.go
Expand Up @@ -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)
}
51 changes: 19 additions & 32 deletions go/src/socialapi/workers/payment/api/server_test.go
Expand Up @@ -2,7 +2,6 @@ package api

import (
"fmt"
"koding/db/mongodb/modelhelper"
"socialapi/config"
"socialapi/workers/common/mux"
"socialapi/workers/common/tests"
Expand All @@ -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()
})
}
27 changes: 9 additions & 18 deletions go/src/socialapi/workers/payment/api/util_test.go
Expand Up @@ -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)) {
Expand Down
20 changes: 2 additions & 18 deletions go/src/socialapi/workers/presence/api/handlers_test.go
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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"

Expand Down
5 changes: 5 additions & 0 deletions scripts/gotests.sh
Expand Up @@ -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