Skip to content

Commit

Permalink
Add TestContext to factor out boilerplate code
Browse files Browse the repository at this point in the history
  • Loading branch information
kegsay committed Nov 22, 2023
1 parent cb3e656 commit a8406fa
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 192 deletions.
52 changes: 52 additions & 0 deletions tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/matrix-org/complement"
"github.com/matrix-org/complement-crypto/internal/api"
"github.com/matrix-org/complement-crypto/internal/deploy"
"github.com/matrix-org/complement/client"
"github.com/matrix-org/complement/helpers"
"github.com/matrix-org/complement/must"
)

Expand Down Expand Up @@ -100,3 +102,53 @@ func MustLoginClient(t *testing.T, clientType api.ClientType, opts api.ClientCre
}
panic("unreachable")
}

type TestContext struct {
Deployment *deploy.SlidingSyncDeployment
Alice *client.CSAPI
Bob *client.CSAPI
}

func CreateTestContext(t *testing.T, clientTypeA, clientTypeB api.ClientType) *TestContext {
deployment := Deploy(t)
// pre-register alice and bob
csapiAlice := deployment.Register(t, clientTypeA.HS, helpers.RegistrationOpts{
LocalpartSuffix: "alice",
Password: "complement-crypto-password",
})
csapiBob := deployment.Register(t, clientTypeB.HS, helpers.RegistrationOpts{
LocalpartSuffix: "bob",
Password: "complement-crypto-password",
})
return &TestContext{
Deployment: deployment,
Alice: csapiAlice,
Bob: csapiBob,
}
}

func (c *TestContext) CreateNewEncryptedRoom(t *testing.T, creator *client.CSAPI, preset string, invite []string) (roomID string) {
t.Helper()
if invite == nil {
invite = []string{} // else synapse 500s
}
return creator.MustCreateRoom(t, map[string]interface{}{
"name": t.Name(),
"preset": preset,
"invite": invite,
"initial_state": []map[string]interface{}{
{
"type": "m.room.encryption",
"state_key": "",
"content": map[string]interface{}{
"algorithm": "m.megolm.v1.aes-sha2",
},
},
},
})
}

func (c *TestContext) MustLoginClient(t *testing.T, cli *client.CSAPI, clientType api.ClientType) api.Client {
t.Helper()
return MustLoginClient(t, clientType, api.FromComplementClient(cli, "complement-crypto-password"), c.Deployment.SlidingSyncURL(t))
}
Loading

0 comments on commit a8406fa

Please sign in to comment.