Fork me on GitHub
#beginners
<
2017-01-08
>
notanon03:01:48

is "Friend authentication" some specific technology or pattern or something?

notanon04:01:41

@sb you'll either need to create a server endpoint to inform the client (the browser) the user is authenticated or have the server send the extra element down in the html payload, depending on whether you're rendering html on the server or using a client library on the browser to render page

notanon04:01:57

of course this is true generally, and not specific to Friend

notanon04:01:57

which method are you using to render the html? the server (hiccup maybe?) or the client (reagent maybe?)

notanon04:01:02

either way it will involve some logic in here

(def secured-app
  (-> ring-app
    (friend/authenticate {:credential-fn (partial creds/bcrypt-credential-fn users)
                          :workflows [(workflows/interactive-form)]})
    ; ...required Ring middlewares ...
    ))

notanon04:01:59

after you get past friend/authenticate, you'll either add the extra element to the hiccup based on the fact they're logged in

notanon04:01:26

if you're using reagent or something, i'd have to look into friend's details to see if they add tokens to the response which your reagent code could use to determine you're 'logged in'

sb06:01:11

Thanks @notanon, Yes, I use hiccup and I created in this way the normal authentication.

sb06:01:05

Yes, I use the same logic. I try it, if I will have question I will come back!

roelof13:01:35

spec question

roelof13:01:54

I need a spec where there is a name keyword with as value z4

roelof13:01:16

I tried this :

(s/def ::test (s/keys :req-un [::name])) 

roelof13:01:38

and then :

(s/def ::name "z4")   

roelof13:01:59

but when I do :

(s/exercise ::test)  

roelof13:01:19

then I see this message : ExceptionInfo Unable to construct gen at: [:name] for: :name

roelof13:01:45

anyone who can give me a hint how to solve this ?

Alex Miller (Clojure team)13:01:48

Use (s/def ::name #{"z4"})

Alex Miller (Clojure team)13:01:28

Use a set for any spec that enumerates values

neurogoo18:01:09

In reagent I want to have make on-click event that is given the id of the parent element of the event. How should I do this?

neurogoo18:01:17

I have tried something like wrapping the whole component with let, but it seems that the event doesn't seem to get the values once it is called

roelof18:01:24

I hope the last spec question.

roelof18:01:36

I have these specs :

(s/def :image/tiles
 (s/coll-of (s/keys :req-un [::url] ):min-count 1 :max-count 1))

(s/def ::name #{"z4"})

(s/def :image/levels
 (s/keys :req-un [::name :image/tiles]))

(s/def :image/body
 (s/keys :reg-un [:image/levels]))

(s/def :image/response
 (s/keys :req-un [:image/body])) 

roelof18:01:32

when I do (s/exercise :image/response) I see only these results :

([{:body {}} {:body {}}] [{:body {}} {:body {}}] [{:body {}} {:body {}}] [{:body {}} {:body {}}] [{:body {}} {:body {}}] [{:body {}} {:body {}}] [{:body {}} {:body {}}] [{:body {}} {:body {}}] [{:body {}} {:body {}}] [{:body {}} {:body {}}])
 

roelof18:01:24

How can I take care that something like this is schown :

{
  "levels": [{
       "tiles": [{
            "url": ""
    },   

schmee19:01:47

roelof there is a typo in your image/body spec, it says :reg-un instead of :req-un

schmee19:01:03

it will be nice when we have specs for spec so we can catch such errors šŸ™‚

roelof19:01:02

Yes, and then specs to catch errors in the specs to catch error in specs šŸ™‚

schmee19:01:26

and down the rabbit hole we go šŸ˜„

roelof19:01:20

one question , I have made this spec to test a function :

(s/fdef get-image-url
 :args (s/cat :response :image/response)
 :ret  :image/object) 

roelof19:01:44

but when I do (stest/summarize-results (stest/check 'paintings2/api-get/get-image-url'))

roelof19:01:04

I see as output {:total 0}

roelof19:01:27

here are the specs :

(s/def ::url string?)

(s/def :image/tiles
 (s/coll-of (s/keys :req-un [::url] ):min-count 1 :max-count 1))

(s/def :image/name #{"z4"})

(s/def :image/levels
 (s/coll-of (s/keys :req-un [:image/name :image/tiles]) :min-count 1 :max-count 1  ))

(s/def :image/body
 (s/keys :req-un [:image/levels]))

(s/def :image/response
 (s/keys :req-un [:image/body]))

(s/def :image/object
 (s/keys :req-un [::url]))
 

roelof19:01:00

and here the function that needs to be tested :

(defn get-image-url
  "Reads the image-url"
  [response]
  (let [art-objects (-> response
                        :body
                        :levels)
        url (filter #(= (:name %) "z4") art-objects)
        tiles (:tiles (first url))
        image (get-in tiles [0 :url])]

    {:tiles image})) 

roelof19:01:11

Did I do something wrong here ?

schmee19:01:26

yep šŸ™‚

schmee19:01:10

stest/check takes a fully namespace-qualified symbol, so you want

(stest/check `paintings2/api-get/get-image-url)
(notice the backtick)

schmee19:01:08

Iā€™m not sure how it works if you have more than one / in a symbol name

schmee19:01:50

what does your ns declaration look like?

roelof19:01:30

your right, there is also a typo

roelof19:01:17

one last question : I have this spec

(s/def :image/tiles
 (s/coll-of (s/keys :req-un [::url] ) :min-count 1 :max-count 1)) 

roelof19:01:38

and this one

(s/def :image/levels
 (s/coll-of (s/keys :req-un [:image/name :image/tiles]) :min-count 1 :max-count 1))  

roelof19:01:06

so I hoped that tiles will always have one url

roelof19:01:38

why do I see this failing test :

{:cause "Specification-based check failed", 
            :via [{:type clojure.lang.ExceptionInfo, 
                   :at [clojure.core$ex_info 
                        invokeStatic 
                        "core.clj" 
                        4725], 
                   :message "Specification-based check failed", 
                   :data {:clojure.spec/problems ({:path [:ret], 
                                                   :pred (contains? 
                                                          % 
                                                          :url), 
                                                   :via [], 
                                                   :val {:tiles ""}, 
                                                   :in []}), 
                          :clojure.spec.test/val {:tiles ""}, 
                          :clojure.spec.test/args ({:body {:levels [{:name "z4", 
                                                                     :tiles [{:url ""}]}]}}), 
                          :clojure.spec/failure :check-failed}}],   

roelof19:01:07

where there seems not to be a url in the tiles part as I understand this well

roelof19:01:23

anyone a tip how to solve this ?

schmee19:01:32

roelof your says that you need to have a :url key

schmee19:01:00

but your function returns {:tiles image}, i.e. no :url key

roelof19:01:09

correct the contents of the url is the place where the image can be found

roelof19:01:48

so it wil be something like this {:tiles "http://image.com/image " }

schmee19:01:37

yes, but in your fn spec you say that it returns an image/object:

(s/fdef get-image-url
 :args (s/cat :response :image/response)
 :ret  :image/object) 

schmee19:01:59

and image/object requires a :url key:

(s/def :image/object
 (s/keys :req-un [::url]))

schmee19:01:32

but in your return you only have a hash with a :tiles key, and no :url key

schmee19:01:10

I think you want to replace :ret in your fn spec with image/tiles

schmee19:01:25

or fix the return value of the function

roelof19:01:29

oke, I have this function :

(defn get-image-url
  "Reads the image-url"
  [response]
  (let [art-objects (-> response
                        :body
                        :levels)
        url (filter #(= (:name %) "z4") art-objects)
        tiles (:tiles (first url))
        image (get-in tiles [0 :url])]

    {:tiles image}))  

roelof19:01:42

so image is the url of the image, right ?

roelof19:01:43

it looks like this : [{:body {:levels [{:name "z4", :tiles [{:url ""}]

roelof19:01:58

and I need the contents of the url part

roelof19:01:18

@schmee so far so good ?

schmee19:01:58

yes, try replacing :ret in your fn spec with image/tiles and see if that solves your problem šŸ™‚

roelof19:01:03

same problem

roelof19:01:29

at some way the test is using a empty tiles

roelof19:01:50

so this [{:body {:levels [{:name "z4", :tiles [{}]

roelof19:01:59

and then the url part cannot be found

schmee19:01:18

are you sure?

schmee19:01:44

in the example you posted above, there is a different error

roelof19:01:02

yep, I still see this error :

({:failure {:cause "Specification-based check failed", 
            :via [{:type clojure.lang.ExceptionInfo, 
                   :at [clojure.core$ex_info 
                        invokeStatic 
                        "core.clj" 
                        4725], 
                   :message "Specification-based check failed", 
                   :data {:clojure.spec/problems ({:path [:ret], 
                                                   :pred map?, 
                                                   :via [], 
                                                   :val [:tiles ""], 
                                                   :in [0]}), 
                          :clojure.spec.test/val {:tiles ""}, 
                          :clojure.spec.test/args ({:body {:levels [{:name "z4", 
                                                                     :tiles [{:url ""}]}]}}), 
                          :clojure.spec/failure :check-failed}}],  

roelof19:01:26

is it now the input or output spec which is failing. Im confused now

roelof19:01:54

aha, I see it

roelof19:01:26

I use tiles but I have also said this :

(s/def :image/tiles
 (s/coll-of (s/keys :req-un [::url] ) :min-count 1 :max-count 1)) 

neurogoo19:01:09

I can't anymore find who replied me, but I'm doing something like

neurogoo19:01:39

(let [album-id (:id album)]
   [:ul {:on-click (fn []
                     (swap! opened not)
                     (when-not ((keyword album-id) @album-songs)
                       (GET (str "/song/album/" album-id)
                            {:handler (fn [response]
                                        (swap! album-songs assoc (keyword album-id) (js->clj response)))
                             :error-handler error-handler})))}
    (:name album]))

roelof19:01:27

solved it by making two seperate :tiles spec. One for the input and one for the output

roelof19:01:46

@schmee thanks for the pointers