Skip to content

Commit bf186fa

Browse files
committed
[AUTH TOKEN]: model and test that value was created
1 parent 53140c4 commit bf186fa

File tree

4 files changed

+121
-1
lines changed

4 files changed

+121
-1
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
defmodule CodeCorps.Repo.Migrations.CreateAuthToken do
2+
use Ecto.Migration
3+
4+
def change do
5+
create table(:auth_token) do
6+
add :value, :string
7+
8+
add :user_id, references(:users, on_delete: :delete_all)
9+
10+
timestamps()
11+
end
12+
13+
create index(:auth_token, [:user_id])
14+
end
15+
end

priv/repo/structure.sql

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,38 @@ SET default_tablespace = '';
5050

5151
SET default_with_oids = false;
5252

53+
--
54+
-- Name: auth_token; Type: TABLE; Schema: public; Owner: -
55+
--
56+
57+
CREATE TABLE auth_token (
58+
id integer NOT NULL,
59+
value character varying(255),
60+
user_id integer,
61+
inserted_at timestamp without time zone NOT NULL,
62+
updated_at timestamp without time zone NOT NULL
63+
);
64+
65+
66+
--
67+
-- Name: auth_token_id_seq; Type: SEQUENCE; Schema: public; Owner: -
68+
--
69+
70+
CREATE SEQUENCE auth_token_id_seq
71+
START WITH 1
72+
INCREMENT BY 1
73+
NO MINVALUE
74+
NO MAXVALUE
75+
CACHE 1;
76+
77+
78+
--
79+
-- Name: auth_token_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
80+
--
81+
82+
ALTER SEQUENCE auth_token_id_seq OWNED BY auth_token.id;
83+
84+
5385
--
5486
-- Name: categories; Type: TABLE; Schema: public; Owner: -
5587
--
@@ -1343,6 +1375,13 @@ CREATE SEQUENCE users_id_seq
13431375
ALTER SEQUENCE users_id_seq OWNED BY users.id;
13441376

13451377

1378+
--
1379+
-- Name: id; Type: DEFAULT; Schema: public; Owner: -
1380+
--
1381+
1382+
ALTER TABLE ONLY auth_token ALTER COLUMN id SET DEFAULT nextval('auth_token_id_seq'::regclass);
1383+
1384+
13461385
--
13471386
-- Name: id; Type: DEFAULT; Schema: public; Owner: -
13481387
--
@@ -1581,6 +1620,14 @@ ALTER TABLE ONLY user_tasks ALTER COLUMN id SET DEFAULT nextval('user_tasks_id_s
15811620
ALTER TABLE ONLY users ALTER COLUMN id SET DEFAULT nextval('users_id_seq'::regclass);
15821621

15831622

1623+
--
1624+
-- Name: auth_token_pkey; Type: CONSTRAINT; Schema: public; Owner: -
1625+
--
1626+
1627+
ALTER TABLE ONLY auth_token
1628+
ADD CONSTRAINT auth_token_pkey PRIMARY KEY (id);
1629+
1630+
15841631
--
15851632
-- Name: categories_pkey; Type: CONSTRAINT; Schema: public; Owner: -
15861633
--
@@ -1813,6 +1860,13 @@ ALTER TABLE ONLY users
18131860
ADD CONSTRAINT users_pkey PRIMARY KEY (id);
18141861

18151862

1863+
--
1864+
-- Name: auth_token_user_id_index; Type: INDEX; Schema: public; Owner: -
1865+
--
1866+
1867+
CREATE INDEX auth_token_user_id_index ON auth_token USING btree (user_id);
1868+
1869+
18161870
--
18171871
-- Name: comments_task_id_index; Type: INDEX; Schema: public; Owner: -
18181872
--
@@ -2198,6 +2252,14 @@ CREATE UNIQUE INDEX users_lower_username_index ON users USING btree (lower((user
21982252
CREATE TRIGGER task_created BEFORE INSERT ON tasks FOR EACH ROW EXECUTE PROCEDURE assign_number();
21992253

22002254

2255+
--
2256+
-- Name: auth_token_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
2257+
--
2258+
2259+
ALTER TABLE ONLY auth_token
2260+
ADD CONSTRAINT auth_token_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
2261+
2262+
22012263
--
22022264
-- Name: comments_task_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
22032265
--
@@ -2610,5 +2672,5 @@ ALTER TABLE ONLY user_tasks
26102672
-- PostgreSQL database dump complete
26112673
--
26122674

2613-
INSERT INTO "schema_migrations" (version) VALUES (20160723215749), (20160804000000), (20160804001111), (20160805132301), (20160805203929), (20160808143454), (20160809214736), (20160810124357), (20160815125009), (20160815143002), (20160816020347), (20160816034021), (20160817220118), (20160818000944), (20160818132546), (20160820113856), (20160820164905), (20160822002438), (20160822004056), (20160822011624), (20160822020401), (20160822044612), (20160830081224), (20160830224802), (20160911233738), (20160912002705), (20160912145957), (20160918003206), (20160928232404), (20161003185918), (20161019090945), (20161019110737), (20161020144622), (20161021131026), (20161031001615), (20161121005339), (20161121014050), (20161121043941), (20161121045709), (20161122015942), (20161123081114), (20161123150943), (20161124085742), (20161125200620), (20161126045705), (20161127054559), (20161205024856), (20161207112519), (20161209192504), (20161212005641), (20161214005935), (20161215052051), (20161216051447), (20161218005913), (20161219160401), (20161219163909), (20161220141753), (20161221085759), (20161226213600), (20161231063614), (20170102130055), (20170102181053), (20170104113708), (20170104212623), (20170104235423), (20170106013143), (20170115035159), (20170115230549), (20170121014100), (20170131234029), (20170201014901), (20170201025454), (20170201035458), (20170201183258), (20170220032224), (20170224233516), (20170226050552), (20170228085250), (20170308214128), (20170308220713);
2675+
INSERT INTO "schema_migrations" (version) VALUES (20160723215749), (20160804000000), (20160804001111), (20160805132301), (20160805203929), (20160808143454), (20160809214736), (20160810124357), (20160815125009), (20160815143002), (20160816020347), (20160816034021), (20160817220118), (20160818000944), (20160818132546), (20160820113856), (20160820164905), (20160822002438), (20160822004056), (20160822011624), (20160822020401), (20160822044612), (20160830081224), (20160830224802), (20160911233738), (20160912002705), (20160912145957), (20160918003206), (20160928232404), (20161003185918), (20161019090945), (20161019110737), (20161020144622), (20161021131026), (20161031001615), (20161121005339), (20161121014050), (20161121043941), (20161121045709), (20161122015942), (20161123081114), (20161123150943), (20161124085742), (20161125200620), (20161126045705), (20161127054559), (20161205024856), (20161207112519), (20161209192504), (20161212005641), (20161214005935), (20161215052051), (20161216051447), (20161218005913), (20161219160401), (20161219163909), (20161220141753), (20161221085759), (20161226213600), (20161231063614), (20170102130055), (20170102181053), (20170104113708), (20170104212623), (20170104235423), (20170106013143), (20170115035159), (20170115230549), (20170121014100), (20170131234029), (20170201014901), (20170201025454), (20170201035458), (20170201183258), (20170220032224), (20170224233516), (20170226050552), (20170228085250), (20170308214128), (20170308220713), (20170308222552);
26142676

test/models/auth_token_test.exs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
defmodule CodeCorps.AuthTokenTest do
2+
use CodeCorps.ModelCase
3+
4+
alias CodeCorps.AuthToken
5+
6+
test "changeset with valid attributes" do
7+
user = insert(:user)
8+
changeset = AuthToken.changeset(%AuthToken{}, user)
9+
assert changeset.valid?
10+
assert changeset.changes.value
11+
end
12+
end

web/models/auth_token.ex

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
defmodule CodeCorps.AuthToken do
2+
@moduledoc """
3+
Represent's one of the user's many possible authentication tokens, created
4+
using `Phoenix.Token.sign/4`.
5+
6+
Many can coexist and be valid at the same time. They can be used for password
7+
resets or passwordless logins.
8+
9+
These tokens expire.
10+
"""
11+
12+
use CodeCorps.Web, :model
13+
14+
schema "auth_token" do
15+
field :value, :string
16+
17+
belongs_to :user, CodeCorps.User
18+
19+
timestamps()
20+
end
21+
22+
@doc """
23+
Builds a changeset based on the `struct`
24+
"""
25+
def changeset(struct, user) do
26+
token = CodeCorps.Endpoint |> Phoenix.Token.sign("user", user.id)
27+
struct
28+
|> cast(%{ value: token }, [:value])
29+
|> validate_required([:value])
30+
end
31+
end

0 commit comments

Comments
 (0)