Commit graph

885 commits

Author SHA1 Message Date
smavros
ff19fdcdaf Modifies NewTestEndpoint() by removing if body == nil 2019-08-15 18:44:49 +02:00
smavros
94ea3c4ebf Breaks user test into different testing functions 2019-08-15 18:16:30 +02:00
smavros
c2429394ed Renames VisitorAuthenticate() to RegisterAuthenticate() 2019-08-15 14:52:19 +02:00
smavros
426b834ea5 Modifies tests for accepting {"id":$userID} response 2019-08-15 14:37:13 +02:00
smavros
8ce554b6d7 Modifies user/ endpoint:
Functions `addUser()`, `updateUser()` and `deleteUser()` now return
    a body of `{ "id": $userID }`.
2019-08-15 14:34:35 +02:00
smavros
1e4a2ee7cb Adds test for PUT /user/$ID endpoint 2019-08-15 00:36:46 +02:00
smavros
7529f4803f Improves PUT /user/$ID {$updatedUser} endpoint:
- Improves the semantics and scope of the updatedUser() by moving
    operations in updateUserRequest's updatedUser() method.
    - Fixes the marshaling of the updateUserRequest by renaming and
    embedding the already existing type.
    - Fixes bug of password hashing by using `omitempty` tags in the
    update struct.
2019-08-15 00:36:03 +02:00
smavros
86870e3075 Adds test for POST /user 2019-08-13 19:30:21 +02:00
smavros
59a37bf601 Removes Credentials type from common/models.go:
Every test will fail except from the user's package since it is the
    only one using the new Request type.
2019-08-13 19:21:39 +02:00
smavros
487810f148 Adds Request type in requests.go:
This struct will be the basis every possible request. For now it
    contains only the fields of the User request. The idea is that it
    will containt every possible field of any model together with the
    `omitempty` flag so we can marshal only the initialized fields. This
    will deprecate all the ResponseMsg* types that were actually used
    for requests.
2019-08-13 19:15:31 +02:00
smavros
1f4d4c1cf7 Removes commented out types of ResponseMsgUser(s) 2019-08-13 17:51:02 +02:00
smavros
ac54500ed7 Removes ResponseMsgUsers from scenario test 2019-08-13 17:48:11 +02:00
smavros
b6a269bdeb Deprecates ResponseMsgUses. Introduces common/requests.go 2019-08-13 17:41:04 +02:00
smavros
ae2737571f Adds test for GET /user/:ID 2019-08-13 16:56:10 +02:00
smavros
299e314b88 Removes UsersSerializer from scenario test 2019-08-13 16:45:30 +02:00
smavros
74b27f7525 Remove redundant structs and methods:
UserSerializer, UsersSerializer, AuthResponse, UserResponse.
2019-08-13 16:40:54 +02:00
smavros
42f473cde0 Fix bug of User marshaling:
Instead of using the json.Marshal() we can user `gin.H` type of the
    response that will be passed through an XMLMarshaler. In that case
    we don't even need the responses structs!!
2019-08-13 16:20:58 +02:00
smavros
28d81e52c2 Derpecates UsersSerializer() from user endpoints 2019-08-13 16:17:37 +02:00
smavros
50b1902bf3 Deprecates common.UserSerializer() from user endpoints 2019-08-13 15:49:33 +02:00
smavros
b9d78a3823 Improves NewTestEndpoint():
When the HTTP code check is failing (non-200 message) the response
    body is printed so the failure can be identified from the test
    information.
2019-08-13 11:35:03 +02:00
smavros
0fb9bc9157 Fix of User.ID serialization:
As @skolen mentioned out, the `ID` field of the embedded struct
    `Model` in the model `User` must be serialized so it can be included
    in the responses. In the previous commit we completely ignored the
    field with the tag `json:"-"` so we can compare the serialized
    request (where ID was 0) with the response of the API.

    To limit the omission of the `User.ID` field only for the case that
    its value is 0 we modify the tag to `json:",omitempty"`.
2019-08-13 11:15:36 +02:00
smavros
76658c3ed4 Bug fix (read that commit message!):
The problem started with an internal error in the database (HTTP
    code 500, pq: duplicate key value) while trying to POST at /api/user
    with body {"user":{newUserObject}} to the backend, that already had
    three users User0 (Admin), UserA and UserB. Every test was
    succeeding.

    Eventually, the source of the problem was the initialization of the
    field `ID` (primary key) of the embedded struct `Model` in model
    `User`. When the `User` global variables User0, UserA and UserB
    were created in `common/testdata.go` the `Model.ID` field was set
    manually to the values 1, 2 and 3 respectively. This ofc was
    unnecessary but those variables were used for comparison with the
    relevant responses.

    When gorm (or postgres, not entirely sure) have a new variable of a
    model type, it will check if its primary key is zero (which means is
    uninitialized) and will set it to the last used value +1. That means
    that the new user that we were trying to insert in the DB got the
    primary key value 1. This value was already in use by the User0 so
    the DB refused to add the new user since there was a duplicate
    primary key value. That's why we were getting an postgre's error and
    HTTP 500.

    Hence to fix the bug this commit:

    - Removes `User.ID` initialization from `common/testdata.go`

    - Omits the `User.ID` from the json marshaler by setting the struct
    json tag to `json:"-"`. This is done because the request body is
    compared to the response *after* is serialized by a call to
    json.Marshal(). Since the `User.ID` is always uninitialized (value
    0) the jsondiff.Compare() will always return a "NoMatch".

    - Includes check for return of "SupersetMatch" in
    NewTestEndpoint()'s jsondiff.Compare() call. Since the `User.ID` is
    not serialized the json representation of the request will not
    include an "id" key. On the contrary, the response *has* an "id" key
    so it will always going to be a superset json object of the
    serialized request.

    - Modifies the validator's data struct to match the form of the
    request ({"modelKey":{requestBody}}) by adding an intermediate
    struct with the proper tag `json:"user"`.
2019-08-13 01:45:24 +02:00
smavros
bbe133655c Fixes scenario_test that is using NewTestEndpoint() 2019-08-11 22:48:21 +02:00
smavros
26bfe26f50 Improves NewTestEndpoint():
As the expectedResponse variable the responseBody is now marshaled
    in the body of the function and not in the test file. This
    simplifies the body of the testing function, improves the semantic,
    avoids marshaling repetition and checks for marshaling errors.
2019-08-11 22:47:00 +02:00
smavros
6aee1a7aca Improves NewAuthenticateForTest():
- As NewTestEndpoint() the credentials are passed as interface{} and
    marsaling is happening inside the function.
    - Add check for ignored error from http.NewRequest()
2019-08-11 20:03:23 +02:00
smavros
41bb54e933 Modifies NewTestEndpoint() and user_test:
Instead of marshaling the expected response in the test function the
    marshaling happens inside the NewTestEndpoint() where is passed as
    interface{}.
2019-08-11 19:41:48 +02:00
smavros
dbd8d7ddeb Adds DummyOnlyAdminDB() for testing with empty DB 2019-08-11 18:10:33 +02:00
smavros
4a1de4a471 Improves testdata. Removes User*_response. 2019-08-11 18:00:15 +02:00
smavros
15760f7d01 Variable renaming in LengthOfResponse() 2019-08-11 17:12:03 +02:00
smavros
9ef00d6204 Function LengthOfResponse() returns int and error 2019-08-11 17:11:13 +02:00
smavros
6a9709f7b8 Improves LengthOfResponse() utility function:
Problems arise from the fact that the are two kind of successful
    responses: The array of objects response like
    {"key":[{obj1},{obj2},...]} and the single object response like
    {"key":{obj}}. The function will try to check if the response can be
    unmarshaled in an array of generic type variables so to match the
    first case. If not it will try to unmarshal to a single generic type
    variable and 1 will be returned as the length of the response. If
    this will also fail -1 will be returned.
2019-08-11 16:55:20 +02:00
smavros
8f5c47dde9 Tags ID of Model as unique field 2019-08-10 22:41:43 +02:00
smavros
bf24fb9a8f Merge branch 'test-user-endpoints' of git.rwth-aachen.de:acs/public/villas/villasweb-backend-go into test-user-endpoints 2019-08-05 10:17:37 +02:00
smavros
a1c2baaa6c Added function LengthOfResponse() to utilities 2019-08-04 22:04:24 +02:00
smavros
8e185ecba4 Adds NewAuthenticateForTest() in common/utilities.go:
- The function must return the token and an error (string, error).
    In case of an error the token is going to be an empty string. The
    assertion for the error __must__ be executed in the body of the
    test. The utility functions __must not__ panic. All of the tests
    must use this function in the future.
2019-08-04 20:08:27 +02:00
smavros
e2430cadef Changes testing function in \users test file 2019-08-04 19:54:09 +02:00
smavros
61ccdb00c6 Changes to model's tags:
For multiple tag strings use __single space__ as separator according
    to the ```reflect``` package. Commas will __NOT__ work. That way the
    tags can be ordered arbitrarily.
2019-08-04 18:15:01 +02:00
Sonja Happ
169b49cf80 CI: add job for user testing 2019-07-31 14:10:54 +02:00
Sonja Happ
f84e6a1dda Fix in getUser endpoint for role User 2019-07-31 14:03:43 +02:00
Sonja Happ
ee708e1b7b Use capital letter for CommonModelFields type 2019-07-31 12:56:40 +02:00
smavros
a1b8d93263 Scenario test uses User serializer instead of UserResponse WIP 2019-07-31 11:34:40 +02:00
smavros
4c0d74a4a8 Adds new function for testing endpoints WIP:
- NewTestEndpoint() should replace TestEndpoint() since it is
    returning an error in case that the code or the response is not
    matching the expected one. The assertion __must__ be executed in the
    body of the actual test (of the corresponding package) so the
    printed error message can include the right number of line and the
    file where the assertion failed.
    - The function is used now only in package scenario tests.
2019-07-31 11:30:23 +02:00
Sonja Happ
a4a6ab0770 rename user validators file 2019-07-31 09:37:51 +02:00
Sonja Happ
05a70f80f6 add json tags to all models 2019-07-31 09:37:12 +02:00
smavros
cf69e59a5f Modifies testdata setup:
- Decorates User model with json tags for marshaling.
    - Introduces Model type same as gorm.Model with additional json tags
    for marshaling.
    - Modifies ResponseMsgUsers type.
    - Modifies the testdata for the users by including initializer for
    the Mode.ID field.
    - The scenario test fails for those changes
2019-07-30 20:31:59 +02:00
smavros
a283d32dff Adds first users endpoint test 2019-07-30 18:03:20 +02:00
smavros
e6c8fff397 Merge remote-tracking branch 'origin/master' into user-validators 2019-07-26 15:29:45 +02:00
smavros
be6556612c Only Admins can update the role of a user 2019-07-25 19:23:31 +02:00
smavros
8b7e8fb2bb Adds validator for user update 2019-07-25 18:58:28 +02:00
smavros
55be6d7e9f Adds source file for user validators:
- Adds validate() method for loginRequest and addUserRequest
    - Simplifies user endpoints validation
2019-07-25 17:46:46 +02:00