@@ -8,7 +8,8 @@ defmodule ResourceManager.Factory do
8
8
9
9
@ default_password "My-passw@rd123"
10
10
11
- @ doc false
11
+ @ doc "Builds a default struct from the requested model"
12
+ @ spec build ( model :: atom ( ) ) :: struct ( )
12
13
def build ( :user ) do
13
14
% User {
14
15
username: "my-test-username#{ System . unique_integer ( ) } " ,
@@ -37,7 +38,7 @@ defmodule ResourceManager.Factory do
37
38
38
39
def build ( :public_key ) do
39
40
% PublicKey {
40
- value: get_priv_public_key ( )
41
+ value: get_public_key ( )
41
42
}
42
43
end
43
44
@@ -62,43 +63,60 @@ defmodule ResourceManager.Factory do
62
63
}
63
64
end
64
65
65
- @ doc false
66
+ @ doc "Returns the a model struct with the given attributes"
67
+ @ spec build ( factory_name :: atom ( ) , attributes :: Keyword . t ( ) ) :: struct ( )
66
68
def build ( factory_name , attributes ) when is_atom ( factory_name ) and is_list ( attributes ) do
67
69
factory_name
68
70
|> build ( )
69
71
|> struct! ( attributes )
70
72
end
71
73
72
- @ doc false
74
+ @ doc "Inserts a model with the given attributes on database"
75
+ @ spec insert! ( factory_name :: atom ( ) , attributes :: Keyword . t ( ) ) :: struct ( )
73
76
def insert! ( factory_name , attributes \\ [ ] ) when is_atom ( factory_name ) do
74
77
factory_name
75
78
|> build ( attributes )
76
79
|> Repo . insert! ( )
77
80
end
78
81
79
- @ doc false
82
+ @ doc "Inserts a list of the given model on database"
83
+ @ spec insert_list! (
84
+ factory_name :: atom ( ) ,
85
+ count :: integer ( ) ,
86
+ attributes :: Keyword . t ( )
87
+ ) :: list ( struct ( ) )
80
88
def insert_list! ( factory_name , count \\ 10 , attributes \\ [ ] ) when is_atom ( factory_name ) ,
81
89
do: Enum . map ( 0 .. count , fn _ -> insert! ( factory_name , attributes ) end )
82
90
83
- @ doc false
91
+ @ doc "Returns the given password hashed using the selected algorithm"
92
+ @ spec gen_hashed_password (
93
+ password :: String . t ( ) ,
94
+ algorithm :: :argon2 | :bcrypt | :pbkdf2
95
+ ) :: String . t ( )
84
96
def gen_hashed_password ( password \\ @ default_password , alg \\ :argon2 )
85
- def gen_hashed_password ( password , :argon2 ) , do: Argon2 . hash_pwd_salt ( password )
86
- def gen_hashed_password ( password , :bcrypt ) , do: Bcrypt . hash_pwd_salt ( password )
87
- def gen_hashed_password ( password , :pbkdf2 ) , do: Pbkdf2 . hash_pwd_salt ( password )
88
-
89
- @ doc false
90
- def get_priv_public_key do
91
- :resource_manager
92
- |> :code . priv_dir ( )
93
- |> Path . join ( "/keys/resource_manager_key.pub" )
97
+
98
+ def gen_hashed_password ( password , :argon2 ) when is_binary ( password ) ,
99
+ do: Argon2 . hash_pwd_salt ( password )
100
+
101
+ def gen_hashed_password ( password , :bcrypt ) when is_binary ( password ) ,
102
+ do: Bcrypt . hash_pwd_salt ( password )
103
+
104
+ def gen_hashed_password ( password , :pbkdf2 ) when is_binary ( password ) ,
105
+ do: Pbkdf2 . hash_pwd_salt ( password )
106
+
107
+ @ doc "Returns the mocked public key"
108
+ @ spec get_public_key ( ) :: String . t ( )
109
+ def get_public_key do
110
+ File . cwd! ( )
111
+ |> Path . join ( "/test/support/mocks/keys/public_key.pub" )
94
112
|> File . read! ( )
95
113
end
96
114
97
- @ doc false
98
- def get_priv_private_key do
99
- :resource_manager
100
- |> :code . priv_dir ( )
101
- |> Path . join ( "/keys/resource_manager_key .pem" )
115
+ @ doc "Returns the mocked private key"
116
+ @ spec get_private_key ( ) :: String . t ( )
117
+ def get_private_key do
118
+ File . cwd! ( )
119
+ |> Path . join ( "/test/support/mocks/ keys/private_key .pem" )
102
120
|> File . read! ( )
103
121
end
104
122
end
0 commit comments