Test anonymous user is same as non-tier user

pull/600/head
binwiederhier 2023-01-28 21:27:05 -05:00
parent 92d563371c
commit 0e36ac84d8
5 changed files with 90 additions and 10 deletions

View File

@ -39,7 +39,6 @@ import (
- HIGH Rate limiting: Sensitive endpoints (account/login/change-password/...) - HIGH Rate limiting: Sensitive endpoints (account/login/change-password/...)
- HIGH Stripe payment methods - HIGH Stripe payment methods
- MEDIUM: Test new token endpoints & never-expiring token - MEDIUM: Test new token endpoints & never-expiring token
- MEDIUM: Test that anonymous user and user without tier are the same visitor
- MEDIUM: Make sure account endpoints make sense for admins - MEDIUM: Make sure account endpoints make sense for admins
- MEDIUM: Reservation (UI): Show "This topic is reserved" error message when trying to reserve a reserved topic (Thorben) - MEDIUM: Reservation (UI): Show "This topic is reserved" error message when trying to reserve a reserved topic (Thorben)
- MEDIUM: Reservation (UI): Ask for confirmation when removing reservation (deadcade) - MEDIUM: Reservation (UI): Ask for confirmation when removing reservation (deadcade)

View File

@ -622,3 +622,50 @@ func TestAccount_Reservation_Add_Kills_Other_Subscribers(t *testing.T) {
t.Fatal("Waiting for user subscription to be killed failed") t.Fatal("Waiting for user subscription to be killed failed")
} }
} }
func TestAccount_Persist_UserStats_After_Tier_Change(t *testing.T) {
conf := newTestConfigWithAuthFile(t)
conf.AuthDefault = user.PermissionReadWrite
conf.AuthStatsQueueWriterInterval = 100 * time.Millisecond
s := newTestServer(t, conf)
defer s.closeDatabases()
// Create user with tier
require.Nil(t, s.userManager.AddUser("phil", "phil", user.RoleUser))
require.Nil(t, s.userManager.CreateTier(&user.Tier{
Code: "starter",
MessageLimit: 10,
}))
require.Nil(t, s.userManager.CreateTier(&user.Tier{
Code: "pro",
MessageLimit: 20,
}))
require.Nil(t, s.userManager.ChangeTier("phil", "starter"))
// Publish a message
rr := request(t, s, "POST", "/mytopic", "hi", map[string]string{
"Authorization": util.BasicAuth("phil", "phil"),
})
require.Equal(t, 200, rr.Code)
// Wait for stats queue writer
time.Sleep(200 * time.Millisecond)
// Verify that message stats were persisted
u, err := s.userManager.User("phil")
require.Nil(t, err)
require.Equal(t, int64(1), u.Stats.Messages)
// Change tier, make a request (to reset limiters)
require.Nil(t, s.userManager.ChangeTier("phil", "pro"))
rr = request(t, s, "GET", "/v1/account", "", map[string]string{
"Authorization": util.BasicAuth("phil", "phil"),
})
require.Equal(t, 200, rr.Code)
// Verify that message stats were persisted
time.Sleep(300 * time.Millisecond)
u, err = s.userManager.User("phil")
require.Nil(t, err)
require.Equal(t, int64(0), u.Stats.Messages) // v.EnqueueStats had run!
}

View File

@ -1785,10 +1785,45 @@ func TestServer_PublishWhileUpdatingStatsWithLotsOfMessages(t *testing.T) {
log.Info("Done: Publishing message; took %s", time.Since(start).Round(time.Millisecond)) log.Info("Done: Publishing message; took %s", time.Since(start).Round(time.Millisecond))
// Wait for all goroutines // Wait for all goroutines
<-statsChan select {
case <-statsChan:
case <-time.After(10 * time.Second):
t.Fatal("Timed out waiting for Go routines")
}
log.Info("Done: Waiting for all locks") log.Info("Done: Waiting for all locks")
} }
func TestServer_AnonymousUser_And_NonTierUser_Are_Same_Visitor(t *testing.T) {
conf := newTestConfigWithAuthFile(t)
s := newTestServer(t, conf)
defer s.closeDatabases()
// Create user without tier
require.Nil(t, s.userManager.AddUser("phil", "phil", user.RoleUser))
// Publish a message (anonymous user)
rr := request(t, s, "POST", "/mytopic", "hi", nil)
require.Equal(t, 200, rr.Code)
// Publish a message (non-tier user)
rr = request(t, s, "POST", "/mytopic", "hi", map[string]string{
"Authorization": util.BasicAuth("phil", "phil"),
})
require.Equal(t, 200, rr.Code)
// User stats (anonymous user)
rr = request(t, s, "GET", "/v1/account", "", nil)
account, _ := util.UnmarshalJSON[apiAccountResponse](io.NopCloser(rr.Body))
require.Equal(t, int64(2), account.Stats.Messages)
// User stats (non-tier user)
rr = request(t, s, "GET", "/v1/account", "", map[string]string{
"Authorization": util.BasicAuth("phil", "phil"),
})
account, _ = util.UnmarshalJSON[apiAccountResponse](io.NopCloser(rr.Body))
require.Equal(t, int64(2), account.Stats.Messages)
}
func newTestConfig(t *testing.T) *Config { func newTestConfig(t *testing.T) *Config {
conf := NewConfig() conf := NewConfig()
conf.BaseURL = "http://127.0.0.1:12345" conf.BaseURL = "http://127.0.0.1:12345"

View File

@ -302,13 +302,12 @@ func (v *visitor) resetLimitersNoLock(messages, emails int64, enqueueUpdate bool
} else { } else {
v.accountLimiter = nil // Users cannot create accounts when logged in v.accountLimiter = nil // Users cannot create accounts when logged in
} }
/* if enqueueUpdate && v.user != nil {
if enqueueUpdate && v.user != nil { go v.userManager.EnqueueStats(v.user.ID, &user.Stats{
go v.userManager.EnqueueStats(v.user.ID, &user.Stats{ Messages: messages,
Messages: messages, Emails: emails,
Emails: emails, })
}) }
}*/
} }
func (v *visitor) Limits() *visitorLimits { func (v *visitor) Limits() *visitorLimits {

View File

@ -1113,7 +1113,7 @@ func (a *Manager) CreateTier(tier *Tier) error {
if tier.ID == "" { if tier.ID == "" {
tier.ID = util.RandomStringPrefix(tierIDPrefix, tierIDLength) tier.ID = util.RandomStringPrefix(tierIDPrefix, tierIDLength)
} }
if _, err := a.db.Exec(insertTierQuery, tier.ID, tier.Code, tier.Name, tier.MessageLimit, int64(tier.MessageExpiryDuration.Seconds()), tier.EmailLimit, tier.ReservationLimit, tier.AttachmentFileSizeLimit, tier.AttachmentTotalSizeLimit, int64(tier.AttachmentExpiryDuration.Seconds()), tier.AttachmentBandwidthLimit, tier.StripePriceID); err != nil { if _, err := a.db.Exec(insertTierQuery, tier.ID, tier.Code, tier.Name, tier.MessageLimit, int64(tier.MessageExpiryDuration.Seconds()), tier.EmailLimit, tier.ReservationLimit, tier.AttachmentFileSizeLimit, tier.AttachmentTotalSizeLimit, int64(tier.AttachmentExpiryDuration.Seconds()), tier.AttachmentBandwidthLimit, nullString(tier.StripePriceID)); err != nil {
return err return err
} }
return nil return nil