diff options
author | Jesús Espino <jespinog@gmail.com> | 2018-04-18 22:46:10 +0200 |
---|---|---|
committer | Christopher Speller <crspeller@gmail.com> | 2018-04-18 13:46:10 -0700 |
commit | 0910eae31de8ed7b409654515dbd11f5c86dbf71 (patch) | |
tree | 3d5fb47842693cd2ea1a357994c85d04902773a7 /app/team_test.go | |
parent | b13a228b0451098ea32933a36fe64566e366583d (diff) | |
download | chat-0910eae31de8ed7b409654515dbd11f5c86dbf71.tar.gz chat-0910eae31de8ed7b409654515dbd11f5c86dbf71.tar.bz2 chat-0910eae31de8ed7b409654515dbd11f5c86dbf71.zip |
MM-9779: Incorporate a Token into the invitations system (#8604)
* Incorporate a Token into the invitations system
* Adding unit tests
* Fixing some api4 client tests
* Removing unnecesary hash validation
* Change the Hash concept on invitations with tokenId
* Not send invitation if it wasn't able to create the Token
* Fixing some naming problems
* Changing the hash query params received from the client side
* Removed unneded data param in the token usage
Diffstat (limited to 'app/team_test.go')
-rw-r--r-- | app/team_test.go | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/app/team_test.go b/app/team_test.go index 95f4b83d6..7ebfb8166 100644 --- a/app/team_test.go +++ b/app/team_test.go @@ -105,6 +105,84 @@ func TestAddUserToTeam(t *testing.T) { } } +func TestAddUserToTeamByToken(t *testing.T) { + th := Setup().InitBasic() + defer th.TearDown() + + user := model.User{Email: strings.ToLower(model.NewId()) + "success+test@example.com", Nickname: "Darth Vader", Username: "vader" + model.NewId(), Password: "passwd1", AuthService: ""} + ruser, _ := th.App.CreateUser(&user) + + t.Run("invalid token", func(t *testing.T) { + if _, err := th.App.AddUserToTeamByToken(ruser.Id, "123"); err == nil { + t.Fatal("Should fail on unexisting token") + } + }) + + t.Run("invalid token type", func(t *testing.T) { + token := model.NewToken( + TOKEN_TYPE_VERIFY_EMAIL, + model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id}), + ) + <-th.App.Srv.Store.Token().Save(token) + defer th.App.DeleteToken(token) + if _, err := th.App.AddUserToTeamByToken(ruser.Id, token.Token); err == nil { + t.Fatal("Should fail on bad token type") + } + }) + + t.Run("expired token", func(t *testing.T) { + token := model.NewToken( + TOKEN_TYPE_TEAM_INVITATION, + model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id}), + ) + token.CreateAt = model.GetMillis() - TEAM_INVITATION_EXPIRY_TIME - 1 + <-th.App.Srv.Store.Token().Save(token) + defer th.App.DeleteToken(token) + if _, err := th.App.AddUserToTeamByToken(ruser.Id, token.Token); err == nil { + t.Fatal("Should fail on expired token") + } + }) + + t.Run("invalid team id", func(t *testing.T) { + token := model.NewToken( + TOKEN_TYPE_TEAM_INVITATION, + model.MapToJson(map[string]string{"teamId": model.NewId()}), + ) + <-th.App.Srv.Store.Token().Save(token) + defer th.App.DeleteToken(token) + if _, err := th.App.AddUserToTeamByToken(ruser.Id, token.Token); err == nil { + t.Fatal("Should fail on bad team id") + } + }) + + t.Run("invalid user id", func(t *testing.T) { + token := model.NewToken( + TOKEN_TYPE_TEAM_INVITATION, + model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id}), + ) + <-th.App.Srv.Store.Token().Save(token) + defer th.App.DeleteToken(token) + if _, err := th.App.AddUserToTeamByToken(model.NewId(), token.Token); err == nil { + t.Fatal("Should fail on bad user id") + } + }) + + t.Run("valid request", func(t *testing.T) { + token := model.NewToken( + TOKEN_TYPE_TEAM_INVITATION, + model.MapToJson(map[string]string{"teamId": th.BasicTeam.Id}), + ) + <-th.App.Srv.Store.Token().Save(token) + if _, err := th.App.AddUserToTeamByToken(ruser.Id, token.Token); err != nil { + t.Log(err) + t.Fatal("Should add user to the team") + } + if result := <-th.App.Srv.Store.Token().GetByToken(token.Token); result.Err == nil { + t.Fatal("The token must be deleted after be used") + } + }) +} + func TestAddUserToTeamByTeamId(t *testing.T) { th := Setup().InitBasic() defer th.TearDown() |