Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
go-gitlab
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
go-gitlab
Commits
5e1c7f37
Commit
5e1c7f37
authored
Apr 27, 2018
by
Joe Rocklin
Committed by
Sander van Harmelen
Mar 07, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update user block/unblock to use new error handling
parent
f4839529
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
81 additions
and
7 deletions
+81
-7
users.go
users.go
+6
-7
users_test.go
users_test.go
+75
-0
No files found.
users.go
View file @
5e1c7f37
...
...
@@ -17,7 +17,6 @@
package
gitlab
import
(
"errors"
"fmt"
"time"
)
...
...
@@ -431,7 +430,7 @@ func (s *UsersService) BlockUser(user int, options ...OptionFunc) error {
}
resp
,
err
:=
s
.
client
.
Do
(
req
,
nil
)
if
err
!=
nil
{
if
err
!=
nil
&&
resp
==
nil
{
return
err
}
...
...
@@ -439,9 +438,9 @@ func (s *UsersService) BlockUser(user int, options ...OptionFunc) error {
case
201
:
return
nil
case
403
:
return
errors
.
New
(
"Cannot block a user that is already blocked by LDAP synchronization"
)
return
UserBlockPreventedError
case
404
:
return
errors
.
New
(
"User does not exist"
)
return
UserNotFoundError
default
:
return
fmt
.
Errorf
(
"Received unexpected result code: %d"
,
resp
.
StatusCode
)
}
...
...
@@ -459,7 +458,7 @@ func (s *UsersService) UnblockUser(user int, options ...OptionFunc) error {
}
resp
,
err
:=
s
.
client
.
Do
(
req
,
nil
)
if
err
!=
nil
{
if
err
!=
nil
&&
resp
==
nil
{
return
err
}
...
...
@@ -467,9 +466,9 @@ func (s *UsersService) UnblockUser(user int, options ...OptionFunc) error {
case
201
:
return
nil
case
403
:
return
errors
.
New
(
"Cannot unblock a user that is blocked by LDAP synchronization"
)
return
UserUnblockPreventedError
case
404
:
return
errors
.
New
(
"User does not exist"
)
return
UserNotFoundError
default
:
return
fmt
.
Errorf
(
"Received unexpected result code: %d"
,
resp
.
StatusCode
)
}
...
...
users_test.go
View file @
5e1c7f37
...
...
@@ -7,6 +7,81 @@ import (
"testing"
)
func
TestBlockUser
(
t
*
testing
.
T
)
{
mux
,
server
,
client
:=
setup
()
defer
teardown
(
server
)
mux
.
HandleFunc
(
"/users/1/block"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
testMethod
(
t
,
r
,
"POST"
)
w
.
WriteHeader
(
http
.
StatusCreated
)
})
err
:=
client
.
Users
.
BlockUser
(
1
)
if
err
!=
nil
{
t
.
Errorf
(
"Users.BlockUser returned error: %v"
,
err
)
}
}
func
TestBlockUser_UserNotFound
(
t
*
testing
.
T
)
{
mux
,
server
,
client
:=
setup
()
defer
teardown
(
server
)
mux
.
HandleFunc
(
"/users/1/block"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
testMethod
(
t
,
r
,
"POST"
)
w
.
WriteHeader
(
http
.
StatusNotFound
)
})
err
:=
client
.
Users
.
BlockUser
(
1
)
if
err
!=
UserNotFoundError
{
t
.
Errorf
(
"Users.BlockUser error.
\n
Expected: %+v
\n
Got: %+v"
,
UserNotFoundError
,
err
)
}
}
func
TestBlockUser_BlockPrevented
(
t
*
testing
.
T
)
{
mux
,
server
,
client
:=
setup
()
defer
teardown
(
server
)
mux
.
HandleFunc
(
"/users/1/block"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
testMethod
(
t
,
r
,
"POST"
)
w
.
WriteHeader
(
http
.
StatusForbidden
)
})
err
:=
client
.
Users
.
BlockUser
(
1
)
if
err
!=
UserBlockPreventedError
{
t
.
Errorf
(
"Users.BlockUser error.
\n
Expected: %+v
\n
Got: %+v"
,
UserBlockPreventedError
,
err
)
}
}
func
TestBlockUser_UnknownError
(
t
*
testing
.
T
)
{
mux
,
server
,
client
:=
setup
()
defer
teardown
(
server
)
mux
.
HandleFunc
(
"/users/1/block"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
testMethod
(
t
,
r
,
"POST"
)
w
.
WriteHeader
(
http
.
StatusTeapot
)
})
want
:=
fmt
.
Errorf
(
"Received unexpected result code: %d"
,
http
.
StatusTeapot
)
err
:=
client
.
Users
.
BlockUser
(
1
)
if
!
reflect
.
DeepEqual
(
err
,
want
)
{
t
.
Errorf
(
"Users.BlockUser error.
\n
Expected: %+v
\n
Got: %+v"
,
want
,
err
)
}
}
func
TestBlockUser_BadResponseFromNet
(
t
*
testing
.
T
)
{
client
:=
NewClient
(
nil
,
""
)
client
.
SetBaseURL
(
""
)
want
:=
"Post /users/1/block: unsupported protocol scheme
\"\"
"
err
:=
client
.
Users
.
BlockUser
(
1
)
if
err
.
Error
()
!=
want
{
t
.
Errorf
(
"Users.BlockUser error.
\n
Expected: %+v
\n
Got: %+v"
,
want
,
err
)
}
}
// ------------------------ Unblock user -------------------------
func
TestUnblockUser
(
t
*
testing
.
T
)
{
mux
,
server
,
client
:=
setup
()
defer
teardown
(
server
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment