-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
235 additions
and
467 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package project | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/google/uuid" | ||
"github.com/porter-dev/porter/api/server/handlers" | ||
"github.com/porter-dev/porter/api/server/shared" | ||
"github.com/porter-dev/porter/api/server/shared/apierrors" | ||
"github.com/porter-dev/porter/api/server/shared/config" | ||
"github.com/porter-dev/porter/api/types" | ||
"github.com/porter-dev/porter/internal/models" | ||
"github.com/porter-dev/porter/internal/telemetry" | ||
) | ||
|
||
// GetProjectReferralDetailsHandler is a handler for getting a project's referral code | ||
type GetProjectReferralDetailsHandler struct { | ||
handlers.PorterHandlerWriter | ||
} | ||
|
||
// NewGetProjectReferralDetailsHandler returns an instance of GetProjectReferralDetailsHandler | ||
func NewGetProjectReferralDetailsHandler( | ||
config *config.Config, | ||
writer shared.ResultWriter, | ||
) *GetProjectReferralDetailsHandler { | ||
return &GetProjectReferralDetailsHandler{ | ||
PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer), | ||
} | ||
} | ||
|
||
func (c *GetProjectReferralDetailsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
ctx, span := telemetry.NewSpan(r.Context(), "serve-get-project-referral-details") | ||
defer span.End() | ||
|
||
proj, _ := ctx.Value(types.ProjectScope).(*models.Project) | ||
|
||
if !c.Config().BillingManager.MetronomeConfigLoaded || !proj.GetFeatureFlag(models.MetronomeEnabled, c.Config().LaunchDarklyClient) || | ||
proj.UsageID == uuid.Nil || proj.EnableSandbox { | ||
c.WriteResult(w, r, "") | ||
|
||
telemetry.WithAttributes(span, | ||
telemetry.AttributeKV{Key: "metronome-config-exists", Value: c.Config().BillingManager.MetronomeConfigLoaded}, | ||
telemetry.AttributeKV{Key: "metronome-enabled", Value: proj.GetFeatureFlag(models.MetronomeEnabled, c.Config().LaunchDarklyClient)}, | ||
) | ||
return | ||
} | ||
|
||
if proj.ReferralCode == "" { | ||
telemetry.WithAttributes(span, | ||
telemetry.AttributeKV{Key: "referral-code-exists", Value: false}, | ||
) | ||
|
||
// Generate referral code for project if not present | ||
proj.ReferralCode = models.NewReferralCode() | ||
_, err := c.Repo().Project().UpdateProject(proj) | ||
if err != nil { | ||
err := telemetry.Error(ctx, span, err, "error updating project") | ||
c.HandleAPIError(w, r, apierrors.NewErrInternal(err)) | ||
return | ||
} | ||
} | ||
|
||
referralCount, err := c.Repo().Referral().CountReferralsByProjectID(proj.ID, models.ReferralStatusCompleted) | ||
if err != nil { | ||
err := telemetry.Error(ctx, span, err, "error listing referrals by project id") | ||
c.HandleAPIError(w, r, apierrors.NewErrInternal(err)) | ||
return | ||
} | ||
|
||
referralCodeResponse := struct { | ||
Code string `json:"code"` | ||
ReferralCount int64 `json:"referral_count"` | ||
}{ | ||
Code: proj.ReferralCode, | ||
ReferralCount: referralCount, | ||
} | ||
|
||
c.WriteResult(w, r, referralCodeResponse) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package user_test | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"testing" | ||
|
||
|
@@ -10,7 +9,6 @@ import ( | |
"github.com/porter-dev/porter/api/server/shared/apitest" | ||
"github.com/porter-dev/porter/api/types" | ||
"github.com/porter-dev/porter/internal/repository/test" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCreateUserSuccessful(t *testing.T) { | ||
|
@@ -40,14 +38,7 @@ func TestCreateUserSuccessful(t *testing.T) { | |
// Use a struct that is the same as types.User but without the | ||
// referral fields. This is because the referral code is randomly | ||
// generated and is tested separately. | ||
expUser := &struct { | ||
ID uint `json:"id"` | ||
Email string `json:"email"` | ||
EmailVerified bool `json:"email_verified"` | ||
FirstName string `json:"first_name"` | ||
LastName string `json:"last_name"` | ||
CompanyName string `json:"company_name"` | ||
}{ | ||
expUser := &types.CreateUserResponse{ | ||
ID: 1, | ||
FirstName: "Mister", | ||
LastName: "Porter", | ||
|
@@ -56,14 +47,7 @@ func TestCreateUserSuccessful(t *testing.T) { | |
EmailVerified: false, | ||
} | ||
|
||
gotUser := &struct { | ||
ID uint `json:"id"` | ||
Email string `json:"email"` | ||
EmailVerified bool `json:"email_verified"` | ||
FirstName string `json:"first_name"` | ||
LastName string `json:"last_name"` | ||
CompanyName string `json:"company_name"` | ||
}{} | ||
gotUser := &types.CreateUserResponse{} | ||
|
||
apitest.AssertResponseExpected(t, rr, expUser, gotUser) | ||
} | ||
|
@@ -210,41 +194,3 @@ func TestFailingCreateSessionMethod(t *testing.T) { | |
|
||
apitest.AssertResponseInternalServerError(t, rr) | ||
} | ||
|
||
func TestCreateUserReferralCode(t *testing.T) { | ||
req, rr := apitest.GetRequestAndRecorder( | ||
t, | ||
string(types.HTTPVerbPost), | ||
"/api/users", | ||
&types.CreateUserRequest{ | ||
FirstName: "Mister", | ||
LastName: "Porter", | ||
CompanyName: "Porter Technologies, Inc.", | ||
Email: "[email protected]", | ||
Password: "somepassword", | ||
}, | ||
) | ||
|
||
config := apitest.LoadConfig(t) | ||
|
||
handler := user.NewUserCreateHandler( | ||
config, | ||
shared.NewDefaultRequestDecoderValidator(config.Logger, config.Alerter), | ||
shared.NewDefaultResultWriter(config.Logger, config.Alerter), | ||
) | ||
|
||
handler.ServeHTTP(rr, req) | ||
gotUser := &types.CreateUserResponse{} | ||
|
||
// apitest.AssertResponseExpected(t, rr, expUser, gotUser) | ||
err := json.NewDecoder(rr.Body).Decode(gotUser) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// This is the default lenth of a shortuuid | ||
desiredLenth := 22 | ||
assert.NotEmpty(t, gotUser.ReferralCode, "referral code should not be empty") | ||
assert.Len(t, gotUser.ReferralCode, desiredLenth, "referral code should be 22 characters long") | ||
assert.Equal(t, gotUser.ReferralRewardClaimed, false, "referral reward claimed should be false for new user") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.