11defmodule NervesHub.ScriptsTest do
2+ alias NervesHub.Accounts
23 use NervesHub.DataCase
34
45 alias NervesHub.Fixtures
56 alias NervesHub.Scripts
67
78 setup do
89 user = Fixtures . user_fixture ( )
10+ user2 = Fixtures . user_fixture ( )
911 org = Fixtures . org_fixture ( user )
1012 product = Fixtures . product_fixture ( user , org )
1113
12- % { user: user , org: org , product: product }
14+ % { user: user , user2: user2 , org: org , product: product }
1315 end
1416
1517 describe "creating a script" do
@@ -21,6 +23,63 @@ defmodule NervesHub.ScriptsTest do
2123 } )
2224
2325 assert script . product_id == product . id
26+ assert script . created_by_id == user . id
27+ end
28+ end
29+
30+ describe "updating a script" do
31+ test "successful update" , % { product: product , user: user } do
32+ { :ok , script } =
33+ Scripts . create ( product , user , % {
34+ name: "MOTD" ,
35+ text: "NervesMOTD.print()"
36+ } )
37+
38+ { :ok , script } =
39+ Scripts . update ( script , user , % { name: "New Name" } )
40+
41+ assert script . name == "New Name"
42+ assert script . last_updated_by_id == user . id
43+ end
44+
45+ test "other user updates script" , % {
46+ product: product ,
47+ user: user ,
48+ user2: user2
49+ } do
50+ { :ok , script } =
51+ Scripts . create ( product , user , % {
52+ name: "MOTD" ,
53+ text: "NervesMOTD.print()"
54+ } )
55+
56+ { :ok , script } =
57+ Scripts . update ( script , user , % { name: "New Name" } )
58+
59+ { :ok , script } =
60+ Scripts . update ( script , user2 , % { text: "New text" } )
61+
62+ assert script . text == "New text"
63+ assert script . last_updated_by_id == user2 . id
64+ end
65+ end
66+
67+ describe "user removal" do
68+ test "user is removed - editor fields are nilified" , % { product: product , user: user } do
69+ { :ok , script } =
70+ Scripts . create ( product , user , % {
71+ name: "MOTD" ,
72+ text: "NervesMOTD.print()"
73+ } )
74+
75+ { :ok , _script } =
76+ Scripts . update ( script , user , % { name: "New Name" } )
77+
78+ Accounts . remove_account ( user . id )
79+
80+ script = Scripts . get! ( script . id )
81+ assert script . created_by_id == nil
82+ assert script . last_updated_by_id == nil
2483 end
2584 end
2685end
0 commit comments