You ask a loaded question, friend.
The way I see it your question is not about what you should do, but about what is expected of you to do.
I would split your question into several sub-questions.
The first part is about a team leader or project owner asking a team member to perform a task he hasn't been prepared or hired for.
Is it moral or normal?
No, it is not, IMO.
Is it usual in a lot of companies to use testers as emergency devs?
Yes. The implications here are mostly ethical and usually financial, but let's not worry about those now, shall we?
Still, in a normal Agile team I would have pushed back against the request and have it formalized as a story or an epic.
Yes you'll get the usual bs that it doesn't add value to the customer, but your stakeholders are not only your customers.
If a stakeholder yields value from a story, it's valid effort.
Assuming you work in an Agile team, I would try to build a story around your assignment that can be tracked, assign it valid Acceptance Criteria, split it into sub-tasks, and time box each task. It should be easier to track all your research, implementation and failures/successes in small tasks that are timeboxed. This way your manager can track progress and you can justify time spent with progress to show.
The fact that you struggled on the task alone without anyone asking questions for two weeks tells me that you either don't work in an Agile team or that no one in your team cares about SCRUM meetings. This is bad for you either way.
I'd try to fix this before it gets worse. And by fixing this I mean making sure I have someone to tell I am stuck somewhere and don't know what to do, so a colleague could help me get unstuck.
The second issue with your situation is that the person that asked you to perform the task sold it short.
To you, it should have been a relatively simple task, similar to the ones you were performing before, but it turned out not so simple and not so like the ones before. This alone should have prompted you to raise the issue ASAP to the requester.
"Hello dear manager, you told me this would be nice and simple, a taste of real programming.
It's not. What do?"
This is what should have come out of your mouth (in one form or another) at some point in time. Preferably immediately after figuring out you've been duped.
I would suggest you make up for lost time and inform your manager that you were both in the wrong about the complexity of the task and do not share a common understanding of the necessary knowledge to complete it.
Obviously he thought you were capable of doing it because he assumed you possess the knowledge or are able to acquire it in a reasonable time frame. Obviously you thought the same.
Obviously you were both wrong.
Fixing this problem is a simple matter of re-aligning knowledge.
You already spent two weeks finding out you both are wrong.
Try to figure out why he was wrong in the first place and then explain it to your manager. Was your manager mistaken about your ability to code? Was he mistaken about your ability to learn? Were you too eager to show yourself? Answer those questions and more like them and you should be able to figure out how you got here in the first place. And what you should do to avoid the situation in the future. And maybe some ideas on how to fix it (assign more time to learning, prepare informal meetings with other devs to explain how they would do it would be a couple of ideas that come to mind).
Coming to the final part of your question: What should you do. Or more accurately, what do these people expect you to do?
The person that assigned you the task might expect different things than just completing the task within the allotted time frame. She might expect your team leader to evaluate your progress, quality of implementation, ability to learn while under pressure, desire to work with new technologies and current skills. She might want to make a developer out of you, should you choose this path. She might just need an inexpensive dev committing code that will be refactored in a few weeks just to do a demo for a client.
Your team leader might expect other things. She might care about how well you worked within your team while performing the task. If you asked for help when you got stuck. If you were able to recognize possible risks and handle them in a proactive manner. Or she might expect you to fail so she can hire a real dev.
I don't know, these are just speculations. You can find out how true they are by asking either of those persons what is expected of you before you take on the task.
It's not too late to ask now, either IMO.