-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UI and API for an admin to update a user
- Loading branch information
Showing
7 changed files
with
155 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<script lang="ts"> | ||
import {getUser, updateUser} from 'data/api/users' | ||
import type {User, UserUpdateInput} from 'data/types/user' | ||
import {goto} from '@roxi/routify' | ||
import {Button, Form, TextField} from '@silintl/ui-components' | ||
import {onMount} from 'svelte' | ||
export let id: string | ||
let user = {} as User | ||
const formData = {} as UserUpdateInput | ||
onMount(async () => { | ||
user = await getUser(id) | ||
formData.FirstName = user.FirstName | ||
formData.LastName = user.LastName | ||
formData.Email = user.Email | ||
}) | ||
const onSubmit = (event: any) => { | ||
updateUser(id, formData) | ||
$goto('/admin/users') | ||
} | ||
const onCancel = (event: Event) => { | ||
event.preventDefault() | ||
$goto('/admin/users') | ||
} | ||
</script> | ||
|
||
<h2>Edit User</h2> | ||
|
||
<Form on:submit={onSubmit}> | ||
<div class="my-1"> | ||
<p> | ||
<TextField required label="First Name" bind:value={formData.FirstName} /> | ||
</p> | ||
<p> | ||
<TextField required label="Last Name" bind:value={formData.LastName} /> | ||
</p> | ||
<p> | ||
<TextField required label="Email" bind:value={formData.Email} /> | ||
</p> | ||
</div> | ||
<div class="form-button"> | ||
<Button raised>Save</Button> | ||
</div> | ||
<div class="form-button"> | ||
<Button on:click={onCancel}>Cancel</Button> | ||
</div> | ||
</Form> | ||
|
||
|
||
<style> | ||
.form-button { | ||
float: right; | ||
margin: 0.5rem; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,3 +113,56 @@ func (ts *TestSuite) Test_GetUserList() { | |
}) | ||
} | ||
} | ||
|
||
func (ts *TestSuite) Test_usersUpdateHandler() { | ||
user := ts.createUserFixture(app.UserRoleBasic) | ||
admin := ts.createUserFixture(app.UserRoleAdmin) | ||
|
||
tests := []struct { | ||
name string | ||
actor db.User | ||
userID string | ||
wantStatus int | ||
}{ | ||
{ | ||
name: "not a valid user", | ||
userID: "xyz", | ||
wantStatus: http.StatusUnauthorized, | ||
}, | ||
{ | ||
name: "a user cannot update a user", | ||
actor: user, | ||
userID: admin.ID, | ||
wantStatus: http.StatusNotFound, | ||
}, | ||
{ | ||
name: "admin can update a user", | ||
actor: admin, | ||
userID: user.ID, | ||
wantStatus: http.StatusOK, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
ts.T().Run(tt.name, func(t *testing.T) { | ||
email := "[email protected]" | ||
input := app.UserUpdateInput{Email: &email} | ||
body, status := ts.request(http.MethodPut, "/api/users/"+tt.userID, tt.actor.Email, input) | ||
|
||
// Assertions | ||
ts.Equal(tt.wantStatus, status, "incorrect http status, body: \n%s", body) | ||
|
||
if tt.wantStatus != http.StatusOK { | ||
return | ||
} | ||
|
||
var gotUser app.User | ||
ts.NoError(json.Unmarshal(body, &gotUser)) | ||
ts.Equal(*input.Email, gotUser.Email, "incorrect User Email, body: \n%s", body) | ||
|
||
dbUser, err := db.FindUserByID(ts.ctx, gotUser.ID) | ||
ts.NoError(err) | ||
ts.Equal(*input.Email, dbUser.Email, "incorrect User Name in db") | ||
}) | ||
} | ||
} |