Commit graph

34 commits

Author SHA1 Message Date
smavros
5eed44f8f3 Adds function GetResponseID() for reading "id" 2019-08-25 10:58:02 +02:00
smavros
89bdb59e5d Separates NewTestEndpoint() response and code comparison:
NewTestEndpoint() function must only send the HTTP request to the
    server and return the code, response and an error. This serves cases
    where the response cannot be compared with a predefined message e.g.
    when a new user is added we should not assume its assigned ID
    beforehand. The assertions of probable error, non-200 codes and non
    expected response bodies should be done inside the test function.
2019-08-18 18:44:41 +02:00
smavros
d3aa873732 Improves LengthOfResponse() 2019-08-17 20:53:33 +02:00
smavros
ff19fdcdaf Modifies NewTestEndpoint() by removing if body == nil 2019-08-15 18:44:49 +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
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
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
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
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
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
1f5e64b9ea Fixex for simulator POST/ PUT endpoints and simulator testing, 2019-07-24 16:39:59 +02:00
Sonja Happ
eb277fc92e use jsondiff in endpoint testing 2019-07-24 13:17:47 +02:00
Sonja Happ
c33bcd0202 Fix password validation to work with testing DB entries 2019-07-04 15:50:48 +02:00
Sonja Happ
7edb27de2e Merge branch 'user-validators'
# Conflicts:
#	go.mod
#	go.sum
#	routes/user/userEndpoints.go
#	routes/user/userMethods.go
2019-07-04 15:39:41 +02:00
Sonja Happ
304680603d implement DELETE endpoint for files, cleanup 2019-07-04 10:34:54 +02:00
Sonja Happ
7c9318de8f add testing for widget endpoints 2019-06-17 14:15:39 +02:00
smavros
b74b5f3a44 Fixes bug in getUsers(). Adds helper funnction. 2019-06-10 16:47:46 +02:00
Sonja Happ
b6c2d9fc6a mode testEndpoint and authenticateForTest functions to common package 2019-06-05 16:21:46 +02:00
Sonja Happ
6081fab080 add permission checks to simulation model endpoints 2019-06-05 16:05:27 +02:00
Sonja Happ
072d948795 - checkPermissions method for simulation endpoints
- remove simulationID from simulationmodel and visualization endpoints
2019-06-05 14:34:44 +02:00
Sonja Happ
4806583b1f Merge branch 'authentication'
# Conflicts:
#	common/models.go
#	common/utilities.go
#	routes/user/userEndpoints.go
2019-06-05 10:42:49 +02:00
smavros
4cc98867ca Merge branch 'authentication' of git.rwth-aachen.de:acs/public/villas/villasweb-backend-go into authentication 2019-06-05 10:03:49 +02:00
smavros
1b39f2fd83 Adds global constant strings for Context custom variables 2019-06-05 10:03:12 +02:00
Sonja Happ
d3cdf0008b - rename Modes to Permissions
- rename actions.go to roles.go
- put all role related functions in file roles.go
2019-06-05 09:38:17 +02:00
smavros
e4c8fb9825 Renames IsActionAllowed() to ValidateRole() 2019-06-04 20:33:27 +02:00
smavros
5d3bb4c632 Improves actions mechanism in common package
- Typedefs the name of a model (ModelName) and the CRUD operations
    (CRUD).
    - Defines constants for models's names and the 4 CRUD operations
    - Changes the corresponding functions that need to validate the role
2019-06-04 20:22:09 +02:00
smavros
1a1a3c1876 Adds IsActionAllowed() in common utilities
- Is used only in deleteUser handler function for now
2019-06-04 18:03:47 +02:00
Sonja Happ
786e36398b implement model-specific methods instead of queries (WIP) 2019-05-28 16:41:30 +02:00
Sonja Happ
144c97ce49 Refactoring code base to new structure, removing circular dependencies, creating response types 2019-05-23 16:50:05 +02:00