feat(server): Replace single-user service with muti-users service (#62)
* feat(svc/multiusers): add multi-users service * test(multiusers): add unit tests for user store * feat(multiusers): add multiusers service and refactor userstore * feat(multiusers): add adduser api and tests * feat(client): add adduser api
This commit is contained in:
parent
1680c5cb2f
commit
4b6f6d9e1f
13 changed files with 866 additions and 90 deletions
|
@ -1,10 +1,11 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
su "github.com/ihexxa/quickshare/src/handlers/singleuserhdr"
|
||||
"github.com/ihexxa/quickshare/src/handlers/multiusers"
|
||||
"github.com/parnurzeal/gorequest"
|
||||
)
|
||||
|
||||
|
@ -27,7 +28,7 @@ func (cl *SingleUserClient) url(urlpath string) string {
|
|||
|
||||
func (cl *SingleUserClient) Login(user, pwd string) (*http.Response, string, []error) {
|
||||
return cl.r.Post(cl.url("/v1/users/login")).
|
||||
Send(su.LoginReq{
|
||||
Send(multiusers.LoginReq{
|
||||
User: user,
|
||||
Pwd: pwd,
|
||||
}).
|
||||
|
@ -42,10 +43,29 @@ func (cl *SingleUserClient) Logout(token *http.Cookie) (*http.Response, string,
|
|||
|
||||
func (cl *SingleUserClient) SetPwd(oldPwd, newPwd string, token *http.Cookie) (*http.Response, string, []error) {
|
||||
return cl.r.Patch(cl.url("/v1/users/pwd")).
|
||||
Send(su.SetPwdReq{
|
||||
Send(multiusers.SetPwdReq{
|
||||
OldPwd: oldPwd,
|
||||
NewPwd: newPwd,
|
||||
}).
|
||||
AddCookie(token).
|
||||
End()
|
||||
}
|
||||
|
||||
func (cl *SingleUserClient) AddUser(name, pwd, role string, token *http.Cookie) (*http.Response, *multiusers.AddUserResp, []error) {
|
||||
resp, body, errs := cl.r.Post(cl.url("/v1/users/")).
|
||||
AddCookie(token).
|
||||
Send(multiusers.AddUserReq{
|
||||
Name: name,
|
||||
Pwd: pwd,
|
||||
Role: role,
|
||||
}).
|
||||
End()
|
||||
|
||||
auResp := &multiusers.AddUserResp{}
|
||||
err := json.Unmarshal([]byte(body), auResp)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
return nil, nil, errs
|
||||
}
|
||||
return resp, auResp, nil
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import { BaseClient, Response } from "./";
|
||||
|
||||
|
||||
export class UsersClient extends BaseClient {
|
||||
constructor(url: string) {
|
||||
super(url);
|
||||
|
@ -15,7 +14,7 @@ export class UsersClient extends BaseClient {
|
|||
pwd,
|
||||
},
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// token cookie is set by browser
|
||||
logout = (): Promise<Response> => {
|
||||
|
@ -23,14 +22,14 @@ export class UsersClient extends BaseClient {
|
|||
method: "post",
|
||||
url: `${this.url}/v1/users/logout`,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
isAuthed = (): Promise<Response> => {
|
||||
return this.do({
|
||||
method: "get",
|
||||
url: `${this.url}/v1/users/isauthed`,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// token cookie is set by browser
|
||||
setPwd = (oldPwd: string, newPwd: string): Promise<Response> => {
|
||||
|
@ -42,5 +41,18 @@ export class UsersClient extends BaseClient {
|
|||
newPwd,
|
||||
},
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// token cookie is set by browser
|
||||
adduser = (name: string, pwd: string, role: string): Promise<Response> => {
|
||||
return this.do({
|
||||
method: "post",
|
||||
url: `${this.url}/v1/users/`,
|
||||
data: {
|
||||
name,
|
||||
pwd,
|
||||
role,
|
||||
},
|
||||
});
|
||||
};
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ export class MockUsersClient {
|
|||
private logoutMockResp: Promise<Response>;
|
||||
private isAuthedMockResp: Promise<Response>;
|
||||
private setPwdMockResp: Promise<Response>;
|
||||
private addUserMockResp: Promise<Response>;
|
||||
|
||||
constructor(url: string) {
|
||||
this.url = url;
|
||||
|
@ -24,6 +25,9 @@ export class MockUsersClient {
|
|||
setPwdMock = (resp: Promise<Response>) => {
|
||||
this.setPwdMockResp = resp;
|
||||
}
|
||||
addUserMock = (resp: Promise<Response>) => {
|
||||
this.addUserMockResp = resp;
|
||||
}
|
||||
|
||||
login = (user: string, pwd: string): Promise<Response> => {
|
||||
return this.loginMockResp;
|
||||
|
@ -41,4 +45,8 @@ export class MockUsersClient {
|
|||
return this.setPwdMockResp;
|
||||
}
|
||||
|
||||
addUser = (name: string, pwd: string, role: string): Promise<Response> => {
|
||||
return this.addUserMockResp;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue