()
| 8 | ) |
| 9 | |
| 10 | func (s *DatabaseSuite) TestClient() { |
| 11 | if client, err := s.db.GetClientByID(1); assert.NoError(s.T(), err) { |
| 12 | assert.Nil(s.T(), client, "not existing client") |
| 13 | } |
| 14 | if client, err := s.db.GetClientByToken("asdasd"); assert.NoError(s.T(), err) { |
| 15 | assert.Nil(s.T(), client, "not existing client") |
| 16 | } |
| 17 | |
| 18 | user := &model.User{Name: "test", Pass: []byte{1}} |
| 19 | s.db.CreateUser(user) |
| 20 | assert.NotEqual(s.T(), 0, user.ID) |
| 21 | |
| 22 | if clients, err := s.db.GetClientsByUser(user.ID); assert.NoError(s.T(), err) { |
| 23 | assert.Empty(s.T(), clients) |
| 24 | } |
| 25 | |
| 26 | client := &model.Client{UserID: user.ID, Token: "C0000000000", Name: "android"} |
| 27 | assert.NoError(s.T(), s.db.CreateClient(client)) |
| 28 | |
| 29 | if clients, err := s.db.GetClientsByUser(user.ID); assert.NoError(s.T(), err) { |
| 30 | assert.Len(s.T(), clients, 1) |
| 31 | assert.Contains(s.T(), clients, client) |
| 32 | } |
| 33 | |
| 34 | newClient, err := s.db.GetClientByID(client.ID) |
| 35 | if assert.NoError(s.T(), err) { |
| 36 | assert.Equal(s.T(), client, newClient) |
| 37 | } |
| 38 | |
| 39 | if newClient, err := s.db.GetClientByToken(client.Token); assert.NoError(s.T(), err) { |
| 40 | assert.Equal(s.T(), client, newClient) |
| 41 | } |
| 42 | |
| 43 | updateClient := &model.Client{ID: client.ID, UserID: user.ID, Token: "C0000000000", Name: "new_name"} |
| 44 | s.db.UpdateClient(updateClient) |
| 45 | if updatedClient, err := s.db.GetClientByID(client.ID); assert.NoError(s.T(), err) { |
| 46 | assert.Equal(s.T(), updateClient, updatedClient) |
| 47 | } |
| 48 | |
| 49 | lastUsed := time.Now().Add(-time.Hour) |
| 50 | s.db.UpdateClientTokensLastUsed([]string{client.Token}, &lastUsed) |
| 51 | newClient, err = s.db.GetClientByID(client.ID) |
| 52 | if assert.NoError(s.T(), err) { |
| 53 | assert.Equal(s.T(), lastUsed.Unix(), newClient.LastUsed.Unix()) |
| 54 | } |
| 55 | |
| 56 | s.db.DeleteClientByID(client.ID) |
| 57 | |
| 58 | if clients, err := s.db.GetClientsByUser(user.ID); assert.NoError(s.T(), err) { |
| 59 | assert.Empty(s.T(), clients) |
| 60 | } |
| 61 | |
| 62 | if client, err := s.db.GetClientByID(client.ID); assert.NoError(s.T(), err) { |
| 63 | assert.Nil(s.T(), client) |
| 64 | } |
| 65 | } |
nothing calls this directly
no test coverage detected