(t *testing.T)
| 28 | ) |
| 29 | |
| 30 | func TestLoginFromJSONReader(t *testing.T) { |
| 31 | ctx := context.Background() |
| 32 | |
| 33 | tsts := []struct { |
| 34 | Name string |
| 35 | Body string |
| 36 | |
| 37 | WantUsername string |
| 38 | WantDeviceID string |
| 39 | WantDeletedTokens []string |
| 40 | }{ |
| 41 | { |
| 42 | Name: "passwordWorks", |
| 43 | Body: `{ |
| 44 | "type": "m.login.password", |
| 45 | "identifier": { "type": "m.id.user", "user": "alice" }, |
| 46 | "password": "herpassword", |
| 47 | "device_id": "adevice" |
| 48 | }`, |
| 49 | WantUsername: "alice", |
| 50 | WantDeviceID: "adevice", |
| 51 | }, |
| 52 | { |
| 53 | Name: "tokenWorks", |
| 54 | Body: `{ |
| 55 | "type": "m.login.token", |
| 56 | "token": "atoken", |
| 57 | "device_id": "adevice" |
| 58 | }`, |
| 59 | WantUsername: "@auser:example.com", |
| 60 | WantDeviceID: "adevice", |
| 61 | WantDeletedTokens: []string{"atoken"}, |
| 62 | }, |
| 63 | } |
| 64 | for _, tst := range tsts { |
| 65 | t.Run(tst.Name, func(t *testing.T) { |
| 66 | var userAPI fakeUserInternalAPI |
| 67 | cfg := &config.ClientAPI{ |
| 68 | Matrix: &config.Global{ |
| 69 | ServerName: serverName, |
| 70 | }, |
| 71 | } |
| 72 | login, cleanup, err := LoginFromJSONReader(ctx, strings.NewReader(tst.Body), &userAPI, &userAPI, cfg) |
| 73 | if err != nil { |
| 74 | t.Fatalf("LoginFromJSONReader failed: %+v", err) |
| 75 | } |
| 76 | cleanup(ctx, &util.JSONResponse{Code: http.StatusOK}) |
| 77 | |
| 78 | if login.Username() != tst.WantUsername { |
| 79 | t.Errorf("Username: got %q, want %q", login.Username(), tst.WantUsername) |
| 80 | } |
| 81 | |
| 82 | if login.DeviceID == nil { |
| 83 | if tst.WantDeviceID != "" { |
| 84 | t.Errorf("DeviceID: got %v, want %q", login.DeviceID, tst.WantDeviceID) |
| 85 | } |
| 86 | } else { |
| 87 | if *login.DeviceID != tst.WantDeviceID { |
nothing calls this directly
no test coverage detected