2019-06-28 15:34:10 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package marketingweb_test
|
|
|
|
|
|
|
|
import (
|
2019-08-23 19:33:21 +01:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2019-06-28 15:34:10 +01:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2019-07-10 18:12:40 +01:00
|
|
|
"strconv"
|
2019-06-28 15:34:10 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/testcontext"
|
|
|
|
"storj.io/storj/private/testplanet"
|
2019-06-28 15:34:10 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type CreateRequest struct {
|
|
|
|
Path string
|
|
|
|
Values url.Values
|
|
|
|
}
|
|
|
|
|
2019-07-10 18:12:40 +01:00
|
|
|
func TestCreateAndStopOffers(t *testing.T) {
|
2019-06-28 15:34:10 +01:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
|
|
|
|
requests := []CreateRequest{
|
|
|
|
{
|
2019-08-23 19:33:21 +01:00
|
|
|
Path: "/create/referral",
|
2019-06-28 15:34:10 +01:00
|
|
|
Values: url.Values{
|
|
|
|
"Name": {"Referral Credit"},
|
|
|
|
"Description": {"desc"},
|
|
|
|
"ExpiresAt": {"2119-06-27"},
|
2019-07-01 20:16:49 +01:00
|
|
|
"InviteeCredit": {"50"},
|
2019-06-28 15:34:10 +01:00
|
|
|
"InviteeCreditDurationDays": {"50"},
|
2019-07-01 20:16:49 +01:00
|
|
|
"AwardCredit": {"50"},
|
2019-06-28 15:34:10 +01:00
|
|
|
"AwardCreditDurationDays": {"50"},
|
|
|
|
"RedeemableCap": {"150"},
|
|
|
|
},
|
|
|
|
}, {
|
2019-08-23 19:33:21 +01:00
|
|
|
Path: "/create/free-credit",
|
2019-06-28 15:34:10 +01:00
|
|
|
Values: url.Values{
|
2019-07-25 23:06:23 +01:00
|
|
|
"Name": {"Free Credit"},
|
|
|
|
"Description": {"desc"},
|
|
|
|
"ExpiresAt": {"2119-06-27"},
|
|
|
|
"InviteeCredit": {"50"},
|
|
|
|
"InviteeCreditDurationDays": {"50"},
|
|
|
|
"RedeemableCap": {"150"},
|
|
|
|
},
|
|
|
|
}, {
|
2019-08-23 19:33:21 +01:00
|
|
|
Path: "/create/partner",
|
2019-07-25 23:06:23 +01:00
|
|
|
Values: url.Values{
|
2019-08-23 19:33:21 +01:00
|
|
|
"Name": {"FileZilla"},
|
2019-06-28 15:34:10 +01:00
|
|
|
"Description": {"desc"},
|
|
|
|
"ExpiresAt": {"2119-06-27"},
|
2019-07-01 20:16:49 +01:00
|
|
|
"InviteeCredit": {"50"},
|
2019-06-28 15:34:10 +01:00
|
|
|
"InviteeCreditDurationDays": {"50"},
|
|
|
|
"RedeemableCap": {"150"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := planet.Satellites[0].Marketing.Listener.Addr()
|
|
|
|
|
|
|
|
var group errgroup.Group
|
2019-07-10 18:12:40 +01:00
|
|
|
for index, offer := range requests {
|
2019-06-28 15:34:10 +01:00
|
|
|
o := offer
|
2019-07-10 18:12:40 +01:00
|
|
|
id := strconv.Itoa(index + 1)
|
|
|
|
|
2019-06-28 15:34:10 +01:00
|
|
|
group.Go(func() error {
|
|
|
|
baseURL := "http://" + addr.String()
|
|
|
|
|
2019-08-22 12:40:15 +01:00
|
|
|
req, err := http.PostForm(baseURL+o.Path, o.Values)
|
2019-06-28 15:34:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-23 19:33:21 +01:00
|
|
|
require.Equal(t, http.StatusOK, req.StatusCode)
|
|
|
|
//reading out the rest of the connection
|
|
|
|
_, err = io.Copy(ioutil.Discard, req.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-22 12:40:15 +01:00
|
|
|
if err := req.Body.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-28 15:34:10 +01:00
|
|
|
|
2019-08-22 12:40:15 +01:00
|
|
|
req, err = http.Get(baseURL)
|
2019-06-28 15:34:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-23 19:33:21 +01:00
|
|
|
require.Equal(t, http.StatusOK, req.StatusCode)
|
|
|
|
_, err = io.Copy(ioutil.Discard, req.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-22 12:40:15 +01:00
|
|
|
if err := req.Body.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-28 15:34:10 +01:00
|
|
|
|
2019-08-22 12:40:15 +01:00
|
|
|
req, err = http.Post(baseURL+"/stop/"+id, "application/x-www-form-urlencoded", nil)
|
2019-07-10 18:12:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-23 19:33:21 +01:00
|
|
|
require.Equal(t, http.StatusOK, req.StatusCode)
|
|
|
|
_, err = io.Copy(ioutil.Discard, req.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-22 12:40:15 +01:00
|
|
|
if err := req.Body.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-10 18:12:40 +01:00
|
|
|
|
2019-06-28 15:34:10 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
err := group.Wait()
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|