gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

email_test.go (4660B)


      1 // GoToSocial
      2 // Copyright (C) GoToSocial Authors admin@gotosocial.org
      3 // SPDX-License-Identifier: AGPL-3.0-or-later
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 package user_test
     19 
     20 import (
     21 	"context"
     22 	"fmt"
     23 	"testing"
     24 	"time"
     25 
     26 	"github.com/stretchr/testify/suite"
     27 )
     28 
     29 type EmailConfirmTestSuite struct {
     30 	UserStandardTestSuite
     31 }
     32 
     33 func (suite *EmailConfirmTestSuite) TestSendConfirmEmail() {
     34 	user := suite.testUsers["local_account_1"]
     35 
     36 	// set a bunch of stuff on the user as though zork hasn't been confirmed (perish the thought)
     37 	user.UnconfirmedEmail = "some.email@example.org"
     38 	user.Email = ""
     39 	user.ConfirmedAt = time.Time{}
     40 	user.ConfirmationSentAt = time.Time{}
     41 	user.ConfirmationToken = ""
     42 
     43 	err := suite.user.EmailSendConfirmation(context.Background(), user, "the_mighty_zork")
     44 	suite.NoError(err)
     45 
     46 	// zork should have an email now
     47 	suite.Len(suite.sentEmails, 1)
     48 	email, ok := suite.sentEmails["some.email@example.org"]
     49 	suite.True(ok)
     50 
     51 	// a token should be set on zork
     52 	token := user.ConfirmationToken
     53 	suite.NotEmpty(token)
     54 
     55 	// email should contain the token
     56 	emailShould := fmt.Sprintf("To: some.email@example.org\r\nFrom: test@example.org\r\nSubject: GoToSocial Email Confirmation\r\n\r\nHello the_mighty_zork!\r\n\r\nYou are receiving this mail because you've requested an account on http://localhost:8080.\r\n\r\nWe just need to confirm that this is your email address. To confirm your email, paste the following in your browser's address bar:\r\n\r\nhttp://localhost:8080/confirm_email?token=%s\r\n\r\nIf you believe you've been sent this email in error, feel free to ignore it, or contact the administrator of http://localhost:8080\r\n\r\n", token)
     57 	suite.Equal(emailShould, email)
     58 
     59 	// confirmationSentAt should be recent
     60 	suite.WithinDuration(time.Now(), user.ConfirmationSentAt, 1*time.Minute)
     61 }
     62 
     63 func (suite *EmailConfirmTestSuite) TestConfirmEmail() {
     64 	ctx := context.Background()
     65 
     66 	user := suite.testUsers["local_account_1"]
     67 
     68 	// set a bunch of stuff on the user as though zork hasn't been confirmed yet, but has had an email sent 5 minutes ago
     69 	updatingColumns := []string{"unconfirmed_email", "email", "confirmed_at", "confirmation_sent_at", "confirmation_token"}
     70 	user.UnconfirmedEmail = "some.email@example.org"
     71 	user.Email = ""
     72 	user.ConfirmedAt = time.Time{}
     73 	user.ConfirmationSentAt = time.Now().Add(-5 * time.Minute)
     74 	user.ConfirmationToken = "1d1aa44b-afa4-49c8-ac4b-eceb61715cc6"
     75 
     76 	err := suite.db.UpdateByID(ctx, user, user.ID, updatingColumns...)
     77 	suite.NoError(err)
     78 
     79 	// confirm with the token set above
     80 	updatedUser, errWithCode := suite.user.EmailConfirm(ctx, "1d1aa44b-afa4-49c8-ac4b-eceb61715cc6")
     81 	suite.NoError(errWithCode)
     82 
     83 	// email should now be confirmed and token cleared
     84 	suite.Equal("some.email@example.org", updatedUser.Email)
     85 	suite.Empty(updatedUser.UnconfirmedEmail)
     86 	suite.Empty(updatedUser.ConfirmationToken)
     87 	suite.WithinDuration(updatedUser.ConfirmedAt, time.Now(), 1*time.Minute)
     88 	suite.WithinDuration(updatedUser.UpdatedAt, time.Now(), 1*time.Minute)
     89 }
     90 
     91 func (suite *EmailConfirmTestSuite) TestConfirmEmailOldToken() {
     92 	ctx := context.Background()
     93 
     94 	user := suite.testUsers["local_account_1"]
     95 
     96 	// set a bunch of stuff on the user as though zork hasn't been confirmed yet, but has had an email sent 8 days ago
     97 	updatingColumns := []string{"unconfirmed_email", "email", "confirmed_at", "confirmation_sent_at", "confirmation_token"}
     98 	user.UnconfirmedEmail = "some.email@example.org"
     99 	user.Email = ""
    100 	user.ConfirmedAt = time.Time{}
    101 	user.ConfirmationSentAt = time.Now().Add(-192 * time.Hour)
    102 	user.ConfirmationToken = "1d1aa44b-afa4-49c8-ac4b-eceb61715cc6"
    103 
    104 	err := suite.db.UpdateByID(ctx, user, user.ID, updatingColumns...)
    105 	suite.NoError(err)
    106 
    107 	// confirm with the token set above
    108 	updatedUser, errWithCode := suite.user.EmailConfirm(ctx, "1d1aa44b-afa4-49c8-ac4b-eceb61715cc6")
    109 	suite.Nil(updatedUser)
    110 	suite.EqualError(errWithCode, "ConfirmEmail: confirmation token expired")
    111 }
    112 
    113 func TestEmailConfirmTestSuite(t *testing.T) {
    114 	suite.Run(t, &EmailConfirmTestSuite{})
    115 }