Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
accountserver
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Monitor
Service Desk
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ai3
accountserver
Commits
a319c94b
Commit
a319c94b
authored
3 years ago
by
ale
Browse files
Options
Downloads
Patches
Plain Diff
Add tests for errors returned by the HTTP API
parent
b590bd1e
No related branches found
No related tags found
1 merge request
!42
Support Webauthn
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
integrationtest/errors_test.go
+93
-0
93 additions, 0 deletions
integrationtest/errors_test.go
integrationtest/integration_test.go
+11
-3
11 additions, 3 deletions
integrationtest/integration_test.go
with
104 additions
and
3 deletions
integrationtest/errors_test.go
0 → 100644
+
93
−
0
View file @
a319c94b
package
integrationtest
import
(
"encoding/json"
"errors"
"net/http"
"testing"
as
"git.autistici.org/ai3/accountserver"
)
func
TestIntegration_Error_GetUser
(
t
*
testing
.
T
)
{
stop
,
_
,
c
:=
startService
(
t
)
defer
stop
()
testdata
:=
[]
struct
{
reqUser
string
authUser
string
authGroup
string
expectedErrCode
int
}{
{
"uno@investici.org"
,
"due@investici.org"
,
""
,
http
.
StatusForbidden
},
{
"zero@investici.org"
,
testAdminUser
,
testAdminGroup
,
http
.
StatusNotFound
},
{
""
,
testAdminUser
,
testAdminGroup
,
http
.
StatusNotFound
},
}
for
_
,
td
:=
range
testdata
{
var
user
as
.
User
var
groups
[]
string
if
td
.
authGroup
!=
""
{
groups
=
append
(
groups
,
td
.
authGroup
)
}
err
:=
c
.
request
(
"/api/user/get"
,
&
as
.
GetUserRequest
{
UserRequestBase
:
as
.
UserRequestBase
{
RequestBase
:
as
.
RequestBase
{
SSO
:
c
.
ssoTicket
(
td
.
authUser
,
groups
...
),
},
Username
:
td
.
reqUser
,
},
},
&
user
)
if
err
==
nil
{
t
.
Errorf
(
"oops, request for %s as %s succeeded unexpectedly"
,
td
.
reqUser
,
td
.
authUser
)
continue
}
var
httpErr
*
httpError
if
!
errors
.
As
(
err
,
&
httpErr
)
{
t
.
Errorf
(
"oops, request for %s as %s returned unexpected error: %v"
,
td
.
reqUser
,
td
.
authUser
,
err
)
continue
}
if
httpErr
.
code
!=
td
.
expectedErrCode
{
t
.
Errorf
(
"request for %s as %s returned error code %d, expected %d"
,
td
.
reqUser
,
td
.
authUser
,
httpErr
.
code
,
td
.
expectedErrCode
)
}
}
}
func
TestIntegration_Error_Validation
(
t
*
testing
.
T
)
{
stop
,
_
,
c
:=
startService
(
t
)
defer
stop
()
var
resp
as
.
CreateUserResponse
err
:=
c
.
request
(
"/api/user/create"
,
&
as
.
CreateUserRequest
{
AdminRequestBase
:
as
.
AdminRequestBase
{
RequestBase
:
as
.
RequestBase
{
SSO
:
c
.
ssoTicket
(
testAdminUser
),
},
},
User
:
&
as
.
User
{
Name
:
"%@example.com"
,
Resources
:
[]
*
as
.
Resource
{
&
as
.
Resource
{
Type
:
as
.
ResourceTypeEmail
,
Name
:
"%@example.com"
,
},
},
},
},
&
resp
)
if
err
==
nil
{
t
.
Fatal
(
"CreateUser() succeeded unexpectedly"
)
}
var
httpErr
*
httpError
if
!
errors
.
As
(
err
,
&
httpErr
)
{
t
.
Fatalf
(
"CreateUser() did not return a structured error: %v"
,
err
)
}
var
fields
map
[
string
][]
string
if
err
:=
json
.
Unmarshal
(
httpErr
.
data
,
&
fields
);
err
!=
nil
{
t
.
Fatalf
(
"error unmarshaling JSON error: %v"
,
err
)
}
if
values
,
ok
:=
fields
[
"name"
];
!
ok
||
len
(
values
)
!=
1
{
t
.
Fatalf
(
"error does not contain details for the 'name' field: %+v"
,
fields
)
}
}
This diff is collapsed.
Click to expand it.
integrationtest/integration_test.go
+
11
−
3
View file @
a319c94b
...
@@ -4,7 +4,6 @@ import (
...
@@ -4,7 +4,6 @@ import (
"bytes"
"bytes"
"context"
"context"
"encoding/json"
"encoding/json"
"errors"
"fmt"
"fmt"
"io/ioutil"
"io/ioutil"
"log"
"log"
...
@@ -116,6 +115,15 @@ func (c *testClient) ssoTicket(username string, groups ...string) string {
...
@@ -116,6 +115,15 @@ func (c *testClient) ssoTicket(username string, groups ...string) string {
return
signed
return
signed
}
}
type
httpError
struct
{
code
int
data
[]
byte
}
func
(
e
*
httpError
)
Error
()
string
{
return
fmt
.
Sprintf
(
"HTTP status %d"
,
e
.
code
)
}
func
(
c
*
testClient
)
request
(
uri
string
,
req
,
out
interface
{})
error
{
func
(
c
*
testClient
)
request
(
uri
string
,
req
,
out
interface
{})
error
{
data
,
_
:=
json
.
Marshal
(
req
)
data
,
_
:=
json
.
Marshal
(
req
)
resp
,
err
:=
http
.
Post
(
c
.
srvURL
+
uri
,
"application/json"
,
bytes
.
NewReader
(
data
))
resp
,
err
:=
http
.
Post
(
c
.
srvURL
+
uri
,
"application/json"
,
bytes
.
NewReader
(
data
))
...
@@ -127,11 +135,11 @@ func (c *testClient) request(uri string, req, out interface{}) error {
...
@@ -127,11 +135,11 @@ func (c *testClient) request(uri string, req, out interface{}) error {
if
resp
.
StatusCode
>=
400
&&
resp
.
StatusCode
<
500
{
if
resp
.
StatusCode
>=
400
&&
resp
.
StatusCode
<
500
{
log
.
Printf
(
"request error: %s"
,
string
(
data
))
log
.
Printf
(
"request error: %s"
,
string
(
data
))
return
errors
.
New
(
string
(
data
))
return
&
httpError
{
code
:
resp
.
StatusCode
,
data
:
data
}
}
}
if
resp
.
StatusCode
!=
200
{
if
resp
.
StatusCode
!=
200
{
log
.
Printf
(
"remote error: %s"
,
string
(
data
))
log
.
Printf
(
"remote error: %s"
,
string
(
data
))
return
fmt
.
Errorf
(
"http status code %d"
,
resp
.
StatusCode
)
return
&
httpError
{
code
:
resp
.
StatusCode
,
data
:
data
}
}
}
if
resp
.
Header
.
Get
(
"Content-Type"
)
!=
"application/json"
{
if
resp
.
Header
.
Get
(
"Content-Type"
)
!=
"application/json"
{
return
fmt
.
Errorf
(
"unexpected content-type %s"
,
resp
.
Header
.
Get
(
"Content-Type"
))
return
fmt
.
Errorf
(
"unexpected content-type %s"
,
resp
.
Header
.
Get
(
"Content-Type"
))
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment